-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocaldatabase.h
92 lines (72 loc) · 2.08 KB
/
localdatabase.h
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
#ifndef LOCALDATABASE_H
#define LOCALDATABASE_H
#include <QTemporaryFile>
#include <QtSql>
#include <QJsonValue>
#include <QJsonObject>
struct Account {
long id;
QString email;
QString username;
QString password;
long createdAt;
QJsonObject toJson() {
QJsonObject json;
json.insert("id", (int) id);
json.insert("email", email);
json.insert("username", username);
json.insert("createdAt", (qint64) createdAt);
return json;
}
};
struct Box {
long id;
QString colorName;
QString colorValue;
bool isActive;
QJsonObject toJson() {
QJsonObject json;
json.insert("id", (int) id);
json.insert("colorName", colorName);
json.insert("colorValue", colorValue);
json.insert("isActive", isActive);
return json;
}
static Box fromRecord(QSqlQuery& query) {
Box box;
box.id = query.value("box_id").toInt();
box.colorName = query.value("color_name").toString();
box.colorValue = query.value("color_value").toString();
box.isActive = query.value("is_active").toBool();
return box;
}
};
class LocalDatabase
{
private:
QTemporaryFile *databaseFile;
QSqlDatabase sqlDatabase = QSqlDatabase::addDatabase("QSQLITE");
bool _isReady;
bool isAccountExists(QString email);
void prepareAccountsModel();
void prepareBoxesModel();
void prepareSettingsModel();
void prepareSettingsViewModel();
void throwInternalError();
public:
LocalDatabase();
~LocalDatabase();
bool isReady();
QSqlQueryModel accountsModel;
QSqlQueryModel boxesModel;
QSqlQueryModel settingsModel;
QSqlQueryModel settingsViewModel;
Account getAccount(long id);
Account getAccountByEmail(QString email);
void signUp(Account& account);
void updateUsername(long id, QString newUsername);
QList<Box> getBoxes(long accountId, bool* isActive = nullptr);
Box getBox(long accountId, long boxId);
void setActiveFlag(long userId, long boxId, bool isActive);
};
#endif // LOCALDATABASE_H