-
Notifications
You must be signed in to change notification settings - Fork 3
/
Forum.js
144 lines (116 loc) · 4.32 KB
/
Forum.js
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
class Forum {
constructor() {
this._users = [];
this._questions = [];
this._id = 1;
this._loggedUsers = [];
}
register(username, password, repeatPassword, email) {
if (username === "" || password === "" ||
repeatPassword === "" || email === "") {
throw new Error("Input can not be empty");
}
if (password !== repeatPassword) {
throw new Error("Passwords do not match");
}
//Check if users contains username or email
let user = this._users.filter(u => u.username === username || u.email === email)[0]
if (!user) {
let user = {
username,
password,
email
}
this._users.push(user);
} else {
throw new Error("This user already exists!");
}
return `${username} with ${email} was registered successfully!`;
}
login(username, password){
let user = this._users.filter(u => u.username === username)[0];
if(!user){
throw new Error("There is no such user");
}
if(user.password === password){
this._loggedUsers.push(user);
return "Hello! You have logged in successfully";
}
}
logout(username, password){
let user = this._users.filter(u => u.username === username)[0];
if(!user){
throw new Error("There is no such user");
}
if(user.password === password){
let userIndex = this._users.indexOf(user);
this._loggedUsers.splice(userIndex, 1);
return "You have logged out successfully";
}
}
postQuestion(username, question){
let user = this._users.filter(u => u.username === username)[0];
//TODO: if i have time i can make it filter only the user because its the same
let userLoggedIn = this._loggedUsers.includes(user);
if(!user || !userLoggedIn){
throw new Error("You should be logged in to post questions");
}
if(question === ""){
throw new Error("Invalid question");
}
let currQuestion = {
id: this._id,
question: question,
username: user.username,
answers: []
};
this._id++;
this._questions.push(currQuestion);
return `Your question has been posted successfully`;
}
postAnswer(username, questionId, answer){
let user = this._users.filter(u => u.username === username)[0];
//TODO: if i have time i can make it filter only the user because its the same
let userLoggedIn = this._loggedUsers.includes(user);
if(!user || !userLoggedIn){
throw new Error("You should be logged in to post answers");
}
if(answer === ""){
throw new Error("Invalid answer");
}
let currentQuestion = this._questions.filter(q => q.id === questionId)[0];
if(!currentQuestion){
throw new Error("There is no such question");
}
let resultAnswer = {
username: user.username,
answer: answer
}
currentQuestion["answers"].push(resultAnswer);
return "Your answer has been posted successfully";
}
showQuestions(){
let result = "";
this._questions.forEach(x => {
result += `Question ${x.id} by ${x.username}: ${x.question}\n`;
//let resultAnswers = [];
for (const line in x.answers) {
let currAnswer = x.answers[line];
result += `---${currAnswer.username}: ${currAnswer.answer}\n`
}
})
result = result.trim();
return result;
}
}
let forum = new Forum();
forum.register('Michael', '123', '123', 'michael@abv.bg');
forum.register('Stoyan', '123ab7', '123ab7', 'some@gmail@.com');
forum.login('Michael', '123');
forum.login('Stoyan', '123ab7');
forum.postQuestion('Michael', "Can I rent a snowboard from your shop?");
forum.postAnswer('Stoyan', 1, "Yes, I have rented one last year.");
forum.postQuestion('Stoyan', "How long are supposed to be the ski for my daughter?");
forum.postAnswer('Michael',2, "How old is she?");
forum.postAnswer('Michael',2, "Tell us how tall she is.");
console.log(forum.showQuestions());