Repository navigation
Expand file tree
/
Copy pathFollows.cpp
More file actions
433 lines (345 loc) · 11.1 KB
/
Copy pathFollows.cpp
File metadata and controls
433 lines (345 loc) · 11.1 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
#include "Follows.h"
Follows::Follows(std::string path) {
Follows::path = path;
Follows::readData();
}
void Follows::readData() {
std::string filename = "follows.bin";
std::ifstream followsFileRead(Follows::path + filename, std::ios::binary);
if (followsFileRead.is_open()) {
// If file already exists read data in vector
Follows::follows.clear();
Follow follow;
while (followsFileRead.peek() != EOF) {
followsFileRead.read((char*)&follow.id, sizeof(follow.id));
followsFileRead.read((char*)&follow.followerId, sizeof(follow.followerId));
followsFileRead.read((char*)&follow.followingId, sizeof(follow.followingId));
Follows::follows.push_back(follow);
}
if (!Follows::follows.empty()) {
Follows::lastId = Follows::follows.back().id;
}
}
else {
// Else create a file
std::ofstream followsFileWrite(Follows::path + filename, std::ios::binary);
if (!followsFileWrite.is_open()) {
throw std::runtime_error("Error creating " + filename + "!");
}
followsFileWrite.close();
}
}
std::string Follows::save() {
std::string filename = "follows.bin";
std::ofstream followsFile(Follows::path + filename, std::ios::binary);
if (!followsFile.is_open()) {
return colored("Error opening file " + filename + "!", "red");
}
for (Follow follow : Follows::follows) {
followsFile.write((char*)&follow.id, sizeof(follow.id));
followsFile.write((char*)&follow.followerId, sizeof(follow.followerId));
followsFile.write((char*)&follow.followingId, sizeof(follow.followingId));
}
followsFile.close();
return colored("Follow were saved successfully.", "green");
}
int Follows::getUserId(std::string whatToGet) {
Users users(Follows::path);
std::cout << users.readAllById();
std::string id;
std::regex pattern("[0-9]+");
while (true) {
std::cout << "Enter " << colored(whatToGet, "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 Follows::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);
}
bool Follows::followed(int followerId, int followingId) {
for (Follow follow : Follows::follows) {
if (follow.followerId == followerId && follow.followingId == followingId) {
return true;
}
}
return false;
}
int Follows::getFollowersCount(int userId) {
int count = 0;
for (Follow follow : Follows::follows) {
if (follow.followingId == userId) {
++count;
}
}
return count;
}
int Follows::getFollowingCount(int userId) {
int count = 0;
for (Follow follow : Follows::follows) {
if (follow.followerId == userId) {
++count;
}
}
return count;
}
// Create
std::string Follows::create() {
int followerId = Follows::getUserId("follower id");
int followingId = Follows::getUserId("following id");
if (Follows::followed(followerId, followingId)) {
return colored("User " + std::to_string(followerId) + " already following user " + std::to_string(followingId) + ".", "red");
}
if (followerId == followingId) {
return colored("User can't follow himself.", "red");
}
Follows::lastId++;
Follow newFollows;
newFollows.id = Follows::lastId;
newFollows.followerId = followerId;
newFollows.followingId = followingId;
Follows::follows.push_back(newFollows);
Follows::save();
return colored("Follow " + std::to_string(newFollows.id) + " has been created.", "green");
}
// Read One
std::string Follows::readOneById() {
Users users(Follows::path);
int id = Follows::getId();
for (Follow follow : Follows::follows) {
if (follow.id == id) {
fort::char_table table;
table << fort::header
<< "ID" << "Follower Id" << "Follower" << "Following Id" << "Following" << fort::endr
<< follow.id << follow.followerId << users.getUsernameById(follow.followerId) << follow.followingId << users.getUsernameById(follow.followingId) << fort::endr << fort::separator;
table.row(0).set_cell_bg_color(fort::color::yellow);
return table.to_string();
}
}
return colored("Follow does not exist.", "red");
}
// Read All
std::string Follows::readAllById() {
Users users(Follows::path);
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Follower Id" << "Follower" << "Following Id" << "Following" << fort::endr;
// Set Content
for (Follow follow : Follows::follows) {
table << follow.id << follow.followerId << users.getUsernameById(follow.followerId) << follow.followingId << users.getUsernameById(follow.followingId) << fort::endr << fort::separator;
}
table.row(0).set_cell_bg_color(fort::color::yellow);
return table.to_string();
}
std::string Follows::readAllByFollowerId() {
Users users(Follows::path);
std::vector<Follow> copyFollows = Follows::follows;
// Sort
std::sort(copyFollows.begin(), copyFollows.end(), [](Follow a, Follow b) {
return a.followerId < b.followerId;
});
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Follower Id" << "Follower" << "Following Id" << "Following" << fort::endr;
// Set Content
for (Follow follow : copyFollows) {
table << follow.id << follow.followerId << users.getUsernameById(follow.followerId) << follow.followingId << users.getUsernameById(follow.followingId) << fort::endr << fort::separator;
}
table.row(0).set_cell_bg_color(fort::color::yellow);
return table.to_string();
}
std::string Follows::readAllByFollowingId() {
Users users(Follows::path);
std::vector<Follow> copyFollows = Follows::follows;
// Sort
std::sort(copyFollows.begin(), copyFollows.end(), [](Follow a, Follow b) {
return a.followingId < b.followingId;
});
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Follower Id" << "Follower" << "Following Id" << "Following" << fort::endr;
// Set Content
for (Follow follow : copyFollows) {
table << follow.id << follow.followerId << users.getUsernameById(follow.followerId) << follow.followingId << users.getUsernameById(follow.followingId) << fort::endr << fort::separator;
}
table.row(0).set_cell_bg_color(fort::color::yellow);
return table.to_string();
}
// Filter
std::string Follows::filterByFollowerId() {
Users users(Follows::path);
int followerId = getUserId("follower id");
std::vector<Follow> filteredFollows;
for (Follow follow : Follows::follows) {
if (follow.followerId == followerId) {
filteredFollows.push_back(follow);
}
}
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Follower Id" << "Follower" << "Following Id" << "Following" << fort::endr;
// Set Content
for (Follow follow : filteredFollows) {
table << follow.id << follow.followerId << users.getUsernameById(follow.followerId) << follow.followingId << users.getUsernameById(follow.followingId) << fort::endr << fort::separator;
}
table.row(0).set_cell_bg_color(fort::color::yellow);
return table.to_string();
}
std::string Follows::filterByFollowingId() {
Users users(Follows::path);
int followingId = getUserId("following id");
std::vector<Follow> filteredFollows;
for (Follow follow : Follows::follows) {
if (follow.followingId == followingId) {
filteredFollows.push_back(follow);
}
}
// Create a table
fort::char_table table;
// Set Header
table << fort::header
<< "ID" << "Follower Id" << "Follower" << "Following Id" << "Following" << fort::endr;
// Set Content
for (Follow follow : filteredFollows) {
table << follow.id << follow.followerId << users.getUsernameById(follow.followerId) << follow.followingId << users.getUsernameById(follow.followingId) << fort::endr << fort::separator;
}
table.row(0).set_cell_bg_color(fort::color::yellow);
return table.to_string();
}
// Delete One
std::string Follows::deleteOneById() {
std::cout << Follows::readAllById();
int id = Follows::getId();
for (auto it = Follows::follows.begin(); it != Follows::follows.end(); ++it) {
if (it->id == id) {
Follows::follows.erase(it);
Follows::save();
return colored("Follow with id " + std::to_string(id) + " has been deleted.", "green");
}
}
return colored("Follow does not exist.", "red");
}
// Delete All
std::string Follows::deleteAllByFollowerId() {
std::cout << Follows::readAllByFollowerId();
int followerId = Follows::getUserId("follower id");
std::string message = "";
std::vector<Follow>::iterator it = Follows::follows.begin();
while (it != Follows::follows.end()) {
if (it->followerId == followerId) {
int id = it->id;
it = Follows::follows.erase(it);
message += colored("Follow " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This user is not following anyone.", "red");
}
Follows::save();
return message;
}
std::string Follows::deleteAllByFollowerId(int userId) {
std::string message = "";
std::vector<Follow>::iterator it = Follows::follows.begin();
while (it != Follows::follows.end()) {
if (it->followerId == userId) {
int id = it->id;
it = Follows::follows.erase(it);
message += colored("Follow " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This user is not following anyone.\n", "red");
}
Follows::save();
return message;
}
std::string Follows::deleteAllByFollowingId() {
std::cout << Follows::readAllByFollowingId();
int followingId = Follows::getUserId("following id");
std::string message = "";
std::vector<Follow>::iterator it = Follows::follows.begin();
while (it != Follows::follows.end()) {
if (it->followingId == followingId) {
int id = it->id;
it = Follows::follows.erase(it);
message += colored("Follow " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This user does not have any followers.", "red");
}
Follows::save();
return message;
}
std::string Follows::deleteAllByFollowingId(int userId) {
std::string message = "";
std::vector<Follow>::iterator it = Follows::follows.begin();
while (it != Follows::follows.end()) {
if (it->followingId == userId) {
int id = it->id;
it = Follows::follows.erase(it);
message += colored("Follow " + std::to_string(id) + " has been deleted.\n", "green");
}
else {
++it;
}
}
if (message == "") {
return colored("This user does not have any followers.\n", "red");
}
Follows::save();
return message;
}
std::string Follows::getNumberOfFollows(){
std::string output = "There is " + colored(std::to_string(Follows::follows.size()), "blue");
if (Follows::follows.size() == 1) {
output += " follow.";
} else {
output += " follows.";
}
return output;
}