feat: category updating

This commit is contained in:
Sergey Yarkov 2022-01-19 23:09:29 +03:00
parent 175150a60a
commit bac861a09d
7 changed files with 91 additions and 4 deletions

View File

@ -7,9 +7,10 @@ import { Exception, inject, Ioc } from '@adonisjs/core/build/standalone';
import CategoryService from 'App/Services/CategoryService';
/**
* Validatoes
* Validators
*/
import CreateCategoryValidator from 'App/Validators/Category/CreateCategoryValidator';
import UpdateCategoryValidator from 'App/Validators/Category/UpdateCategoryValidator';
import BaseController from '../../BaseController';
@ -78,6 +79,21 @@ export default class CategoriesController extends BaseController {
return this.sendResponse(ctx, result.data, result.message, result.status);
}
/**
* Update category by id
* PATCH /categories/:id
*/
public async update(ctx: HttpContextContract) {
const payload = await ctx.request.validate(UpdateCategoryValidator);
const result = await this.categoryService.updateCategory(ctx.params.id, payload);
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(CategoriesController);

View File

@ -24,10 +24,10 @@ export default class Category extends BaseModel {
})
public color: BelongsTo<typeof Color>;
@column.dateTime({ autoCreate: true, serializeAs: null })
@column.dateTime({ autoCreate: true })
public createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true, serializeAs: null })
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime;
/**

View File

@ -7,6 +7,7 @@ import Category from 'App/Models/Category';
* Validators
*/
import CreateCategoryValidator from 'App/Validators/Category/CreateCategoryValidator';
import UpdateCategoryValidator from 'App/Validators/Category/UpdateCategoryValidator';
export default class CategoryRepository {
private Category: typeof Category;
@ -67,4 +68,27 @@ export default class CategoryRepository {
return null;
}
/**
*
* @param id Category id
* @param data
* @returns
*/
public async update(id: string | number, data: UpdateCategoryValidator['schema']['props']): Promise<Category | null> {
const category = await this.Category.query().where('id', id).first();
if (category) {
await category
.merge({
title: data.title,
description: data.description,
})
.save();
return category;
}
return null;
}
}

View File

@ -15,6 +15,7 @@ import CategoryRepository from 'App/Repositories/CategoryRepository';
* Validators
*/
import CreateCategoryValidator from 'App/Validators/Category/CreateCategoryValidator';
import UpdateCategoryValidator from 'App/Validators/Category/UpdateCategoryValidator';
@inject()
export default class CategoryService {
@ -114,6 +115,38 @@ export default class CategoryService {
data,
};
}
/**
* Update category
*
* @param id Category id
* @returns Updated category
*/
public async updateCategory(
id: string | number,
data: UpdateCategoryValidator['schema']['props']
): Promise<IResponse> {
const category = await this.categoryRepository.update(id, data);
if (!category) {
return {
success: false,
status: HttpStatusEnum.NOT_FOUND,
message: 'Category not found.',
data: {},
error: {
code: 'E_NOT_FOUND',
},
};
}
return {
success: true,
status: HttpStatusEnum.OK,
message: 'Category updated.',
data,
};
}
}
new Ioc().make(CategoryService);

View File

@ -6,7 +6,7 @@ export default class CreateCategoryValidator {
public schema = schema.create({
title: schema.string(),
description: schema.string(),
description: schema.string.optional(),
});
public messages = {};

View File

@ -0,0 +1,13 @@
import { schema } from '@ioc:Adonis/Core/Validator';
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
export default class UpdateCategoryValidator {
constructor(protected ctx: HttpContextContract) {}
public schema = schema.create({
title: schema.string.optional(),
description: schema.string.optional(),
});
public messages = {};
}

View File

@ -5,4 +5,5 @@ Route.group(() => {
Route.get('/:id', 'Api/v1/CategoriesController.show').middleware('role:admin,teacher,student').as('categories.show');
Route.post('/', 'Api/v1/CategoriesController.create').middleware('role:admin,teacher').as('categories.create');
Route.delete('/:id', 'Api/v1/CategoriesController.delete').middleware('role:admin,teacher').as('categories.delete');
Route.patch('/:id', 'Api/v1/CategoriesController.update').middleware('role:admin,teacher').as('categories.update');
}).prefix('categories');