Advertisement
kstoyanov

03. Ski Forum-26 October 2019- exam

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