Advertisement
Guest User

ski forum\

a guest
Oct 10th, 2020
126
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.  
  68.     postAnswer(username, questionId, answer) {
  69.         const usernameCheck = this._users.find((o) => { return o.username === username });
  70.         const idCheck = this._questions.find((o) => { return o.id === questionId });
  71.         if (!this.loggedUsers.includes(username) || !usernameCheck) {
  72.             throw Error("You should be logged in to post answers");
  73.         }
  74.  
  75.         if (!answer) {
  76.             throw Error("Invalid answer");
  77.         }
  78.  
  79.         if (!idCheck) {
  80.             throw Error("There is no such question");
  81.         }
  82.  
  83.         idCheck.answer.push([username, answer]);
  84.  
  85.         return "Your answer has been posted successfully";
  86.     }
  87.  
  88.     showQuestions() {
  89.         let result = [];
  90.  
  91.         for (const line of this._questions) {
  92.             result.push(`Question ${line.id} by ${line.username}: ${line.question}`)
  93.  
  94.             for (const x of line.answer) {
  95.                 result.push(`---${x.shift()}: ${x.shift()}`);
  96.             }
  97.         }
  98.         return result.join('\n');
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement