Advertisement
Guest User

Ski Forum

a guest
Oct 23rd, 2020
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. class Forum {
  2. constructor() {
  3. this._users = [];
  4. this._questions = [];
  5. this._id = 1;
  6. }
  7.  
  8. register(username, password, repeatPassword, email) {
  9. if (!username || !password || !repeatPassword || !email) {
  10. throw new Error("Input can not be empty");
  11. }
  12.  
  13. if (password !== repeatPassword) {
  14. throw new Error("Passwords do not match");
  15. }
  16.  
  17. let exists = this._users.find(us => us.username == username || us.email == email);
  18. if (exists) {
  19. throw new Error("This user already exists!");
  20. }
  21.  
  22. this._users.push({ username, password, email });
  23. return `${username} with ${email} was registered successfully!`;
  24. }
  25.  
  26. login(username, password) {
  27. let user = this._users.find(us => us.username == username);
  28.  
  29. if (!user) {
  30. throw new Error("There is no such user");
  31. }
  32.  
  33. if (user.password !== password) {
  34. return;
  35. }
  36. user.log = true;
  37. return "Hello! You have logged in successfully";
  38. }
  39.  
  40. logout(username, password) {
  41. let user = this._users.find(us => us.username == username);
  42.  
  43. if (!user) {
  44. throw new Error("There is no such user");
  45. }
  46.  
  47. if (user.password !== password) {
  48. return;
  49. }
  50. user.log = false;
  51. return "You have logged out successfully";
  52. }
  53.  
  54. postQuestion(username, question) {
  55. let user = this._users.find(us => us.username == username);
  56.  
  57. if (!user || !user.log) {
  58. throw new Error("You should be logged in to post questions");
  59. }
  60.  
  61. if (question === '') {
  62. throw new Error("Invalid question");
  63. }
  64.  
  65. this._questions.push({ id: this._id, username: user.username, question, answers: [] });
  66.  
  67. this._id++;
  68.  
  69. return "Your question has been posted successfully";
  70. }
  71.  
  72. postAnswer(username, questionId, answer) {
  73. let responder = this._users.find(us => us.username == username);
  74.  
  75. if (!responder || !responder.log) {
  76. throw new Error("You should be logged in to post questions");
  77. }
  78.  
  79. if (answer === '') {
  80. throw new Error("Invalid answer");
  81. }
  82.  
  83. let question = this._questions.find(q => q.id == questionId);
  84.  
  85. if (!question) {
  86. throw new Error("There is no such question");
  87. }
  88.  
  89. let currAnswer = { username, content: answer, };
  90. question.answers.push(currAnswer);
  91.  
  92. return "Your answer has been posted successfully";
  93. }
  94.  
  95. showQuestions() {
  96. let output = '';
  97.  
  98. this._questions.forEach(q => {
  99. output += `Question ${q.id} by ${q.username}: ${q.question}\n`;
  100.  
  101. q.answers.forEach(an => {
  102. output += `---${an.username}: ${an.content}\n`;
  103. });
  104.  
  105. });
  106.  
  107. return output.trim();
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement