-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocialNetwork.cpp
306 lines (302 loc) · 10.5 KB
/
SocialNetwork.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "SocialNetwork.h"
using namespace Commands;
using namespace Constants;
void SocialNetwork::run() {
std::cout << "|Press 1 for help|" << std::endl;
while (true) {
std::cout << ">";
char command[MAX_COMMAND_SIZE];
std::cin >> command;
std::cin.ignore();
if (strcmp(command, SIGNUP) == 0) {
signUp();
}
else if (strcmp(command, LOGIN) == 0) {
login();
}
else if (strcmp(command, EXIT) == 0) {
break;
}
else if (strcmp(command, ONE) == 0) {
printHelpInTheBeginning();
}
}
}
void SocialNetwork::printHelpInTheBeginning()const{
std::cout << "You can choose :" << std::endl;
std::cout << ">>login - log in existing profile" << std::endl;
std::cout << ">>signup - create new profile" << std::endl;
std::cout << ">>exit - exit the app" << std::endl<<std::endl;
}
void SocialNetwork::printHelpInTopic()const {
std::cout << "You can choose :" << std::endl;
std::cout << ">>list - list all the posts in the topic" << std::endl;
std::cout << ">>post - create new post" << std::endl;
std::cout << ">>p_open (name or id) - to open specific post" << std::endl;
std::cout << ">>quit - leave the topic" << std::endl << std::endl;
}
void SocialNetwork::printHelpInPost()const {
std::cout << "You can choose :" << std::endl;
std::cout << ">>comment - add new comment" << std::endl;
std::cout << ">>reply - reply specific comment" << std::endl;
std::cout << ">>comments - see all the comments in the post" << std::endl;
std::cout << ">>upvote - give upvote to specific comment" << std::endl;
std::cout << ">>downvote - give downvote to specific comment" << std::endl << std::endl;
}
void SocialNetwork::printHelpInLogin()const {
std::cout << "You can choose :" << std::endl;
std::cout << ">>search - search for topics" << std::endl;
std::cout << ">>create - create new topic" << std::endl;
std::cout << ">>comments - see all the comments in the post" << std::endl;
std::cout << ">>open(name or id) - open specific topic" << std::endl;
std::cout << ">>whoami - see your information" << std::endl;
std::cout << ">>logout - logout" << std::endl<<std::endl;
}
void SocialNetwork::successfullOpenTopic() {
while (true) {
std::cout << ">";
char buffer[MAX_COMMAND_SIZE], command[MAX_COMMAND_SIZE];
std::cin.getline(buffer, MAX_COMMAND_SIZE);
std::stringstream ss(buffer);
ss >> command;
if (strcmp(command, LIST) == 0) {
currTopic.listPost();
}
else if (strcmp(command, POST) == 0) {
std::cout << "Enter your question: ";
char buffer[MAX_COMMAND_SIZE];
std::cin.getline(buffer, MAX_COMMAND_SIZE);
if (!currTopic.isUniquePost(buffer)) {
std::cout << "This question alredy exits" << std::endl << std::endl;
continue;
}
currTopic.addPost(buffer);
std::cout << ">Success!" << std::endl <<std::endl;
}
else if (strcmp(command, P_OPEN) == 0) {
char openType[MAX_ID_SIZE];
ss.getline( openType,MAX_ID_SIZE);
if (isStringNumber(openType)) {
if (currTopic.openPost(stringToSize_t(openType))) {
std::cout << "Q: " << currTopic.getCurrPostTitle() << std::endl << std::endl;
successfullOpenPost();
}
else
std::cout << "No post found" << std::endl << std::endl;
}
else {
if (currTopic.openPost(openType)) {
std::cout << "Q: " << currTopic.getCurrPostTitle() << std::endl << std::endl;
successfullOpenPost();
}
else
std::cout << "No post found" << std::endl << std::endl;
}
}
else if (strcmp(command, QUIT) == 0) {
std::cout << ">You just left topic , " << "\"" << currTopic.getTitle() << "\"!" << std::endl << std::endl;
topicManager.updateTopic(std::move(currTopic), currTopicIndex);
break;
}
else if (strcmp(command, ONE) == 0) {
printHelpInTopic();
}
}
}
void SocialNetwork::successfullOpenPost() {
while (true) {
std::cout << ">";
char buffer[MAX_COMMAND_SIZE], command[MAX_ID_SIZE];
std::cin.getline(buffer, MAX_COMMAND_SIZE);
std::stringstream ss(buffer);
ss >> command;
if (strcmp(command, COMMENT) == 0) {
std::cout << ">Say something: ";
char buffer[MAX_COMMAND_SIZE];
std::cin.getline(buffer, MAX_COMMAND_SIZE);
currTopic.addComment(buffer);
currUser.addPoints();
std::cout << ">Posted" << std::endl << std::endl;
}
else if (strcmp(command, COMMENTS) == 0) {
currTopic.showComments();
}
else if (strcmp(command, REPLY) == 0) {
char idFromTheConsole[MAX_ID_SIZE];
ss >> idFromTheConsole;
size_t id = stringToSize_t(idFromTheConsole);
int doesIdExits=currTopic.getCurrPost().searchCommentById(id);
if (doesIdExits != -1) {
std::cout << "Say something: ";
char newBuffer[MAX_COMMAND_SIZE];
std::cin.getline(newBuffer, MAX_COMMAND_SIZE);
SharedPointer<Comment> newComment(new Comment(newBuffer));
currTopic.addReply(id, std::move(newComment));
std::cout << "Posted" << std::endl << std::endl;
currUser.addPoints();
}
else
std::cout << "There is no comment with that id" << std::endl << std::endl;
}
else if (strcmp(command, UPVOTE) == 0) {
char idFromTheConsole[MAX_ID_SIZE];
ss >> idFromTheConsole;
size_t id = stringToSize_t(idFromTheConsole);
int doesIdExits = currTopic.getCurrPost().searchCommentById(id);
if (doesIdExits != -1) {
if (!currUser.hasUserUpvotedCurrComment(id)) {
currTopic.getCurrPost().addUpvoteToComment(id);
currUser.addUpvotedCommentId(id);
}
else {
currTopic.getCurrPost().removeUpvoteFromComment(id);
currUser.removeUpvotedCommentId(id);
}
}
else
std::cout << "There is no comment with that id" << std::endl << std::endl;
}
else if (strcmp(command, DOWNVOTE) == 0) {
char idFromTheConsole[MAX_ID_SIZE];
ss >> idFromTheConsole;
size_t id = stringToSize_t(idFromTheConsole);
int doesIdExits = currTopic.getCurrPost().searchCommentById(id);
if (doesIdExits != -1) {
if (!currUser.hasUserDownvotedCurrComment(id)) {
currTopic.getCurrPost().addDownvoteToComment(id);
currUser.addDownvotedCommentId(id);
}
else {
currTopic.getCurrPost().removeDownvoteFromComment(id);
currUser.removeDownvotedCommentId(id);
}
}
else
std::cout << "There is no comment with that id" << std::endl << std::endl;
}
else if (strcmp(command, P_CLOSE) == 0) {
std::cout << "You just left , " << "\"" << currTopic.getCurrPost().getTitle() << "\"" << std::endl << std::endl;
currTopic.updatePost(std::move(currTopic.getCurrPost()), currTopic.getCurrPostIndex());
break;
}
else if (strcmp(command, ONE) == 0) {
printHelpInPost();
}
}
}
void SocialNetwork::successfullLogIn() {
while (true) {
std::cout << ">";
char buffer[MAX_COMMAND_SIZE],command[MAX_ID_SIZE];
std::cin.getline(buffer, MAX_COMMAND_SIZE);
std::stringstream ss(buffer);
ss >> command;
if (strcmp(command, SEARCH) == 0) {
char topicTitle[MAX_ID_SIZE];
ss.getline(topicTitle, MAX_ID_SIZE);
topicManager.searchTopic(topicTitle);
}
else if (strcmp(command, CREATE) == 0) {
create();
}
else if (strcmp(command, OPEN) == 0) {
char openType[MAX_COMMAND_SIZE];
ss.getline(openType, MAX_COMMAND_SIZE);
if (isStringNumber(openType)) {
open(stringToSize_t(openType));
}
else {
open(openType);
}
}
else if (strcmp(command, WHOAMI) == 0) {
std::cout << currUser.getFirstName() << " " << currUser.getLastName() << ", having " << currUser.getPoints() << " points." << std::endl << std::endl;
}
else if (strcmp(command, LOGOUT) == 0) {
std::cout << "Goodbye, " << currUser.getFirstName() << " " << currUser.getLastName() << std::endl << std::endl;
userManager.updateUser(std::move(currUser), currUserIndex);
break;
}
else if (strcmp(command, ABOUT) == 0) {
char idFromTheConsole[MAX_ID_SIZE];
ss >> idFromTheConsole;
size_t id = stringToSize_t(idFromTheConsole);
topicManager.printTopicById(id);
}
else if (strcmp(command, ONE) == 0) {
printHelpInLogin();
}
}
}
void SocialNetwork::signUp() {
MyString firstName, lastName, password;
std::cout << ">Enter your first name: ";
std::cin >> firstName;
std::cout << ">Enter last name: ";
std::cin >> lastName;
std::cout << ">Enter password: ";
std::cin >> password;
User userForCheck(firstName, lastName, password);
if (!userManager.isUniqueUser(userForCheck)) {
std::cout << "This user already exits!" << std::endl << std::endl;
return;
}
userManager.addUser(std::move(userForCheck));
std::cout << "Success!" << std::endl<<std::endl;
}
bool SocialNetwork::login(){
MyString name, password;
std::cout << "name: ";
std::cin >> name;
std::cout << "password: ";
std::cin >> password;
std::cin.ignore();
User userForCheck(name, password);
if (userManager.logInCheck(userForCheck)) {
currUserIndex = userManager.getSpecificUserIndex(userForCheck);
currUser = std::move(userManager.getSpecificUser(userForCheck));
std::cout << "Welcome back, " << currUser.getFirstName() << " " << currUser.getLastName() << "! " << std::endl << std::endl;
successfullLogIn();
return true;
}
else {
std::cout << "No user found! You can try again. " << std::endl<<std::endl;
return false;
}
}
void SocialNetwork::open(const char* topicTitle) {
if (topicManager.searchTopicForCheck(topicTitle)) {
currTopicIndex = topicManager.getTopicIndexByTitle(topicTitle);
currTopic = std::move(topicManager.openTopicByTitle(topicTitle));
std::cout << "Welcome to " << currTopic.getTitle() << std::endl << std::endl;
successfullOpenTopic();
}
else {
std::cout << "No topic found" << std::endl << std::endl;
}
}
void SocialNetwork::open(size_t id){
if (topicManager.searchTopicForCheck(id)) {
currTopic = std::move(topicManager.openTopicById(id));
currTopicIndex = topicManager.getTopicIndexById(id);
std::cout << "Welcome to " << currTopic.getTitle() << std::endl << std::endl;
successfullOpenTopic();
}
else {
std::cout << "No topic found" << std::endl << std::endl;
}
}
void SocialNetwork::create() {
MyString topicTitle, topicDesc;
std::cout << ">Enter topic title: ";
getline(std::cin, topicTitle);
std::cout << ">Enter topic description: ";
getline(std::cin,topicDesc);
if (!topicManager.isUniqueTopic(topicTitle)) {
std::cout << "Topic with this title already exists" << std::endl << std::endl;
}
else {
topicManager.addTopic(Topic(topicTitle, currUser.getFirstName(), topicDesc));
std::cout << "Success!" << std::endl << std::endl;
}
}