Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/#160 #161

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/apis/v1/questions/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ export const deleteQuestion = async (req: RequestWithUser, res: Response) => {
const result = await service.deleteQuestion(params.id);
res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK'));
};

export const getQuestionsByExamId = async (req: RequestWithUser, res: Response) => {
const params: ParamsQuestionDto = req.params;

const result = await service.getQuestionsByExamId(params.id);
res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK'));
};
6 changes: 6 additions & 0 deletions src/apis/v1/questions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { QuestionDto, UpdateQuestionDto, ParamsQuestionDto } from './dto/Questio
const router = Router();

router.get('/', authMiddleware, asyncRouteHandler(controller.getQuestions));
router.get(
'/exam/:id',
authMiddleware,
validationMiddleware(ParamsQuestionDto, APP_CONSTANTS.params),
asyncRouteHandler(controller.getQuestionsByExamId)
);

router.post(
'/',
Expand Down
24 changes: 24 additions & 0 deletions src/apis/v1/questions/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import QuestionModel from 'models/schema/Question';
import { DEFAULT_PAGING } from 'utils/constants';
import { logger } from 'utils/logger';
import URLParams from 'utils/rest/urlparams';
import { hideUserInfoIfRequired } from 'utils';

import { QuestionDto, UpdateQuestionDto } from './dto/QuestionDto';
import { ExamModel } from 'models';

export const createQuestion = async function (input: QuestionDto, author: ObjectId) {
try {
Expand Down Expand Up @@ -96,3 +98,25 @@ export const deleteQuestion = async function (id: string) {
throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE);
}
};

export const getQuestionsByExamId = async function (examId: string) {
try {
const data = await QuestionModel.find({ exam_id: examId });

const exam = ExamModel.findOne({ _id: examId })
.populate('subject', '-is_deleted -created_at -updated_at -__v')
.populate('author', '-is_blocked -roles -created_at -updated_at -__v');
const resultAll = await Promise.all([data, exam]);

return {
questions: resultAll[0],
exam: {
...resultAll[1].toObject(),
author: hideUserInfoIfRequired(resultAll[1].author),
},
};
} catch (error) {
logger.error(`Error while get questions by examId: ${error}`);
throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE);
}
};