changeset 6:0315a3b9f560

Implement person and transaction history deletion.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 30 Mar 2017 15:12:18 +0300
parents a5a3baee3043
children b076c4dfc376
files main.cpp main.h mainwindow.ui
diffstat 3 files changed, 81 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/main.cpp	Thu Mar 30 13:50:42 2017 +0300
+++ b/main.cpp	Thu Mar 30 15:12:18 2017 +0300
@@ -320,6 +320,77 @@
 }
 
 
+int SyntilistaMainWindow::deletePerson(qint64 id)
+{
+    QSqlDatabase::database().transaction();
+
+    QSqlQuery del;
+    del.prepare("DELETE FROM people WHERE id=?");
+    del.addBindValue(id);
+    del.exec();
+    if (!checkAndReportSQLError("delete user", del.lastError()))
+    {
+        QSqlDatabase::database().rollback();
+        return -1;
+    }
+    
+    del.prepare("DELETE FROM transactions WHERE person=?");
+    del.addBindValue(id);
+    del.exec();
+
+    if (!checkAndReportSQLError("delete user transactions", del.lastError()))
+    {
+        QSqlDatabase::database().rollback();
+        return -2;
+    }
+    
+    QSqlDatabase::database().commit();
+    return 0;
+}
+
+
+void SyntilistaMainWindow::on_button_DeletePerson_clicked()
+{
+    if (currPerson.id <= 0)
+    {
+        statusMsg(tr("Ei valittua henkilöä!"));
+        return;
+    }
+    
+    PersonInfo info;
+    if (!getPersonInfo(currPerson.id, info))
+    {
+        statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(currPerson.id));
+        return;
+    }
+
+    QMessageBox::StandardButton ret =
+        QMessageBox::question(this,
+        tr("Varmistus"),
+        tr("\nHaluatko varmasti poistaa henkilön:\n\n<b>'%1, %2'</b> (ID #%3)?\n\n"
+        "Tämä poistaa sekä henkilön ja hänen koko tapahtumahistoriansa PYSYVÄSTI!\n").
+        arg(info.lastName).arg(info.firstName).arg(info.id),
+        QMessageBox::Yes | QMessageBox::No);
+
+    if (ret == QMessageBox::Yes)
+    {
+        int rv = deletePerson(info.id);
+        if (rv != 0)
+        {
+            appError(tr("SQL-tietokantavirhe"),
+                tr("Henkilön tietoja poistettaessa tapahtui virhe #%1.").
+                arg(rv));
+        }
+        else
+        {
+            statusMsg(tr("Henkilö '%1 %2' (ID #%3) poistettu.").
+                arg(info.firstName).arg(info.lastName).
+                arg(info.id));
+        }
+    }
+}
+
+
 void SyntilistaMainWindow::on_button_AddPerson_clicked()
 {
     EditPerson *person = new EditPerson(this);
--- a/main.h	Thu Mar 30 13:50:42 2017 +0300
+++ b/main.h	Thu Mar 30 15:12:18 2017 +0300
@@ -109,6 +109,7 @@
     int  addTransaction(qint64 id, double value, PersonInfo &info);
     int  addTransactionGUI(qint64 id, bool debt, double value);
     void updatePersonList();
+    int  deletePerson(qint64 id);
 
     PersonSQLModel *model_People;
 
@@ -139,6 +140,8 @@
     
     void updateSortOrder(int index, Qt::SortOrder order);
 
+    void on_button_DeletePerson_clicked();
+
 private:
     Ui::SyntilistaMainWindow *ui;
 
--- a/mainwindow.ui	Thu Mar 30 13:50:42 2017 +0300
+++ b/mainwindow.ui	Thu Mar 30 15:12:18 2017 +0300
@@ -60,6 +60,13 @@
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout">
          <item>
+          <widget class="QPushButton" name="button_DeletePerson">
+           <property name="text">
+            <string>Poista henkilö</string>
+           </property>
+          </widget>
+         </item>
+         <item>
           <spacer name="horizontalSpacer">
            <property name="orientation">
             <enum>Qt::Horizontal</enum>