diff --git a/app/Repositories/CourseRepository.ts b/app/Repositories/CourseRepository.ts index dfb57b8..1e8b102 100644 --- a/app/Repositories/CourseRepository.ts +++ b/app/Repositories/CourseRepository.ts @@ -78,7 +78,7 @@ export default class CourseRepository { * @param id Course id * @returns Likes count */ - public async getLikesCount(id: string | number): Promise { + public async getLikesCount(id: string | number): Promise { const likes = await this.Database.query().from('courses_likes').where('course_id', id).count('* as count'); return likes[0].count; } @@ -138,10 +138,9 @@ export default class CourseRepository { * @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); + public async getStudentsCount(id: string | number): Promise { + const students = await this.Database.query().from('users_courses').where('course_id', id).count('* as count'); + return students[0].count; } /** diff --git a/app/Services/CourseService.ts b/app/Services/CourseService.ts index 150fc97..7de6300 100644 --- a/app/Services/CourseService.ts +++ b/app/Services/CourseService.ts @@ -209,25 +209,13 @@ export default class CourseService { * @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', - }, - }; - } + const count = await this.courseRepository.getStudentsCount(id); return { success: true, status: StatusCodeEnum.OK, message: 'Fetched students count.', - data, + data: { count }, }; }