LobbyRouter.ts 2.65 KB
Newer Older
1
import { BodyParam, Delete, Get, JsonController, Param, Put } from 'routing-controllers';
2
import AMQPConnector from '../../db/AMQPConnector';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
3 4 5 6 7 8 9
import DbDAO from '../../db/DbDAO';
import QuizDAO from '../../db/quiz/QuizDAO';
import { DbCollection } from '../../enums/DbOperation';
import { MessageProtocol, StatusProtocol } from '../../enums/Message';
import { QuizState } from '../../enums/QuizState';
import { IQuizSerialized } from '../../interfaces/quizzes/IQuizEntity';
import { QuizModel } from '../../models/quiz/QuizModelItem';
10 11 12 13 14 15
import { AbstractRouter } from './AbstractRouter';

@JsonController('/api/v1/lobby')
export class LobbyRouter extends AbstractRouter {

  @Put('/')
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
16 17 18 19
  private async putOpenLobby( //
    @BodyParam('quiz') quiz: IQuizSerialized, //
    @BodyParam('privateKey') privateKey: string, //
  ): Promise<object> {
20

21
    AMQPConnector.channel.publish('global', '.*', Buffer.from(JSON.stringify({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
22 23
      status: StatusProtocol.Success,
      step: MessageProtocol.SetActive,
24
      payload: {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
25
        quizName: quiz.name,
26
      },
27
    })));
28

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
29 30 31 32 33
    quiz.state = QuizState.Active;
    quiz.currentQuestionIndex = -1;
    quiz.currentStartTimestamp = -1;
    const addedQuiz = QuizDAO.getQuizByName(quiz.name);
    if (addedQuiz) {
34
      DbDAO.updateOne(DbCollection.Quizzes, { _id: addedQuiz.id }, quiz);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
35 36 37 38 39 40 41 42 43
    } else {
      const quizValidator = new QuizModel(quiz);
      const result = quizValidator.validateSync();
      if (result) {
        throw result;
      }
      await quizValidator.save();
    }

44
    return {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
45 46
      status: StatusProtocol.Success,
      step: MessageProtocol.Opened,
47 48 49 50 51 52 53
    };
  }

  @Get('/:quizName')
  private getLobbyData(@Param('quizName') quizName: string, //
  ): object {

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
54 55
    const isActive = QuizDAO.isActiveQuiz(quizName);
    const quiz = isActive ? QuizDAO.getActiveQuizByName(quizName).serialize() : null;
56 57

    return {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
58 59
      status: StatusProtocol.Success,
      step: isActive ? MessageProtocol.Opened : MessageProtocol.Closed,
60 61 62 63 64 65 66 67 68 69
      payload: {
        quiz,
      },
    };
  }

  @Delete('/')
  private deleteLobby(@BodyParam('quizName') quizName: string, //
  ): object {

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
70 71
    const addedQuiz = QuizDAO.getQuizByName(quizName);
    if (addedQuiz) {
72
      DbDAO.updateOne(DbCollection.Quizzes, { _id: addedQuiz.id }, { state: QuizState.Inactive });
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
73
    }
74

75 76 77 78 79 80 81 82
    AMQPConnector.channel.publish('global', '.*', Buffer.from(JSON.stringify({
      status: StatusProtocol.Success,
      step: MessageProtocol.SetInactive,
      payload: {
        quizName,
      },
    })));

83
    return {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
84 85
      status: StatusProtocol.Success,
      step: MessageProtocol.Closed,
86 87 88 89 90 91 92 93 94
      payload: {},
    };
  }

  @Get('/')
  private getAll(): object {
    return {};
  }
}