-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocaldatabase.cpp
157 lines (136 loc) · 5.55 KB
/
localdatabase.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <QTemporaryFile>
#include "localdatabase.h"
#include <QtSql>
#include <QtDebug>
#include "custom_error.h"
LocalDatabase::LocalDatabase() {
databaseFile = QTemporaryFile::createNativeFile(":/database/initial_database.db");
sqlDatabase.setDatabaseName(databaseFile->fileName());
bool isOpened = sqlDatabase.open();
if (!isOpened) return;
prepareAccountsModel();
prepareBoxesModel();
prepareSettingsModel();
prepareSettingsViewModel();
_isReady = true;
}
void LocalDatabase::prepareAccountsModel() {
accountsModel.setQuery(QSqlQuery("SELECT * FROM accounts", sqlDatabase));
}
void LocalDatabase::prepareBoxesModel() {
boxesModel.setQuery(QSqlQuery("SELECT * FROM boxes", sqlDatabase));
}
void LocalDatabase::prepareSettingsModel() {
settingsModel.setQuery(QSqlQuery("SELECT * FROM accounts_boxes_settings", sqlDatabase));
}
void LocalDatabase::prepareSettingsViewModel() {
settingsViewModel.setQuery(QSqlQuery("SELECT email, username, box_id, color_name, color_value, is_active FROM boxes_and_settings_view", sqlDatabase));
}
LocalDatabase::~LocalDatabase() {
sqlDatabase.close();
delete databaseFile;
}
Account createAccount(QSqlQuery& query) {
Account account;
account.id = query.value("id").toInt();
account.email = query.value("email").toString();
account.username = query.value("username").toString();
account.password = query.value("password").toString();
account.createdAt = query.value("created_at").toLongLong();
return account;
}
bool LocalDatabase::isAccountExists(QString email) {
try {
getAccountByEmail(email);
return true;
} catch (...) {
return false;
}
}
bool LocalDatabase::isReady() {
return _isReady;
}
Account LocalDatabase::getAccount(long id) {
QSqlQuery query(sqlDatabase);
query.prepare("SELECT * FROM accounts WHERE id = :id");
query.bindValue(":id", QVariant::fromValue(id));
if (!query.exec()) throwInternalError();
if (query.next()) {
return createAccount(query);
}
throw HttpError(404, "Not Found", "Account doesn't exist");
}
void LocalDatabase::signUp(Account& account) {
if (account.email.isEmpty()) throw HttpError(400, "Bad Request", "Email is empty");
if (account.username.isEmpty()) throw HttpError(400, "Bad Request", "Username is empty");
if (account.password.isEmpty()) throw HttpError(400, "Bad Request", "Password is empty");
if (isAccountExists(account.email)) throw HttpError(409, "Conflict", "User with such email already exists");
QSqlQuery query(sqlDatabase);
query.prepare("INSERT INTO accounts (email, username, password, created_at) VALUES (:email, :username, :password, :created_at)");
query.bindValue(":email", account.email);
query.bindValue(":username", account.username);
query.bindValue(":password", account.password);
query.bindValue(":created_at", QVariant::fromValue(account.createdAt));
if (!query.exec()) throwInternalError();
prepareAccountsModel();
prepareSettingsViewModel();
}
Account LocalDatabase::getAccountByEmail(QString email) {
QSqlQuery query(sqlDatabase);
query.prepare("SELECT * FROM accounts WHERE email = :email");
query.bindValue(":email", email);
if (!query.exec()) throwInternalError();
if (query.next()) {
return createAccount(query);
}
throw HttpError(401, "Unathorized", "Invalid email or password");
}
void LocalDatabase::updateUsername(long id, QString newUsername) {
QSqlQuery query(sqlDatabase);
query.prepare("UPDATE accounts SET username = :username WHERE id = :id");
query.bindValue(":username", newUsername);
query.bindValue(":id", QVariant::fromValue(id));
if (!query.exec()) throwInternalError();
prepareAccountsModel();
prepareSettingsViewModel();
}
QList<Box> LocalDatabase::getBoxes(long accountId, bool* isActive) {
QSqlQuery query(sqlDatabase);
if (isActive == nullptr) {
query.prepare("SELECT * FROM boxes_and_settings_view WHERE account_id = :accountId");
} else {
query.prepare("SELECT * FROM boxes_and_settings_view WHERE account_id = :accountId AND is_active = :isActive");
query.bindValue(":isActive", QVariant::fromValue(*isActive ? 1 : 0));
}
query.bindValue(":accountId", QVariant::fromValue(accountId));
if (!query.exec()) throwInternalError();
QList<Box> boxes;
while (query.next()) {
boxes.append(Box::fromRecord(query));
}
return boxes;
}
Box LocalDatabase::getBox(long accountId, long boxId) {
QSqlQuery query(sqlDatabase);
query.prepare("SELECT * FROM boxes_and_settings_view WHERE account_id = :accountId AND box_id = :boxId");
query.bindValue(":accountId", QVariant::fromValue(accountId));
query.bindValue(":boxId", QVariant::fromValue(boxId));
if (!query.exec()) throwInternalError();
if (query.next()) {
return Box::fromRecord(query);
}
throw HttpError(404, "Not Found", "There is no box with such ID");
}
void LocalDatabase::setActiveFlag(long userId, long boxId, bool isActive) {
QSqlQuery query(sqlDatabase);
query.prepare("INSERT OR REPLACE INTO accounts_boxes_settings VALUES(:userId, :boxId, :isActive)");
query.bindValue(":userId", QVariant::fromValue(userId));
query.bindValue(":boxId", QVariant::fromValue(boxId));
query.bindValue(":isActive", QVariant::fromValue(isActive));
if (!query.exec()) throwInternalError();
prepareSettingsModel();
prepareSettingsViewModel();
}
void LocalDatabase::throwInternalError() {
throw HttpError(500, "Internal Server Error", "Failed to executed the operation");
}