-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
191 lines (150 loc) · 5.35 KB
/
database.cpp
File metadata and controls
191 lines (150 loc) · 5.35 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "database.h"
#include <QFile>
#include <QJsonDocument>
DataBase * DataBase::p_instance = nullptr;
DataBaseDestroyer DataBase::destroyer;
DataBaseDestroyer::~DataBaseDestroyer() {
delete p_instance;
}
void DataBaseDestroyer::initialize( DataBase* p ) {
p_instance = p;
}
DataBase& DataBase::getInstance() {
if(!p_instance) {
p_instance = new DataBase();
destroyer.initialize( p_instance);
}
return *p_instance;
}
Bouquet* DataBase::getBouquetFromDb(QString fileName, int id){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return nullptr;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
for(const auto& i: t.keys()){
if (i == id){
Bouquet *bouquet = new Bouquet(t.value(i).toObject());
return bouquet;
}
}
return nullptr;
}
Flour* DataBase::getFlourFromDb(QString fileName, int id){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return nullptr;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
for(const auto& i: t.keys()){
if (i == QString::number(id)){
QJsonObject c = t.value(i).toObject();
Flour *flour = new Flour(t.value(i).toObject());
return flour;
}
}
return nullptr;
}
QList<int> DataBase::getBouquetsIds(QString fileName){
QList<int> temp;
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return temp;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
for(const auto& i: t.keys()){
temp.append(i.toInt());
}
return temp;
}
QList<int> DataBase::getFloursIds(QString fileName){
QList<int> temp;
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return temp;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
for(const auto& i: t.keys()){
temp.append(i.toInt());
}
return temp;
}
void DataBase::addBouquetToFile(Bouquet newBouqet, QString fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
if (t.keys().isEmpty()) t.insert(QString("0"), newBouqet.getJsonInfo());
else t.insert(QString::number(t.keys().last().toInt()+1), newBouqet
.getJsonInfo());
file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
file.close();
}
void DataBase::addFlourToFile(Flour newFlour, QString fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
if (t.keys().isEmpty()) t.insert(QString("0"), newFlour.getJsonInfo());
else t.insert(QString::number(t.keys().last().toInt()+1), newFlour
.getJsonInfo());
file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
file.close();
}
void DataBase::editFlourInDb(Flour oldFLour, Flour newFLour, QString fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
for(const auto& i: t.keys()){
if (t.value(i).toObject() == oldFLour.getJsonInfo()){
t.remove(i);
t.insert(i, newFLour.getJsonInfo());
}
}
file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
file.close();
}
void DataBase::editBouquetInDb(Bouquet oldBouquet, Bouquet newBouquet,
QString fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
for(const auto& i: t.keys()){
if (t.value(i).toObject() == oldBouquet.getJsonInfo()){
t.remove(i);
t.insert(i, newBouquet.getJsonInfo());
}
}
file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
file.close();
}
void DataBase::deleteFlourFromDb(Flour oddFlour, QString fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
for(const auto& i: t.keys()){
if (t.value(i).toObject() == oddFlour.getJsonInfo()){
t.remove(i);
}
}
file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
file.close();
}
void DataBase::deleteBouquetFromDb(Bouquet oddBouquet, QString fileName){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)) return;
QJsonObject t = QJsonDocument::fromJson(file.readAll()).object();
file.close();
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return;
for(const auto& i: t.keys()){
if (t.value(i).toObject() == oddBouquet.getJsonInfo()){
t.remove(i);
}
}
file.write(QJsonDocument(t).toJson(QJsonDocument::Indented));
file.close();
}