Advertisement
Guest User

Untitled

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