-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.cpp
More file actions
386 lines (342 loc) · 9.89 KB
/
Copy pathcommands.cpp
File metadata and controls
386 lines (342 loc) · 9.89 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
#include "commands.hpp"
#include <iostream>
#include <limits>
#include <string>
#include <unordered_map>
#include <cstdint>
#include <cstdlib>
using namespace std;
/* O metoda care verifica daca string-ul s este numeric (numar natural) */
bool is_number(string s)
{
return !s.empty() && std::find_if(s.begin(), s.end(), [](unsigned char c)
{ return !std::isdigit(c); }) == s.end();
}
/* O clasa care se ocupa de interpretarea informatiilor oferite de catre utilizator */
/* Cazul in care utilizatorul doreste sa se autentifice */
class Login
{
public:
string username;
string password;
friend istream &operator>>(istream &given_string, Login &data)
{
try_again_username:
if (&given_string == &cin)
cout << "username=";
getline(given_string, data.username);
size_t found_username = data.username.find(" ", 0);
/* Se verifica daca numele de utilizator contine spatii */
if (found_username != string::npos)
{
fprintf(stdout, "Type a valid username!\n");
goto try_again_username;
}
try_again_password:
if (&given_string == &cin)
cout << "password=";
getline(given_string, data.password);
/* Se verifica daca parola contine spatii */
size_t found_password = data.password.find(" ", 0);
if (found_password != string::npos)
{
fprintf(stdout, "Type a valid password!\n");
goto try_again_password;
}
return given_string;
}
};
/* O clasa care se ocupa de interpretarea informatiilor oferite de catre utilizator */
/* Cazul in care utilizatorul doreste sa posteze o carte intr-un cont */
class Book
{
public:
string genre;
string publisher;
string title;
string author;
int page_count;
Book() : page_count(0){};
friend istream &operator>>(istream &given_string, Book &data)
{
if (&given_string == &cin)
cout << "title=";
getline(given_string, data.title);
if (&given_string == &cin)
cout << "author=";
getline(given_string, data.author);
if (&given_string == &cin)
cout << "genre=";
getline(given_string, data.genre);
if (&given_string == &cin)
cout << "publisher=";
getline(given_string, data.publisher);
for (; 1;)
{
if (&given_string == &cin)
{
cout << "page_count=";
}
string str;
given_string >> str;
/* Se verifica daca numarul de pagini este un numar natural */
if (is_number(str))
{
data.page_count = stoi(str);
if (data.page_count >= 0 || given_string.eof())
{
given_string.ignore();
break;
}
}
/* Se afiseaza un mesaj corespunzator */
fprintf(stdout, "Type a valid number!\n");
given_string.clear();
given_string.ignore(numeric_limits<streamsize>::max(), '\n');
}
return given_string;
}
};
/* O clasa care se ocupa de interpretarea informatiilor oferite de catre utilizator */
/* Cazul in care utilizatorul doreste sa afle detalii despre o carte */
class Query
{
public:
int id;
Query() : id(0){};
friend istream &operator>>(istream &given_string, Query &query)
{
for (; 1;)
{
if (&given_string == &cin)
{
cout << "id=";
}
string str;
given_string >> str;
/* Se verifica daca numarul de pagini este un numar natural */
if (is_number(str))
{
query.id = stoi(str);
if (query.id >= 0 || given_string.eof())
{
given_string.ignore();
break;
}
}
/* Se afiseaza un mesaj corespunzator */
fprintf(stdout, "Type a valid number!\n");
given_string.clear();
given_string.ignore(numeric_limits<streamsize>::max(), '\n');
}
return given_string;
}
};
/* Metoda care se ocupa de comanda get_book */
void commands::lib_get_book(Client &client)
{
Query data;
cin >> data;
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.get("/api/v1/tema/library/books/" + to_string(data.id));
if (my_pair.second == 200)
{
for (auto elem : (my_pair.first.is_array() ? my_pair.first.at(0) : my_pair.first).items())
{
cout << elem.key() << ": " << elem.value() << endl;
}
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
if (client.cookie.empty())
fprintf(stdout, "And..you are not authenticated!\n");
}
else
{
fprintf(stdout, "Error from server !\n");
}
}
/* Metoda care se ocupa de comanda register */
void commands::register_client(Client &client)
{
Login data;
cin >> data;
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.post("/api/v1/tema/auth/register",
{
{"username", data.username},
{"password", data.password},
});
if (my_pair.second == 201)
{
printf("Account created.\n");
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
}
else
{
fprintf(stdout, "Error from server!\n");
}
}
/* Metoda care se ocupa de comanda login */
void commands::login(Client &client)
{
Login data;
cin >> data;
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.post("/api/v1/tema/auth/login",
{
{"username", data.username},
{"password", data.password},
});
if (my_pair.second == 200)
{
printf("Authenticated.\n");
}
else if (my_pair.second == 204)
{
fprintf(stdout, "Already authenticated.\n");
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
}
else
{
fprintf(stdout, "Error from server!\n");
}
}
/* Metoda care se ocupa de comanda get_books */
void commands::lib_get_books(Client &client)
{
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.get("/api/v1/tema/library/books");
if (my_pair.second == 200 && my_pair.first.is_array())
{
if (my_pair.first.empty())
{
printf("There are no books to show.\n");
}
for (auto book : my_pair.first)
{
cout << book["id"].get<int>() << ": " << book["title"] << endl;
}
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
if (client.cookie.empty())
fprintf(stdout, "And..you are not authenticated!\n");
}
else
{
fprintf(stdout, "Error from server!\n");
}
}
/* Metoda care se ocupa de comanda exit */
void commands::exit(Client &client)
{
(void)client;
/* Se incheie executia programului */
::exit(0);
}
/* Metoda care se ocupa de comanda logout */
void commands::logout(Client &client)
{
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.get("/api/v1/tema/auth/logout");
if (my_pair.second == 200)
{
client.clear();
printf("Deauthenticated.\n");
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
}
else
{
fprintf(stdout, "Error from server!\n");
}
}
/* Metoda care se ocupa de comanda enter_library */
void commands::lib_enter(Client &client)
{
if (client.token.empty())
{
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.get("/api/v1/tema/library/access");
if (my_pair.second == 200)
{
printf("Access granted.\n");
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
}
else
{
fprintf(stdout, "Error from server!\n");
}
}
else
fprintf(stdout, "You already have access!\n");
}
/* Metoda care se ocupa de comanda delete_book */
void commands::lib_del_book(Client &client)
{
Query data;
cin >> data;
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.del("/api/v1/tema/library/books/" + to_string(data.id));
if (my_pair.second == 200)
{
printf("Book deleted.\n");
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
}
else
{
fprintf(stdout, "Error from server!\n");
}
}
/* Metoda care se ocupa de comanda add_book */
void commands::lib_add_book(Client &client)
{
Book data;
cin >> data;
/* Se proceseaza raspunsul primit */
pair<nlohmann::json, int> my_pair =
client.post("/api/v1/tema/library/books",
{
{"title", data.title},
{"author", data.author},
{"genre", data.genre},
{"publisher", data.publisher},
{"page_count", data.page_count},
});
if (my_pair.second == 200)
{
printf("Book added.\n");
}
else if (my_pair.first.contains("error"))
{
cout << my_pair.first["error"].get<string>() << endl;
}
else
{
fprintf(stdout, "Error from server!\n");
}
}