Skip to content

Commit

Permalink
Dev (#137)
Browse files Browse the repository at this point in the history
* feat/#135 (#136)

---------

Co-authored-by: Loc Xuan Dao <102164071+locxuandao@users.noreply.github.com>
  • Loading branch information
nc-minh and locxuandao authored Mar 18, 2023
1 parent e97b190 commit b9bdd7b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/apis/v1/exam/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ export const deleteExam = async (req: RequestWithUser, res: Response) => {

res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK'));
};

export const getExamsBySubjectId = async (req: RequestWithUser, res: Response) => {
const input: ParamsExamDto = req.params;
const result = await service.getExamsBySubjectId(input.id);
res.send(fmt.formatResponse(result, Date.now() - req.startTime, 'OK'));
};
7 changes: 7 additions & 0 deletions src/apis/v1/exam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ router.get(
asyncRouteHandler(controller.getExamById)
);

router.get(
'/subject/:id',
authMiddleware,
validationMiddleware(ParamsExamDto, APP_CONSTANTS.params),
asyncRouteHandler(controller.getExamsBySubjectId)
);

router.post(
'/',
authMiddleware,
Expand Down
22 changes: 22 additions & 0 deletions src/apis/v1/exam/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,25 @@ export const deleteExam = async (id: string) => {
throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE);
}
};

export const getExamsBySubjectId = async (subjectId: string) => {
try {
const results = ExamModel.find({ is_approved: true, subject: subjectId })
.populate('author', '-is_blocked -roles -created_at -updated_at -__v')
.populate('question', '-is_blocked -roles -created_at -updated_at -__v')
.populate('subject', '-is_deleted -created_at -updated_at -__v');

const subject = SubjectModel.findOne({ _id: subjectId });

const resultAll = await Promise.all([results, subject]);

logger.info(`Get all exams by subjectId successfully`);
return {
exams: resultAll[0],
subject: resultAll[1],
};
} catch (error) {
logger.error(`Error while get exams by subjectId: ${error}`);
throw new HttpException(400, ErrorCodes.BAD_REQUEST.MESSAGE, ErrorCodes.BAD_REQUEST.CODE);
}
};

0 comments on commit b9bdd7b

Please sign in to comment.