From cf80f7995c350a3bd9e15141e2bedd767ea874e3 Mon Sep 17 00:00:00 2001 From: Ibrat Date: Mon, 28 Feb 2022 10:14:51 +0500 Subject: [PATCH] feat: delete category --- .../api/category/category-api.controller.ts | 24 +++++++++++++++++++ src/modules/category/category.service.ts | 6 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/modules/api/category/category-api.controller.ts b/src/modules/api/category/category-api.controller.ts index 28d4087..ff0e94d 100644 --- a/src/modules/api/category/category-api.controller.ts +++ b/src/modules/api/category/category-api.controller.ts @@ -1,6 +1,7 @@ import { Body, Controller, + Delete, HttpException, NotFoundException, Param, @@ -81,4 +82,27 @@ export class CategoryDashboardController { return this.categoryService.update(id, body); } + + @ApiOkResponse({ description: 'OK' }) + @ApiNotFoundResponse({ + description: 'Not Found', + schema: { + type: 'String', + example: { + statusCode: 404, + message: 'Category not found!', + error: 'Bad Request' + } + } + }) + @Delete(':id') + async deleteCategory(@Param('id') id: string) { + const category = await this.categoryService.findOne(id); + + if (category === undefined) { + throw new NotFoundException('Category not found!'); + } + + return this.categoryService.delete(id); + } } diff --git a/src/modules/category/category.service.ts b/src/modules/category/category.service.ts index 7506b02..5251152 100644 --- a/src/modules/category/category.service.ts +++ b/src/modules/category/category.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { UpdateResult } from 'typeorm'; +import { DeleteResult, UpdateResult } from 'typeorm'; import { Category } from '../../entities/Category'; import { CategoryRepository } from './category.repository'; import { CreateCategoryDTO } from './dto/CreateCategory'; @@ -22,6 +22,10 @@ export class CategoryService { return this.categoryRepository.update(id, category); } + delete(id: string): Promise { + return this.categoryRepository.delete(id); + } + findOne(id: string): Promise { return this.categoryRepository.findOne(id); }