feat: added limit on fetching courses

This commit is contained in:
Sergey Yarkov 2022-01-23 22:43:36 +03:00
parent 6248134614
commit e3dd4a8d6a
2 changed files with 8 additions and 2 deletions

View File

@ -49,7 +49,7 @@ export default class CourseRepository {
* @returns List of courses
*/
public async getAll(params?: FetchCoursesValidator['schema']['props']): Promise<Course[]> {
const { status, category_id } = params || {};
const { status, category_id, limit } = params || {};
const query = this.Course.query();
@ -61,12 +61,17 @@ export default class CourseRepository {
query.andWhere('status', status);
}
if (limit) {
query.limit(limit);
}
const courses = await query
.preload('color')
.preload('category')
.withCount('students')
.withCount('likes')
.withCount('lessons');
.withCount('lessons')
.orderBy('created_at', 'desc');
return courses;
}

View File

@ -8,6 +8,7 @@ export default class FetchCoursesValidator {
public schema = schema.create({
status: schema.enum.optional(Object.values(CourseStatusEnum)),
category_id: schema.string.optional({}, [rules.exists({ table: 'categories', column: 'id' })]),
limit: schema.number.optional(),
});
public messages = {};