From d625b804a8e966974cd6c589158b2e851e3ec167 Mon Sep 17 00:00:00 2001 From: Rachchanon Klaisuban Date: Fri, 29 Nov 2024 10:29:23 +0700 Subject: [PATCH] feat: search exam by couseModuleID --- src/exam/exam.controller.ts | 49 +++++++++++++++++++++++++++++++++++++ src/exam/exam.service.ts | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/src/exam/exam.controller.ts b/src/exam/exam.controller.ts index 94ada0f..64f2d29 100644 --- a/src/exam/exam.controller.ts +++ b/src/exam/exam.controller.ts @@ -100,6 +100,55 @@ export class ExamController { return new ExamResponseDto(exam); } + @Get('course-module/:courseModuleId') + @ApiResponse({ + status: HttpStatus.OK, + description: 'Returns all exams by courseModule id', + type: PaginatedExamResponseDto, + isArray: true, + }) + @ApiQuery({ + name: 'page', + type: Number, + required: false, + description: 'Page number', + }) + @ApiQuery({ + name: 'limit', + type: Number, + required: false, + description: 'Items per page', + }) + @ApiQuery({ + name: 'search', + type: String, + required: false, + description: 'Search by title', + }) + async findExamByCourseModuleId( + @Req() request: AuthenticatedRequest, + @Query() query: PaginateQueryDto, + @Param( + 'courseModuleId', + new ParseUUIDPipe({ + version: '4', + errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY, + }), + ) + courseModuleId: string, + ): Promise { + return await this.examService.findExamByCourseModuleId( + request.user.id, + request.user.role, + courseModuleId, + { + page: query.page, + limit: query.limit, + search: query.search, + }, + ); + } + @Post() @Roles(Role.TEACHER) @ApiResponse({ diff --git a/src/exam/exam.service.ts b/src/exam/exam.service.ts index 59ecc3e..f0dc13c 100644 --- a/src/exam/exam.service.ts +++ b/src/exam/exam.service.ts @@ -165,6 +165,53 @@ export class ExamService { return exam; } + async findExamByCourseModuleId( + userId: string, + role: Role, + courseModuleId: string, + { + page = 1, + limit = 20, + search = '', + }: { + page?: number; + limit?: number; + search?: string; + }, + ): Promise { + const { find } = await createPagination(this.examRepository, { + page, + limit, + }); + + const whereCondition = this.validateAndCreateCondition( + userId, + role, + search, + ); + whereCondition['courseModule'] = { id: courseModuleId }; + + const courseModule = await this.courseModuleRepository.findOne({ + where: { id: courseModuleId }, + }); + + if (!courseModule) { + throw new NotFoundException('courseModule not found.'); + } + + return await find({ + where: whereCondition, + relations: [ + 'courseModule', + 'courseModule.course', + 'courseModule.course.teacher', + ], + select: { + courseModule: this.selectPopulateCourseModule(), + }, + }).run(); + } + async createExam(createExamDto: CreateExamDto): Promise { const courseModule = await this.courseModuleRepository.findOne({ where: { id: createExamDto.courseModuleId },