Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. let currentSessionID = 0;
  4. let currentRatingID = 0;
  5. let sessions = [];
  6.  
  7. module.exports = function () {
  8.  
  9.     // enum
  10.     var SessionState = {
  11.         Created: 1,
  12.         InEvaluation: 2,
  13.         Evaluatd: 3,
  14.         Closed: 4
  15.     };
  16.  
  17.     // objects
  18.     function Session(title, speaker) {
  19.         this.id = currentSessionID;
  20.         this.speaker = speaker;
  21.         this.title = title;
  22.         this.ratings = [];
  23.         this.currentSessionState = SessionState.Created;
  24.         currentSessionID++;
  25.     }
  26.     function Rating(evaluator, ratingValue) {
  27.         this.rateID = currentRatingID;
  28.         this.evaluator = evaluator;
  29.         this.ratingValue = ratingValue;
  30.         currentRatingID++;
  31.     }
  32.     function _createSession(title, speaker) {
  33.         if (sessions.filter(x => x.title === title).length > 0)
  34.             throw "Session with the title ''" + title + "'' already exits.";
  35.        
  36.         sessions.push(new Session(title, speaker));
  37.     }
  38.     function _rateSession(sessionID, evaluator, ratingValue) {
  39.         if (evaluator.Length == 0 || ratingValue < 0 || ratingValue > 10)
  40.             throw "no valid data was passed to the function";
  41.  
  42.         let current_session = _getSession(sessionID);
  43.  
  44.         if (current_session.currentSessionState == SessionState.Closed)
  45.             throw "You can't rate a closed session";
  46.  
  47.         current_session.ratings.push(new Rating(evaluator, ratingValue));
  48.         current_session.currentSessionState = SessionState.Evaluatd;
  49.     }
  50.     function _closeSession(sessionID) {
  51.         let current_session = _getSession(sessionID);
  52.  
  53.         if (current_session.currentSessionState == SessionState.Created)
  54.             throw "You can't close an unevaluated session.";
  55.  
  56.         current_session.currentSessionState = SessionState.Closed;
  57.     }
  58.     function _deleteSession(sessionID) {
  59.         let currentSession = _getSession(sessionID);
  60.         let idx = sessions.indexOf(currentSession);
  61.         let length = sessions.length - 1;
  62.  
  63.         let temporarySession = sessions[length];
  64.         sessions[length] = currentSession;
  65.         sessions[idx] = temporarySession;
  66.  
  67.         sessions.pop();
  68.     }
  69.     function _getSession(sessionID) {
  70.         let returendSession = sessions.filter(x => x.id === sessionID)[0];
  71.  
  72.         if (returendSession == null)
  73.             throw "Session with the ID = " + sessionID + " doesn't exist.";
  74.  
  75.         return returendSession;
  76.     }
  77.     function _getSessions() {
  78.         return sessions;
  79.     }
  80.    
  81.    
  82.  
  83.     return {    
  84.         CreateSession: _createSession,
  85.         GetSession: _getSession,
  86.         GetSessions: _getSessions,
  87.         RateSession: _rateSession,
  88.         CloseSession: _closeSession,
  89.         DeleteSession: _deleteSession
  90.     };
  91.  
  92. } ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement