Advertisement
Todorov_Stanimir

03. Ski Forum JS Advanced - 26 October 2019

Dec 8th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Forum {
  2.     constructor() {
  3.         this._id = 1;
  4.         this._users = [];
  5.         this._questions = [];
  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.         if (this.findIndexOfUser(username) !== -1) {
  18.             throw new Error('This user already exists!');
  19.         }
  20.  
  21.         let newUser = {
  22.             username,
  23.             password,
  24.             email,
  25.             isLogged: false,
  26.         }
  27.  
  28.         this._users.push(newUser)
  29.         return `${username} with ${email} was registered successfully!`
  30.     }
  31.  
  32.     login(username, password) {
  33.         let userIndex = this.findIndexOfUser(username);
  34.  
  35.         if (userIndex === -1) {
  36.             throw new Error('There is no such user');
  37.         }
  38.  
  39.         if (this._users[userIndex].password === password) {
  40.             this._users[userIndex].isLogged = true;
  41.             return 'Hello! You have logged in successfully'
  42.         }
  43.  
  44.     }
  45.  
  46.     logout(username, password) {
  47.         let userIndex = this.findIndexOfUser(username);
  48.  
  49.         if (userIndex === -1) {
  50.             throw new Error('There is no such user');
  51.         }
  52.  
  53.         if (this._users[userIndex].password === password) {
  54.             this._users[userIndex].isLogged = false;
  55.             return 'You have logged out successfully'
  56.         }
  57.     }
  58.  
  59.     postQuestion(username, question) {
  60.         let userIndex = this.findIndexOfUser(username);
  61.  
  62.         if (userIndex === -1 || this._users[userIndex].isLogged === false) {
  63.             throw new Error('You should be logged in to post questions');
  64.         }
  65.         if (question === '') {
  66.             throw new Error('Invalid question');
  67.         }
  68.         let newQuestion = {
  69.             id: this._id,
  70.             username,
  71.             question,
  72.             answers: [],
  73.         }
  74.  
  75.         this._questions.push(newQuestion);
  76.         this._id++;
  77.  
  78.         //test 7
  79.         return 'Your question has been posted successfully';
  80.     }
  81.  
  82.     postAnswer(username, questionId, answer) {
  83.         let userIndex = this.findIndexOfUser(username);
  84.  
  85.         if (userIndex === -1 || this._users[userIndex].isLogged === false) {
  86.             throw new Error('You should be logged in to post answers');
  87.         }
  88.         if (answer === '') {
  89.             throw new Error('Invalid answer');
  90.         }
  91.  
  92.         let questionIndex = this._questions.findIndex(question => question.id == questionId);
  93.  
  94.         if (questionIndex === -1) {
  95.             throw new Error('There is no such question');
  96.         }
  97.  
  98.         this._questions[questionIndex].answers.push({ username, answer });
  99.  
  100.         return 'Your answer has been posted successfully';
  101.     }
  102.  
  103.     showQuestions() {
  104.  
  105.         // let result = '';
  106.         return this._questions
  107.             .map(q => `Question ${q.id} by ${q.username}: ${q.question}\n` + q.answers.map(ans => { return `---${ans.username}: ${ans.answer}` }).join('\n')).join('\n')
  108.     }
  109.  
  110.     findIndexOfUser(username) {
  111.         return this._users.findIndex(user => user.username === username);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement