From 9c05cd5b5e9250f3cfd04a98370595db8ef8f697 Mon Sep 17 00:00:00 2001 From: Louie S Date: Wed, 28 Feb 2024 18:39:48 -0500 Subject: Working add_group_form --- src/add_group_form.cpp | 21 ++++++++++++--------- src/add_group_form.h | 2 +- src/backend/db_sqlite.cpp | 21 +++++++++++++++++++-- src/backend/db_sqlite.h | 8 +++++--- 4 files changed, 37 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/add_group_form.cpp b/src/add_group_form.cpp index 5662208..75ecd29 100644 --- a/src/add_group_form.cpp +++ b/src/add_group_form.cpp @@ -1,30 +1,33 @@ #include +#include + #include "add_group_form.h" +#include "backend/db_sqlite.h" AddGroupForm::AddGroupForm() { // load uic ui.setupUi(this); - - // setup dialog button connections - QObject::connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); - QObject::connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &AddGroupForm::handleSubmit); } -void AddGroupForm::handleSubmit() { +void AddGroupForm::accept() { QString name_text = ui.new_group_name->text(); QString column_text = ui.new_group_column->currentText(); QString link_text = ui.new_group_link->text(); QMessageBox error_message; + int new_id; if(name_text.isEmpty()) { + qDebug() << "Gets here"; error_message.setIcon(QMessageBox::Warning); error_message.setWindowTitle("Error Message"); error_message.setText("Name cannot be blank"); - error_message.setParent(this); - error_message.show(); + error_message.exec(); + return; } - // TODO insert into database - this->close(); + new_id = BackendDB::insertGroup(Group(0, name_text, column_text, link_text)); + // TODO redraw the main window + + QDialog::accept(); } diff --git a/src/add_group_form.h b/src/add_group_form.h index c3b3765..2206e34 100644 --- a/src/add_group_form.h +++ b/src/add_group_form.h @@ -13,7 +13,7 @@ class AddGroupForm : public QDialog { Ui::Dialog ui; private slots: - void handleSubmit(); + void accept(); }; #endif diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 9cd73f5..4479b09 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -91,7 +91,7 @@ QList BackendDB::loadGroups() { } // load entries -QList loadEntries() { +QList BackendDB::loadEntries() { QSqlDatabase database = openDB(); QSqlQuery query; QList output; @@ -116,7 +116,7 @@ QList loadEntries() { } // load entries -QList loadRules() { +QList BackendDB::loadRules() { QSqlDatabase database = openDB(); QSqlQuery query; QList output; @@ -135,3 +135,20 @@ QList loadRules() { database.close(); return output; } + +// insert group to the database (returns 0 if failed) +int BackendDB::insertGroup(const Group &new_group) { + QSqlDatabase database = openDB(); + QSqlQuery query; + int output; + + query.prepare("INSERT INTO groups (name, column, link) VALUES (?, ?, ?)"); + query.bindValue(0, new_group.name); + query.bindValue(1, new_group.column); + query.bindValue(2, new_group.link); + query.exec(); + + output = query.lastInsertId().toInt(); + database.close(); + return output; +} diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index 6e94fdd..fb04c00 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -8,6 +8,8 @@ #include "../group.h" #include "../rule.h" +// TODO rewrite to be a class (see difference between ::addDatabase and ::database) + namespace BackendDB { const QStringList create_table_queries = { "CREATE TABLE groups (" @@ -43,9 +45,9 @@ namespace BackendDB { QList loadGroups(); QList loadEntries(); QList loadRules(); - void insertGroup(int new_group); // param datatype TBD - void insertEntry(int new_entry); // param datatype TBD - void insertRule(int new_rule); // param datatype TBD + int insertGroup(const Group &new_group); // param datatype TBD + int insertEntry(int new_entry); // param datatype TBD + int insertRule(int new_rule); // param datatype TBD void updateGroup(int group); // param datatype TBD void updateEntry(int entry); // param datatype TBD void updateRule(int rule); // param datatype TBD -- cgit