Skip to content

Commit

Permalink
Merge pull request #48 from Ibratbek/delete-category
Browse files Browse the repository at this point in the history
feat: delete category
  • Loading branch information
lambdajon authored Mar 2, 2022
2 parents 190a872 + cf80f79 commit 0d791e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/modules/api/category/category-api.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
HttpException,
NotFoundException,
Param,
Expand Down Expand Up @@ -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);
}
}
6 changes: 5 additions & 1 deletion src/modules/category/category.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,6 +22,10 @@ export class CategoryService {
return this.categoryRepository.update(id, category);
}

delete(id: string): Promise<DeleteResult> {
return this.categoryRepository.delete(id);
}

findOne(id: string): Promise<Category> {
return this.categoryRepository.findOne(id);
}
Expand Down

0 comments on commit 0d791e8

Please sign in to comment.