mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
refactor: course updating
This commit is contained in:
parent
1db5656d67
commit
2b8658072c
@ -155,28 +155,15 @@ export default class CoursesController extends BaseController {
|
|||||||
* Update Course by "id"
|
* Update Course by "id"
|
||||||
* PATCH /courses/:id
|
* PATCH /courses/:id
|
||||||
*/
|
*/
|
||||||
public async update({ response, request, params }: HttpContextContract) {
|
public async update(ctx: HttpContextContract) {
|
||||||
const payload = await request.validate(UpdateCourseValidator);
|
const payload = await ctx.request.validate(UpdateCourseValidator);
|
||||||
const course = await this.Course.findOrFail(params.id);
|
const result = await this.courseService.updateCourse(ctx.params.id, payload);
|
||||||
|
|
||||||
/**
|
if (!result.success && result.error) {
|
||||||
* Update course
|
throw new Exception(result.message, result.status, result.error.code);
|
||||||
*/
|
|
||||||
Object.keys(payload).forEach(k => {
|
|
||||||
if (payload[k] !== undefined) {
|
|
||||||
course[k] = payload[k];
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
await course.save();
|
return this.sendResponse(ctx, result.data, result.message, result.status);
|
||||||
await course.load('category');
|
|
||||||
await course.load('lessons');
|
|
||||||
await course.load('teacher');
|
|
||||||
|
|
||||||
return response.ok({
|
|
||||||
message: `Course with id: "${course.id}" was successfully updated.`,
|
|
||||||
data: course,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
// eslint-disable-next-line import/no-cycle
|
||||||
/* eslint-disable no-param-reassign */
|
/* eslint-disable no-param-reassign */
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
@ -15,7 +16,6 @@ import {
|
|||||||
import Category from 'App/Models/Category';
|
import Category from 'App/Models/Category';
|
||||||
import Lesson from 'App/Models/Lesson';
|
import Lesson from 'App/Models/Lesson';
|
||||||
import User from 'App/Models/User';
|
import User from 'App/Models/User';
|
||||||
// eslint-disable-next-line import/no-cycle
|
|
||||||
|
|
||||||
export default class Course extends BaseModel {
|
export default class Course extends BaseModel {
|
||||||
@column({ isPrimary: true })
|
@column({ isPrimary: true })
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import User from 'App/Models/User';
|
|||||||
* Validators
|
* Validators
|
||||||
*/
|
*/
|
||||||
import CreateCourseValidator from 'App/Validators/Course/CreateCourseValidator';
|
import CreateCourseValidator from 'App/Validators/Course/CreateCourseValidator';
|
||||||
|
import UpdateCourseValidator from 'App/Validators/Course/UpdateCourseValidator';
|
||||||
|
|
||||||
export default class CourseRepository {
|
export default class CourseRepository {
|
||||||
private Course: typeof Course;
|
private Course: typeof Course;
|
||||||
@ -126,4 +127,27 @@ export default class CourseRepository {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update course
|
||||||
|
*
|
||||||
|
* @param id Course id
|
||||||
|
* @param data Data to update
|
||||||
|
* @returns Updated course
|
||||||
|
*/
|
||||||
|
public async update(id: string | number, data: UpdateCourseValidator['schema']['props']): Promise<Course | null> {
|
||||||
|
const course = await this.Course.query()
|
||||||
|
.preload('teacher')
|
||||||
|
.preload('category')
|
||||||
|
.preload('lessons')
|
||||||
|
.where('id', id)
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (course) {
|
||||||
|
await course.merge(data).save();
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import CategoryRepository from 'App/Repositories/CategoryRepository';
|
|||||||
import CourseRepository from 'App/Repositories/CourseRepository';
|
import CourseRepository from 'App/Repositories/CourseRepository';
|
||||||
import UserRepository from 'App/Repositories/UserRepository';
|
import UserRepository from 'App/Repositories/UserRepository';
|
||||||
import CreateCourseValidator from 'App/Validators/Course/CreateCourseValidator';
|
import CreateCourseValidator from 'App/Validators/Course/CreateCourseValidator';
|
||||||
|
import UpdateCourseValidator from 'App/Validators/Course/UpdateCourseValidator';
|
||||||
|
|
||||||
@inject()
|
@inject()
|
||||||
export default class CourseService {
|
export default class CourseService {
|
||||||
@ -219,9 +220,6 @@ export default class CourseService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check teacher on role
|
|
||||||
*/
|
|
||||||
const isTeacher = RoleHelper.userHasRoles(teacher.roles, [RoleEnum.TEACHER]);
|
const isTeacher = RoleHelper.userHasRoles(teacher.roles, [RoleEnum.TEACHER]);
|
||||||
|
|
||||||
if (!isTeacher) {
|
if (!isTeacher) {
|
||||||
@ -294,6 +292,36 @@ export default class CourseService {
|
|||||||
data: course,
|
data: course,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update course
|
||||||
|
*
|
||||||
|
* @param id Course id
|
||||||
|
* @param data Data to update
|
||||||
|
* @returns Response
|
||||||
|
*/
|
||||||
|
public async updateCourse(id: string | number, data: UpdateCourseValidator['schema']['props']): Promise<IResponse> {
|
||||||
|
const course = await this.courseRepository.update(id, data);
|
||||||
|
|
||||||
|
if (!course) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
status: StatusCodeEnum.NOT_FOUND,
|
||||||
|
message: 'Course not found.',
|
||||||
|
data: {},
|
||||||
|
error: {
|
||||||
|
code: 'E_NOT_FOUND',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
status: StatusCodeEnum.OK,
|
||||||
|
message: 'Course updated.',
|
||||||
|
data: course,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new Ioc().make(CourseService);
|
new Ioc().make(CourseService);
|
||||||
|
|||||||
@ -7,7 +7,6 @@ export default class UpdateCourseValidator {
|
|||||||
public schema = schema.create({
|
public schema = schema.create({
|
||||||
title: schema.string.optional(),
|
title: schema.string.optional(),
|
||||||
description: schema.string.optional(),
|
description: schema.string.optional(),
|
||||||
teacher_id: schema.string.optional({}, [rules.exists({ table: 'users', column: 'id' })]),
|
|
||||||
category_id: schema.string.optional({}, [rules.exists({ table: 'categories', column: 'id' })]),
|
category_id: schema.string.optional({}, [rules.exists({ table: 'categories', column: 'id' })]),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user