feat: students count for course

This commit is contained in:
Sergey Yarkov 2021-08-10 18:20:36 +03:00
parent 7e24c913a0
commit d96a54351e
5 changed files with 57 additions and 2 deletions

View File

@ -181,6 +181,16 @@ export default class CoursesController extends BaseController {
return this.sendResponse(ctx, result.data, result.message, result.status); 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); new Ioc().make(CoursesController);

View File

@ -86,7 +86,7 @@ export default class CourseRepository {
} }
/** /**
* Get students of cours * Get students of course
* *
* @param id Course id * @param id Course id
* @returns List of users or null * @returns List of users or null
@ -96,6 +96,18 @@ export default class CourseRepository {
return course && course.students; 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 * Create course
* *

View File

@ -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<IResponse> {
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 * Create course
* *

View File

@ -22,6 +22,10 @@ Route.group(() => {
.middleware('role:admin,teacher,student') .middleware('role:admin,teacher,student')
.as('courses.show-students'); .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') Route.post('/:id/attach-student', 'Api/v1/CoursesController.attachStudent')
.middleware('role:admin,teacher') .middleware('role:admin,teacher')
.as('courses.attach-student'); .as('courses.attach-student');

View File

@ -2,7 +2,7 @@
import Route from '@ioc:Adonis/Core/Route'; import Route from '@ioc:Adonis/Core/Route';
Route.group(() => { Route.group(() => {
Route.get('/', () => `Educt Backend API.`).as('index'); Route.get('/', () => `Backend API.`).as('index');
/** /**
* *