Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/SAIG-KMITL/edusaig-api into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganthepro committed Nov 29, 2024
2 parents a3f7e2a + dc88fe8 commit e2c1619
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/exam/exam.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaginatedExamResponseDto> {
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({
Expand Down
47 changes: 47 additions & 0 deletions src/exam/exam.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaginatedExamResponseDto> {
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<Exam> {
const courseModule = await this.courseModuleRepository.findOne({
where: { id: createExamDto.courseModuleId },
Expand Down

0 comments on commit e2c1619

Please sign in to comment.