Repository navigation
Expand file tree
/
Copy pathPostsLikes.cpp
More file actions
452 lines (362 loc) · 11.7 KB
/
Copy pathPostsLikes.cpp
File metadata and controls
452 lines (362 loc) · 11.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include "PostsLikes.h"
PostsLikes::PostsLikes(std::string path) {
PostsLikes::path = path;
PostsLikes::readData();
}
void PostsLikes::readData() {
std::string filename = "postsLikes.bin";
std::ifstream postsLikesFileRead(PostsLikes::path + filename, std::ios::binary);
if (postsLikesFileRead.is_open()) {
// If file already exists read data in vector
PostsLikes::postsLikes.clear();
PostLike postLike;
while (postsLikesFileRead.peek() != EOF) {
postsLikesFileRead.read((char*)&postLike.id, sizeof(postLike.id));
postsLikesFileRead.read((char*)&postLike.postId, sizeof(postLike.postId));
postsLikesFileRead.read((char*)&postLike.userId, sizeof(postLike.userId));
PostsLikes::postsLikes.push_back(postLike);
}
if (!PostsLikes::postsLikes.empty()) {
PostsLikes::lastId = PostsLikes::postsLikes.back().id;
}
}
else {
// Else create a file
std::ofstream postsLikesFileWrite(PostsLikes::path + filename, std::ios::binary);
if (!postsLikesFileWrite.is_open()) {
throw std::runtime_error("Error creating " + filename + "!");
}
postsLikesFileWrite.close();
}
}
std::string PostsLikes::save() {
std::string filename = "postsLikes.bin";
std::ofstream postsFile(PostsLikes::path + filename, std::ios::binary);
if (!postsFile.is_open()) {
return colored("Error opening file " + filename + "!", "red");
}
for (PostLike postLike : PostsLikes::postsLikes) {
postsFile.write((char*)&postLike.id, sizeof(postLike.id));
postsFile.write((char*)&postLike.postId, sizeof(postLike.postId));
postsFile.write((char*)&postLike.userId, sizeof(postLike.userId));
}
postsFile.close();
return colored("Posts likes were saved successfully.", "green");
}
int PostsLikes::getId() {
std::string id;
std::regex pattern("[0-9]+");
while (true) {
std::cout << "Enter " << colored("id", "blue") << ": ";
std::getline(std::cin, id);
if (!std::regex_match(id, pattern)) {
std::cout << colored("ID contains only numbers.", "red") << '\n';
}
else if (id.length() >= 10) {
std::cout << colored("ID cannot exceed 999999999.", "red") << '\n';
}
else {
break;
}
}
return stoi(id);
}
int PostsLikes::getPostId() {
Posts posts(PostsLikes::path);
std::cout << posts.readAllById();
std::string id;
std::regex pattern("[0-9]+");
while (true) {
std::cout << "Enter " << colored("post id", "blue") << ": ";
std::getline(std::cin, id);
if (!std::regex_match(id, pattern)) {
std::cout << colored("ID contains only numbers.", "red") << '\n';
}
else if (id.length() >= 10) {
std::cout << colored("ID cannot exceed 999999999.", "red") << '\n';
}
else if (!posts.idExists(stoi(id))) {
std::cout << colored("Post with id " + id + " does not exists.", "red") << '\n';
}
else {
break;
}
}
return stoi(id);
}
int PostsLikes::getUserId() {
Users users(PostsLikes::path);
std::cout << users.readAllById();
std::string id;
std::regex pattern("[0-9]+");
while (true) {
std::cout << "Enter " << colored("user id", "blue") << ": ";
std::getline(std::cin, id);
if (!std::regex_match(id, pattern)) {
std::cout << colored("ID contains only numbers.", "red") << '\n';
}
else if (id.length() >= 10) {
std::cout << colored("ID cannot exceed 999999999.", "red") << '\n';
}
else if (!users.idExists(stoi(id))) {
std::cout << colored("User with id " + id + " does not exists.", "red") << '\n';
}
else {
break;
}
}
return stoi(id);
}
int PostsLikes::getLikesCount(int postId) {
int count = 0;
for (PostLike postLike : PostsLikes::postsLikes) {
if (postLike.postId == postId) {
++count;
}
}
return count;
}
bool PostsLikes::liked(int postId, int userId) {
for (PostLike postLike : PostsLikes::postsLikes) {
if (postLike.postId == postId && postLike.userId == userId) {
return true;
}
}
return false;
}
// Create
std::string PostsLikes::create() {
int postId = PostsLikes::getPostId();
int userId = PostsLikes::getUserId();
if (PostsLikes::liked(postId, userId)) {
return colored("This user already liked this post.", "red");
}
PostsLikes::lastId++;
PostLike newPostLike;
newPostLike.id = PostsLikes::lastId;
newPostLike.postId = postId;
newPostLike.userId = userId;
PostsLikes::postsLikes.push_back(newPostLike);
PostsLikes::save();
return colored("Post like with id ", "green") + colored(std::to_string(newPostLike.id), "green") + colored(" has been created.", "green");
}
// Read One
std::string PostsLikes::readOneById() {
Users users(PostsLikes::path);
Posts posts(PostsLikes::path);
int id = PostsLikes::getId();
for (PostLike postLike : PostsLikes::postsLikes) {
if (postLike.id == id) {
fort::char_table table;
table << fort::header
<< "ID" << "Post Id" << "Title" << "User Id" << "Username" << fort::endr
<< postLike.id << postLike.postId << posts.getTitleById(postLike.postId) << postLike.userId << users.getUsernameById(postLike.userId) << fort::endr << fort::separator;
table.row(0).set_cell_bg_color(fort::color::light_red);
return table.to_string();
}
}
return colored("Post does not exist.", "red");
}
// Read All
std::string PostsLikes::readAllById() {
Users users(PostsLikes::path);
Posts posts(PostsLikes::path);
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Post ID" << "Title" << "User Id" << "Username" << fort::endr;
// Set Content
for (PostLike postLike : PostsLikes::postsLikes) {
table << postLike.id << postLike.postId << posts.getTitleById(postLike.postId) << postLike.userId << users.getUsernameById(postLike.userId) << fort::endr << fort::separator;
}
table.row(0).set_cell_bg_color(fort::color::light_red);
return table.to_string();
}
std::string PostsLikes::readAllByPostId() {
Users users(PostsLikes::path);
Posts posts(PostsLikes::path);
std::vector<PostLike> copyPostsLikes = PostsLikes::postsLikes;
// Sort
std::sort(copyPostsLikes.begin(), copyPostsLikes.end(), [](PostLike a, PostLike b) {
return a.postId < b.postId;
});
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Post ID" << "Title" << "User Id" << "Username" << fort::endr;
// Set Content
for (PostLike postLike : copyPostsLikes) {
table << postLike.id << postLike.postId << posts.getTitleById(postLike.postId) << postLike.userId << users.getUsernameById(postLike.userId) << fort::endr;
}
table.row(0).set_cell_bg_color(fort::color::light_red);
return table.to_string();
}
std::string PostsLikes::readAllByUserId() {
Users users(PostsLikes::path);
Posts posts(PostsLikes::path);
std::vector<PostLike> copyPostsLikes = PostsLikes::postsLikes;
// Sort
std::sort(copyPostsLikes.begin(), copyPostsLikes.end(), [](PostLike a, PostLike b) {
return a.userId < b.userId;
});
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Post ID" << "Title" << "User Id" << "Username" << fort::endr;
// Set Content
for (PostLike postLike : copyPostsLikes) {
table << postLike.id << postLike.postId << posts.getTitleById(postLike.postId) << postLike.userId << users.getUsernameById(postLike.userId) << fort::endr;
}
table.row(0).set_cell_bg_color(fort::color::light_red);
return table.to_string();
}
// Filter
std::string PostsLikes::filterByPostId() {
Users users(PostsLikes::path);
Posts posts(PostsLikes::path);
int postId = getPostId();
std::vector<PostLike> filteredPostsLikes;
for (PostLike postLike : PostsLikes::postsLikes) {
if (postLike.postId == postId) {
filteredPostsLikes.push_back(postLike);
}
}
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Post ID" << "Title" << "User Id" << "Username" << fort::endr;
// Set Content
for (PostLike postLike : filteredPostsLikes) {
table << postLike.id << postLike.postId << posts.getTitleById(postLike.postId) << postLike.userId << users.getUsernameById(postLike.userId) << fort::endr;
}
table.row(0).set_cell_bg_color(fort::color::light_red);
return table.to_string();
}
std::string PostsLikes::filterByUserId() {
Users users(PostsLikes::path);
Posts posts(PostsLikes::path);
int userId = getUserId();
std::vector<PostLike> filteredPostsLikes;
for (PostLike postLike : PostsLikes::postsLikes) {
if (postLike.userId == userId) {
filteredPostsLikes.push_back(postLike);
}
}
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Post ID" << "Title" << "User Id" << "Username" << fort::endr;
// Set Content
for (PostLike postLike : filteredPostsLikes) {
table << postLike.id << postLike.postId << posts.getTitleById(postLike.postId) << postLike.userId << users.getUsernameById(postLike.userId) << fort::endr;
}
table.row(0).set_cell_bg_color(fort::color::light_red);
return table.to_string();
}
// Delete One
std::string PostsLikes::deleteOneById() {
std::cout << PostsLikes::readAllById();
int id = PostsLikes::getId();
for (auto it = PostsLikes::postsLikes.begin(); it != PostsLikes::postsLikes.end(); ++it) {
if (it->id == id) {
PostsLikes::postsLikes.erase(it);
PostsLikes::save();
return colored("Post like with id " + std::to_string(id) + " has been deleted.", "green");
}
}
return colored("Post like does not exist.", "red");
}
// Delete All
std::string PostsLikes::deleteAllByPostId() {
std::cout << PostsLikes::readAllByPostId();
int postId = PostsLikes::getPostId();
std::string message = "";
std::vector<PostLike>::iterator it = PostsLikes::postsLikes.begin();
while (it != PostsLikes::postsLikes.end()) {
if (it->postId == postId) {
int id = it->id;
it = PostsLikes::postsLikes.erase(it);
message += colored("Post like " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This post has no likes", "red");
}
PostsLikes::save();
return message;
}
std::string PostsLikes::deleteAllByPostId(int postId) {
std::string message = "";
std::vector<PostLike>::iterator it = PostsLikes::postsLikes.begin();
while (it != PostsLikes::postsLikes.end()) {
if (it->postId == postId) {
int id = it->id;
it = PostsLikes::postsLikes.erase(it);
message += colored("Post like " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This post has no likes.\n", "red");
}
PostsLikes::save();
return message;
}
std::string PostsLikes::deleteAllByUserId() {
std::cout << PostsLikes::readAllByUserId();
int userId = PostsLikes::getUserId();
std::string message = "";
std::vector<PostLike>::iterator it = PostsLikes::postsLikes.begin();
while (it != PostsLikes::postsLikes.end()) {
if (it->userId == userId) {
int id = it->id;
it = PostsLikes::postsLikes.erase(it);
message += colored("Post like " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This user hasn't liked anyone.", "red");
}
PostsLikes::save();
return message;
}
std::string PostsLikes::deleteAllByUserId(int userId) {
std::string message = "";
std::vector<PostLike>::iterator it = PostsLikes::postsLikes.begin();
while (it != PostsLikes::postsLikes.end()) {
if (it->userId == userId) {
int id = it->id;
it = PostsLikes::postsLikes.erase(it);
message += colored("Post like " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This user hasn't liked anyone.\n", "red");
}
PostsLikes::save();
return message;
}
std::string PostsLikes::getNumberOfPostsLikes() {
std::string output = "There is " + colored(std::to_string(PostsLikes::postsLikes.size()), "blue");
if (PostsLikes::postsLikes.size() == 1) {
output += " post like.";
} else {
output += " posts likes.";
}
return output;
}