krasizorbov

Ski Forum

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