From d732bd0f74225828455c5863444b0211faf586d0 Mon Sep 17 00:00:00 2001 From: Louie S Date: Fri, 15 Mar 2024 15:41:31 -0400 Subject: Add backend for rules dialog --- src/backend/db_sqlite.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/backend/db_sqlite.h | 4 ++-- src/rulesDialog.cpp | 28 +++++++++++++++++++++++++++- src/rulesDialog.h | 2 ++ 4 files changed, 77 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 32108e1..f48089b 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -197,7 +197,7 @@ int BackendDB::insertGroup(const Group &new_group) { return output; } -// insert group to the database (returns 0 if failed) +// insert entry to the database (returns 0 if failed) int BackendDB::insertEntry(const Entry &new_entry) { int output; @@ -222,6 +222,29 @@ int BackendDB::insertEntry(const Entry &new_entry) { return output; } +// insert rule to the database (returns 0 if failed) +int BackendDB::insertRule(const Rule &new_rule) { + int output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("INSERT INTO rules (entry_id, before_after, date, color, highlight) VALUES (:e_id, :when, :date, :color, :highlight)"); + query.bindValue(":e_id", new_rule.entry_id); + query.bindValue(":when", new_rule.when); + query.bindValue(":date", new_rule.date.toString("yyyy-MM-dd")); + query.bindValue(":color", new_rule.color); + query.bindValue(":highlight", new_rule.highlight); + query.exec(); + + output = query.lastInsertId().toInt(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); + return output; +} + void BackendDB::updateGroup(const Group &group) { { QSqlDatabase database(this->openDB()); @@ -274,6 +297,28 @@ void BackendDB::updateEntry(const Entry &entry) { QSqlDatabase::removeDatabase("qt_sql_default_connection"); } +void BackendDB::updateRule(const Rule &rule) { + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("UPDATE rules SET " + "before_after = :when, " + "date = :date, " + "color = :color, " + "highlight = :highlight " + "WHERE id = :id"); + query.bindValue(":when", rule.when); + query.bindValue(":date", rule.date.toString("yyyy-MM-dd")); + query.bindValue(":color", rule.color); + query.bindValue(":highlight", rule.highlight); + query.bindValue(":id", rule.id); + query.exec(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); +} + // hide group and entries belonging to group // return value: number of affected rows int BackendDB::removeGroup(const Group &group) { diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index ce2d900..93ae050 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -19,10 +19,10 @@ class BackendDB : QSqlDatabase { QList loadRules(int entry_id); int insertGroup(const Group &new_group); int insertEntry(const Entry &new_entry); - int insertRule(int new_rule); // param datatype TBD + int insertRule(const Rule &new_rule); void updateGroup(const Group &group); void updateEntry(const Entry &entry); - void updateRule(int rule); // param datatype TBD + void updateRule(const Rule &rule); int removeGroup(const Group &group); int removeEntry(const Entry &entry); int removeRule(const Rule &rule); diff --git a/src/rulesDialog.cpp b/src/rulesDialog.cpp index b856ebb..be8b9d3 100644 --- a/src/rulesDialog.cpp +++ b/src/rulesDialog.cpp @@ -59,7 +59,6 @@ void RulesDialog::drawRules() { } void RulesDialog::addRule() { - // TODO handle in db backend to insert instead of updating if id is 0 Rule *new_rule = new Rule(0, this->entry_id, Rule::before); RuleLayout *new_layout = new RuleLayout(*new_rule); @@ -76,6 +75,33 @@ void RulesDialog::deleteRule(int i) { if(i >= this->rules.size()) return; this->updateRulesList(); + // mark as needing to be deleted from database if id > 0 + if(this->rules[i]->id > 0) + this->deleted_rules.append(*this->rules[i]); this->rules.removeAt(i); this->drawRules(); } + +void RulesDialog::accept() { + int i; + BackendDB database; + + this->updateRulesList(); + + // update rules in database + for(i = 0; i < this->rules.size(); ++i) { + // insert - this is a new rule + if(this->rules[i]->id == 0) + database.insertRule(*this->rules[i]); + // update - this is an existing rule + else + database.updateRule(*this->rules[i]); + } + + // delete deleted rules in database + for(i = 0; i < this->deleted_rules.size(); ++i) { + database.removeRule(this->deleted_rules[i]); + } + + QDialog::accept(); +} diff --git a/src/rulesDialog.h b/src/rulesDialog.h index 8f0fd81..25125fb 100644 --- a/src/rulesDialog.h +++ b/src/rulesDialog.h @@ -19,12 +19,14 @@ class RulesDialog : public QDialog { Ui::rulesDialog ui; int entry_id; QList rules; + QList deleted_rules; void updateRulesList(); private slots: void addRule(); void deleteRule(int i); + void accept(); }; #endif -- cgit