Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- let currentSessionID = 0;
- let currentRatingID = 0;
- let sessions = [];
- module.exports = function () {
- // enum
- var SessionState = {
- Created: 1,
- InEvaluation: 2,
- Evaluatd: 3,
- Closed: 4
- };
- // objects
- function Session(title, speaker) {
- this.id = currentSessionID;
- this.speaker = speaker;
- this.title = title;
- this.ratings = [];
- this.currentSessionState = SessionState.Created;
- currentSessionID++;
- }
- function Rating(evaluator, ratingValue) {
- this.rateID = currentRatingID;
- this.evaluator = evaluator;
- this.ratingValue = ratingValue;
- currentRatingID++;
- }
- function _createSession(title, speaker) {
- if (sessions.filter(x => x.title === title).length > 0)
- throw "Session with the title ''" + title + "'' already exits.";
- sessions.push(new Session(title, speaker));
- }
- function _rateSession(sessionID, evaluator, ratingValue) {
- if (evaluator.Length == 0 || ratingValue < 0 || ratingValue > 10)
- throw "no valid data was passed to the function";
- let current_session = _getSession(sessionID);
- if (current_session.currentSessionState == SessionState.Closed)
- throw "You can't rate a closed session";
- current_session.ratings.push(new Rating(evaluator, ratingValue));
- current_session.currentSessionState = SessionState.Evaluatd;
- }
- function _closeSession(sessionID) {
- let current_session = _getSession(sessionID);
- if (current_session.currentSessionState == SessionState.Created)
- throw "You can't close an unevaluated session.";
- current_session.currentSessionState = SessionState.Closed;
- }
- function _deleteSession(sessionID) {
- let currentSession = _getSession(sessionID);
- let idx = sessions.indexOf(currentSession);
- let length = sessions.length - 1;
- let temporarySession = sessions[length];
- sessions[length] = currentSession;
- sessions[idx] = temporarySession;
- sessions.pop();
- }
- function _getSession(sessionID) {
- let returendSession = sessions.filter(x => x.id === sessionID)[0];
- if (returendSession == null)
- throw "Session with the ID = " + sessionID + " doesn't exist.";
- return returendSession;
- }
- function _getSessions() {
- return sessions;
- }
- return {
- CreateSession: _createSession,
- GetSession: _getSession,
- GetSessions: _getSessions,
- RateSession: _rateSession,
- CloseSession: _closeSession,
- DeleteSession: _deleteSession
- };
- } ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement