mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
feat: added limit on fetching courses
This commit is contained in:
parent
6248134614
commit
e3dd4a8d6a
@ -49,7 +49,7 @@ export default class CourseRepository {
|
|||||||
* @returns List of courses
|
* @returns List of courses
|
||||||
*/
|
*/
|
||||||
public async getAll(params?: FetchCoursesValidator['schema']['props']): Promise<Course[]> {
|
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();
|
const query = this.Course.query();
|
||||||
|
|
||||||
@ -61,12 +61,17 @@ export default class CourseRepository {
|
|||||||
query.andWhere('status', status);
|
query.andWhere('status', status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (limit) {
|
||||||
|
query.limit(limit);
|
||||||
|
}
|
||||||
|
|
||||||
const courses = await query
|
const courses = await query
|
||||||
.preload('color')
|
.preload('color')
|
||||||
.preload('category')
|
.preload('category')
|
||||||
.withCount('students')
|
.withCount('students')
|
||||||
.withCount('likes')
|
.withCount('likes')
|
||||||
.withCount('lessons');
|
.withCount('lessons')
|
||||||
|
.orderBy('created_at', 'desc');
|
||||||
|
|
||||||
return courses;
|
return courses;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ export default class FetchCoursesValidator {
|
|||||||
public schema = schema.create({
|
public schema = schema.create({
|
||||||
status: schema.enum.optional(Object.values(CourseStatusEnum)),
|
status: schema.enum.optional(Object.values(CourseStatusEnum)),
|
||||||
category_id: schema.string.optional({}, [rules.exists({ table: 'categories', column: 'id' })]),
|
category_id: schema.string.optional({}, [rules.exists({ table: 'categories', column: 'id' })]),
|
||||||
|
limit: schema.number.optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
public messages = {};
|
public messages = {};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user