-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactioncontroller.cpp
123 lines (96 loc) · 2.67 KB
/
transactioncontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "transactioncontroller.h"
#include "dbgateway.h"
TransactionController::TransactionController()
{
}
int TransactionController::ParseAccountId(QString account)
{
DbGateway db;
QSqlQuery qry;
int accountId;
if(!db.Connect()) {
qDebug() << "Failed to open the database connection @ ParseAccountId";
return -1;
}
qry.prepare("SELECT _id FROM account WHERE account_name = ?");
qry.bindValue(0, account);
if(!qry.exec())
{
qDebug() << "Account id parsing Went Wrong...";
return -1;
}
while (qry.next())
{
accountId = qry.value(0).toInt();
}
db.Disconnect();
return accountId;
}
int TransactionController::ParseCategoryId(QString category)
{
DbGateway db;
QSqlQuery qry;
int categoryId;
if(!db.Connect()) {
qDebug() << "Failed to open the database connection @ ParseCategoryId";
return -1;
}
qry.prepare("SELECT _id FROM category WHERE type = ?");
qry.bindValue(0, category);
if(!qry.exec())
{
qDebug() << "Category id parsing Went Wrong...";
return -1;
}
while (qry.next())
{
categoryId = qry.value(0).toInt();
}
db.Disconnect();
return categoryId;
}
void TransactionController::CreateTransaction(Transaction transaction)
{
DbGateway db;
QSqlQuery qry;
int accountId = transaction.getAccountId();
int category = transaction.getCategory();
QString type = transaction.getType();
QString amount = transaction.getAmount();
QDate date = transaction.getDate();
if(!db.Connect()) {
qDebug() << "Failed to open the database connection @ CreateTransaction";
return;
}
qry.prepare("INSERT INTO record (account_id, record_type, category, amount, date) VALUES (?, ?, ?, ?, ?)");
qry.bindValue(0, accountId);
qry.bindValue(1, type);
qry.bindValue(2, category);
qry.bindValue(3, amount);
qry.bindValue(4, date);
if(!qry.exec())
{
qDebug() << "Transaction Creation Went Wrong...";
return;
}
qDebug() << "Transaction Saved..";
db.Disconnect();
}
void TransactionController::DeleteTransactionByAccount(int accountId)
{
DbGateway db;
QSqlQuery qry;
if(!db.Connect()) {
qDebug() << "Failed to open the database connection @ DeleteTransactionByAccount";
return;
}
qry.prepare("DELETE FROM record WHERE account_id = ?");
qry.bindValue(0, accountId);
if(!qry.exec())
{
qDebug() << "Transaction of " << accountId << " deletion Went Wrong...";
return;
}
qDebug() << "Transactions of " << accountId << " has been deleted";
db.Disconnect();
}