Removes the lobby router.

parent d924640f
......@@ -16,7 +16,6 @@ import { ApiRouter } from './routers/rest/ApiRouter';
import { ExpiryQuizRouter } from './routers/rest/ExpiryQuizRouter';
import { I18nApiRouter } from './routers/rest/I18nApiRouter';
import { LibRouter } from './routers/rest/LibRouter';
import { LobbyRouter } from './routers/rest/LobbyRouter';
import { MemberRouter } from './routers/rest/MemberRouter';
import { NicksRouter } from './routers/rest/NicksRouter';
import { QuizRouter } from './routers/rest/QuizRouter';
......@@ -34,7 +33,7 @@ export const routingControllerOptions: RoutingControllersOptions = {
defaultErrorHandler: false,
cors: options,
controllers: [
AdminRouter, ApiRouter, ExpiryQuizRouter, I18nApiRouter, LibRouter, LobbyRouter, MemberRouter, NicksRouter, QuizRouter,
AdminRouter, ApiRouter, ExpiryQuizRouter, I18nApiRouter, LibRouter, MemberRouter, NicksRouter, QuizRouter,
],
middlewares: [I18nMiddleware],
};
......
......@@ -175,8 +175,8 @@ class QuizDAO extends AbstractDAO {
});
}
public async setQuizAsInactive(quizName: string, privateKey: string): Promise<Document & QuizModelItem> {
const doc = await QuizModel.updateOne({
public async setQuizAsInactive(quizName: string, privateKey: string): Promise<void> {
await QuizModel.updateOne({
name: this.buildQuiznameQuery(quizName),
privateKey,
}, { state: QuizState.Inactive }).exec();
......@@ -194,7 +194,7 @@ class QuizDAO extends AbstractDAO {
step: MessageProtocol.Closed,
})));
return doc;
await MemberDAO.removeMembersOfQuiz(quizName);
}
public getActiveQuizByName(quizName: string): Promise<Document & QuizModelItem> {
......
import { BodyParam, Delete, Get, JsonController, Param, Put } from 'routing-controllers';
import AMQPConnector from '../../db/AMQPConnector';
import QuizDAO from '../../db/quiz/QuizDAO';
import { MessageProtocol, StatusProtocol } from '../../enums/Message';
import { QuizState } from '../../enums/QuizState';
import { QuizModelItem } from '../../models/quiz/QuizModelItem';
import { AbstractRouter } from './AbstractRouter';
@JsonController('/api/v1/lobby')
export class LobbyRouter extends AbstractRouter {
@Put('/')
private async putOpenLobby( //
@BodyParam('quiz') quiz: QuizModelItem, //
@BodyParam('privateKey') privateKey: string, //
): Promise<object> {
AMQPConnector.channel.publish(AMQPConnector.globalExchange, '.*', Buffer.from(JSON.stringify({
status: StatusProtocol.Success,
step: MessageProtocol.SetActive,
payload: {
quizName: quiz.name,
},
})));
quiz.state = QuizState.Active;
quiz.currentQuestionIndex = -1;
quiz.currentStartTimestamp = -1;
const addedQuiz = await QuizDAO.getQuizByName(quiz.name);
if (addedQuiz) {
await QuizDAO.updateQuiz(addedQuiz._id, quiz);
} else {
await QuizDAO.addQuiz(quiz);
}
return {
status: StatusProtocol.Success,
step: MessageProtocol.Opened,
};
}
@Get('/:quizName')
private async getLobbyData(@Param('quizName') quizName: string, //
): Promise<object> {
const isActive = QuizDAO.isActiveQuiz(quizName);
const quiz = isActive ? (await QuizDAO.getActiveQuizByName(quizName)).toJSON() : null;
return {
status: StatusProtocol.Success,
step: isActive ? MessageProtocol.Opened : MessageProtocol.Closed,
payload: {
quiz,
},
};
}
@Delete('/')
private async deleteLobby(@BodyParam('quizName') quizName: string, //
): Promise<object> {
const addedQuiz = await QuizDAO.getQuizByName(quizName);
if (addedQuiz) {
await QuizDAO.updateQuiz(addedQuiz._id, { state: QuizState.Inactive });
}
AMQPConnector.channel.publish(AMQPConnector.globalExchange, '.*', Buffer.from(JSON.stringify({
status: StatusProtocol.Success,
step: MessageProtocol.SetInactive,
payload: {
quizName,
},
})));
return {
status: StatusProtocol.Success,
step: MessageProtocol.Closed,
payload: {},
};
}
@Get('/')
private getAll(): object {
return {};
}
}
/// <reference path="../../../node_modules/chai-http/types/index.d.ts" />
import * as chai from 'chai';
import * as fs from 'fs';
import { suite, test } from 'mocha-typescript';
import * as mongoUnit from 'mongo-unit';
import * as path from 'path';
import app from '../../App';
import QuizDAO from '../../db/quiz/QuizDAO';
import { IQuiz } from '../../interfaces/quizzes/IQuizEntity';
import { staticStatistics } from '../../statistics';
import { generateQuiz } from '../fixtures';
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
const expect = chai.expect;
const hashtag = 'mocha-test-api-v1';
@suite
class LobbyApiRouterTestSuite {
private _baseApiRoute = `${staticStatistics.routePrefix}/api/v1/lobby`;
private _hashtag = hashtag;
public async before(): Promise<void> {
await mongoUnit.initDb(process.env.MONGODB_CONN_URL, []);
const quiz: IQuiz = generateQuiz(hashtag);
const doc = await QuizDAO.addQuiz(quiz);
await QuizDAO.initQuiz(doc);
}
public async after(): Promise<void> {
return mongoUnit.drop();
}
@test
public async baseApiExists(): Promise<void> {
const res = await chai.request(app).get(`${this._baseApiRoute}`);
expect(res.status).to.equal(200);
expect(res.type).to.equal('application/json');
}
@test
public async putOpenLobby(): Promise<void> {
const quiz: IQuiz = JSON.parse(
fs.readFileSync(path.join(staticStatistics.pathToAssets, 'predefined_quizzes', 'demo_quiz', 'en.demo_quiz.json')).toString('UTF-8'));
quiz.name = this._hashtag;
const res = await chai.request(app).put(`${this._baseApiRoute}/`).send({
quiz,
privateKey: 'privateKey',
});
expect(res.status).to.equal(200);
expect(res.type).to.equal('application/json');
await expect((await QuizDAO.isActiveQuiz(this._hashtag))).to.be.true;
}
@test
public async getLobbyData(): Promise<void> {
const res = await chai.request(app).get(`${this._baseApiRoute}/${this._hashtag}`);
expect(res.status).to.equal(200);
expect(res.type).to.equal('application/json');
}
@test
public async putCloseLobby(): Promise<void> {
const res = await chai.request(app).del(`${this._baseApiRoute}/`).send({ quizName: this._hashtag });
expect(res.status).to.equal(200);
expect(res.type).to.equal('application/json');
await expect(await QuizDAO.isActiveQuiz(this._hashtag)).to.be.false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment