diff --git a/app/Controllers/Http/Api/v1/CoursesController.ts b/app/Controllers/Http/Api/v1/CoursesController.ts index ef5f6e2..ef49b31 100644 --- a/app/Controllers/Http/Api/v1/CoursesController.ts +++ b/app/Controllers/Http/Api/v1/CoursesController.ts @@ -181,6 +181,16 @@ export default class CoursesController extends BaseController { return this.sendResponse(ctx, result.data, result.message, result.status); } + + public async studentsCount(ctx: HttpContextContract) { + const result = await this.courseService.fetchStudentsCount(ctx.params.id); + + if (!result.success && result.error) { + throw new Exception(result.message, result.status, result.error.code); + } + + return this.sendResponse(ctx, result.data, result.message, result.status); + } } new Ioc().make(CoursesController); diff --git a/app/Repositories/CourseRepository.ts b/app/Repositories/CourseRepository.ts index 226897c..3ad6d7c 100644 --- a/app/Repositories/CourseRepository.ts +++ b/app/Repositories/CourseRepository.ts @@ -86,7 +86,7 @@ export default class CourseRepository { } /** - * Get students of cours + * Get students of course * * @param id Course id * @returns List of users or null @@ -96,6 +96,18 @@ export default class CourseRepository { return course && course.students; } + /** + * Get count of students + * + * @param id Course if + * @returns Students count + */ + public async getStudentsCount(id: string | number): Promise<{ count: any } | null> { + const course = await this.Course.query().where('id', id).withCount('students').first(); + + return course && ({ count: course.$extras.students_count } ?? null); + } + /** * Create course * diff --git a/app/Services/CourseService.ts b/app/Services/CourseService.ts index f18d3dd..4571e67 100644 --- a/app/Services/CourseService.ts +++ b/app/Services/CourseService.ts @@ -196,6 +196,35 @@ export default class CourseService { }; } + /** + * Fetch students count by course id + * + * @param id Course id + * @returns Response + */ + public async fetchStudentsCount(id: string | number): Promise { + const data = await this.courseRepository.getStudentsCount(id); + + if (!data) { + return { + success: false, + status: StatusCodeEnum.NOT_FOUND, + message: 'Course not found.', + data: {}, + error: { + code: 'E_NOT_FOUND', + }, + }; + } + + return { + success: true, + status: StatusCodeEnum.OK, + message: 'Fetched students count.', + data, + }; + } + /** * Create course * diff --git a/routes/apis/v1/courses.ts b/routes/apis/v1/courses.ts index e0fb32e..c13479a 100644 --- a/routes/apis/v1/courses.ts +++ b/routes/apis/v1/courses.ts @@ -22,6 +22,10 @@ Route.group(() => { .middleware('role:admin,teacher,student') .as('courses.show-students'); + Route.get('/:id/students/count', 'Api/v1/CoursesController.studentsCount') + .middleware('role:admin,teacher,student') + .as('courses.students-count'); + Route.post('/:id/attach-student', 'Api/v1/CoursesController.attachStudent') .middleware('role:admin,teacher') .as('courses.attach-student'); diff --git a/routes/apis/v1/index.ts b/routes/apis/v1/index.ts index 57346de..0451fd2 100644 --- a/routes/apis/v1/index.ts +++ b/routes/apis/v1/index.ts @@ -2,7 +2,7 @@ import Route from '@ioc:Adonis/Core/Route'; Route.group(() => { - Route.get('/', () => `Educt Backend API.`).as('index'); + Route.get('/', () => `Backend API.`).as('index'); /** *