summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouie S <louie@example.com>2024-03-15 15:41:31 -0400
committerLouie S <louie@example.com>2024-03-15 15:41:31 -0400
commitd732bd0f74225828455c5863444b0211faf586d0 (patch)
tree4c605e694cc02521c9fddb05a710c62d66a97e55
parent3e1d2e46ceea202120b4ff814fbd872bc6fc60f3 (diff)
Add backend for rules dialog
-rw-r--r--src/backend/db_sqlite.cpp47
-rw-r--r--src/backend/db_sqlite.h4
-rw-r--r--src/rulesDialog.cpp28
-rw-r--r--src/rulesDialog.h2
4 files changed, 77 insertions, 4 deletions
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<Rule *> 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<Rule *> rules;
+ QList<Rule> deleted_rules;
void updateRulesList();
private slots:
void addRule();
void deleteRule(int i);
+ void accept();
};
#endif