-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticlemodel.cpp
288 lines (251 loc) · 11.1 KB
/
articlemodel.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "headers/articlemodel.h"
#include "QDate"
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
ArticleModel::ArticleModel(QObject *parent)
{
}
// necessary for qml QAbstractListModel:
int ArticleModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid())
return 0;
return articles.size();
}
// necessary for qml QAbstractListModel:
QVariant ArticleModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
const Article article = articles.at(index.row());
switch(role) {
case TitleRole:
return QVariant(article.getTitle());
case ContentRole:
return QVariant(article.getContent());
case ModificationDateTimeRole:
return QVariant(article.getModificationDateTime());
}
return QVariant();
}
bool ArticleModel::getArticles(QString email)
{
QFile file(QStringLiteral("./data.json"));
if (!file.open(QIODeviceBase::ReadOnly)) {
qWarning("getArticles(): Couldn't open data file.");
return false;
}
QByteArray data = file.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(data));
QJsonArray blogs = loadDoc["blogs"].toArray();
for (int i = 0; i < blogs.size(); i++) {
QJsonObject blog = blogs[i].toObject();
if (blog.contains("owner") && blog["owner"].isString()
&& blog["owner"].toString() == email) {
if (blog.contains("articles") && blog["articles"].isArray()) {
QJsonArray jsonArticles = blog["articles"].toArray();
this->articles.clear();
for (int i = 0; i < jsonArticles.size(); i++) {
QJsonObject jsonArticle = jsonArticles[i].toObject();
this->addArticle(Article(jsonArticle["title"].toString(), jsonArticle["content"].toString(),
QDateTime::fromString(jsonArticle["modificationDateTime"].toString())));
}
file.close();
return true;
} else {
QTextStream(stdout) << "getArticles(): User does not have any articles" << "\n";
this->setArticles(QVector<Article>());
file.close();
return true;
}
}
}
file.close();
return false;
}
bool ArticleModel::putArticle(QString email, QString title, QString content)
{
qWarning("putArticle() invoked");
QFile file(QStringLiteral("./data.json"));
if (!file.open(QIODeviceBase::ReadWrite)) {
qWarning("putArticle(): Couldn't open data file.");
return false;
}
QByteArray data = file.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(data));
QJsonArray blogs = loadDoc["blogs"].toArray();
for (int i = 0; i < blogs.size(); i++) {
QJsonObject blog = blogs[i].toObject();
if (blog.contains("owner") && blog["owner"].isString()
&& blog["owner"].toString() == email) {
if (blog.contains("articles") && blog["articles"].isArray()) {
QJsonArray jsonArticles = blog["articles"].toArray();
Article newArticle(title, content);
QJsonObject newJsonArticle {
{"title", newArticle.getTitle()},
{"content", newArticle.getContent()},
{"modificationDateTime", newArticle.getModificationDateTime().toString()}
};
if (this->editMode) {
QTextStream(stdout) << "putArticle(): Edit mode" << "\n";
for (int j = 0; j < jsonArticles.size(); j++) {
QJsonObject currentJsonArticle = jsonArticles[j].toObject();
if (currentJsonArticle["title"].toString() == this->titleOfArticleToEdit) {
jsonArticles.replace(j, newJsonArticle);
QJsonObject newBlog {
{"blogName", blog["name"].toString()},
{"owner", blog["owner"].toString()},
{"articles", jsonArticles}
};
blogs.replace(i, newBlog);
QJsonObject docObject = loadDoc.object();
docObject.insert("blogs", blogs);
file.resize(0);
file.write(QJsonDocument(docObject).toJson());
file.close();
this->replaceArticle(newArticle, this->titleOfArticleToEdit);
this->disableEditMode();
file.close();
return true;
}
}
} else {
jsonArticles.append(newJsonArticle);
QJsonObject newBlog {
{"blogName", blog["name"].toString()},
{"owner", blog["owner"].toString()},
{"articles", jsonArticles}
};
blogs.replace(i, newBlog);
QJsonObject docObject = loadDoc.object();
docObject.insert("blogs", blogs);
file.resize(0);
file.write(QJsonDocument(docObject).toJson());
file.close();
this->addArticle(newArticle);
file.close();
return true;
}
} else {
QTextStream(stdout) << "getArticles(): User does not have any articles" << "\n";
this->setArticles(QVector<Article>());
file.close();
return true;
}
}
}
return false;
}
bool ArticleModel::deleteArticle(QString email)
{
qWarning("deleteArticle() invoked");
QFile file(QStringLiteral("./data.json"));
if (!file.open(QIODeviceBase::ReadWrite)) {
qWarning("deleteArticle(): Couldn't open data file.");
return false;
}
QByteArray data = file.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(data));
QJsonArray blogs = loadDoc["blogs"].toArray();
for (int i = 0; i < blogs.size(); i++) {
QJsonObject blog = blogs[i].toObject();
if (blog.contains("owner") && blog["owner"].isString()
&& blog["owner"].toString() == email) {
if (blog.contains("articles") && blog["articles"].isArray()) {
QJsonArray jsonArticles = blog["articles"].toArray();
for (int j = 0; j < jsonArticles.size(); j++) {
QJsonObject currentJsonArticle = jsonArticles[j].toObject();
QTextStream(stdout) << "currentJsonArticle title" << currentJsonArticle["title"].toString() << "\n";
QTextStream(stdout) << "this->modificationDateTimeOfArticleToEdit():" << this->titleOfArticleToEdit << "\n";
if (currentJsonArticle["title"].toString() == this->titleOfArticleToEdit) {
jsonArticles.removeAt(j);
QJsonObject newBlog {
{"blogName", blog["name"].toString()},
{"owner", blog["owner"].toString()},
{"articles", jsonArticles}
};
blogs.replace(i, newBlog);
// blogs.insert(i, newBlog);
QJsonObject docObject = loadDoc.object();
docObject.insert("blogs", blogs);
file.resize(0);
file.write(QJsonDocument(docObject).toJson());
file.close();
this->deleteArticleLocal(this->titleOfArticleToEdit);
this->disableEditMode();
file.close();
return true;
}
}
} else {
QTextStream(stdout) << "getArticles(): User does not have any articles" << "\n";
this->setArticles(QVector<Article>());
file.close();
return true;
}
}
}
return false;
}
void ArticleModel::enableEditMode(QString modificationDateTimeOfArticleToEdit, QString titleOfArticleToEdit)
{
this->editMode = true;
this->modificationDateTimeOfArticleToEdit = modificationDateTimeOfArticleToEdit;
this->titleOfArticleToEdit = titleOfArticleToEdit;
}
void ArticleModel::disableEditMode()
{
this->editMode = false;
}
// necessary for qml QAbstractListModel:
QHash<int, QByteArray> ArticleModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TitleRole] = "title";
roles[ContentRole] = "content";
roles[ModificationDateTimeRole] = "modificationDateTime";
return roles;
}
void ArticleModel::setArticles(QVector<Article> articles)
{
this->articles = articles;
}
void ArticleModel::addArticle(Article article)
{
QTextStream(stdout) << "addArticle() title: " << article.getTitle() << "\n";
beginInsertRows({}, articles.size(), articles.size());
this->articles.append(article);
endInsertRows();
}
void ArticleModel::replaceArticle(Article newArticle, QString title)
{
for (int i = 0; i < this->articles.size(); i++) {
if (this->articles.at(i).getTitle() == title) {
this->articles.replace(i, newArticle);
}
}
}
void ArticleModel::deleteArticleLocal(QString title)
{
for (int i = 0; i < this->articles.size(); i++) {
if (this->articles.at(i).getTitle() == title) {
this->articles.remove(i);
}
}
}
//void ArticleModel::putExampleArticles()
//{
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
// articles.push_back(Article("New EU law could require iMessage and WhatsApp to work with other, smaller platforms", "Lorem ipsum lso"));
//}