mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 18:29:15 +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"
|
||||
* PATCH /courses/:id
|
||||
*/
|
||||
public async update({ response, request, params }: HttpContextContract) {
|
||||
const payload = await request.validate(UpdateCourseValidator);
|
||||
const course = await this.Course.findOrFail(params.id);
|
||||
public async update(ctx: HttpContextContract) {
|
||||
const payload = await ctx.request.validate(UpdateCourseValidator);
|
||||
const result = await this.courseService.updateCourse(ctx.params.id, payload);
|
||||
|
||||
/**
|
||||
* Update course
|
||||
*/
|
||||
Object.keys(payload).forEach(k => {
|
||||
if (payload[k] !== undefined) {
|
||||
course[k] = payload[k];
|
||||
}
|
||||
});
|
||||
if (!result.success && result.error) {
|
||||
throw new Exception(result.message, result.status, result.error.code);
|
||||
}
|
||||
|
||||
await course.save();
|
||||
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,
|
||||
});
|
||||
return this.sendResponse(ctx, result.data, result.message, result.status);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
// eslint-disable-next-line import/no-cycle
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { nanoid } from 'nanoid';
|
||||
import { DateTime } from 'luxon';
|
||||
@ -15,7 +16,6 @@ import {
|
||||
import Category from 'App/Models/Category';
|
||||
import Lesson from 'App/Models/Lesson';
|
||||
import User from 'App/Models/User';
|
||||
// eslint-disable-next-line import/no-cycle
|
||||
|
||||
export default class Course extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
|
||||
@ -10,6 +10,7 @@ import User from 'App/Models/User';
|
||||
* Validators
|
||||
*/
|
||||
import CreateCourseValidator from 'App/Validators/Course/CreateCourseValidator';
|
||||
import UpdateCourseValidator from 'App/Validators/Course/UpdateCourseValidator';
|
||||
|
||||
export default class CourseRepository {
|
||||
private Course: typeof Course;
|
||||
@ -126,4 +127,27 @@ export default class CourseRepository {
|
||||
|
||||
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 UserRepository from 'App/Repositories/UserRepository';
|
||||
import CreateCourseValidator from 'App/Validators/Course/CreateCourseValidator';
|
||||
import UpdateCourseValidator from 'App/Validators/Course/UpdateCourseValidator';
|
||||
|
||||
@inject()
|
||||
export default class CourseService {
|
||||
@ -219,9 +220,6 @@ export default class CourseService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check teacher on role
|
||||
*/
|
||||
const isTeacher = RoleHelper.userHasRoles(teacher.roles, [RoleEnum.TEACHER]);
|
||||
|
||||
if (!isTeacher) {
|
||||
@ -294,6 +292,36 @@ export default class CourseService {
|
||||
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);
|
||||
|
||||
@ -7,7 +7,6 @@ export default class UpdateCourseValidator {
|
||||
public schema = schema.create({
|
||||
title: 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' })]),
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user