Skip to content

Commit

Permalink
Merge pull request #47 from Ibratbek/update-category
Browse files Browse the repository at this point in the history
feat: update category
  • Loading branch information
lambdajon authored Feb 24, 2022
2 parents 73cb95d + cc6146f commit 190a872
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
55 changes: 53 additions & 2 deletions src/modules/api/category/category-api.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { Body, Controller, HttpException, Post } from '@nestjs/common';
import {
Body,
Controller,
HttpException,
NotFoundException,
Param,
Post,
Put
} from '@nestjs/common';
import { CreateCategoryDTO } from '../../category/dto/CreateCategory';
import { CategoryService } from '../../category/category.service';
import { ApiBadRequestResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import {
ApiBadRequestResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiTags
} from '@nestjs/swagger';
import { UpdateCategoryDTO } from '../../category/dto/UpdateCategory';

@Controller('/categories')
@ApiTags('Categories')
Expand Down Expand Up @@ -30,4 +44,41 @@ export class CategoryDashboardController {

return this.categoryService.create(body);
}

@Put(':id')
@ApiOkResponse({ description: 'Ok' })
@ApiBadRequestResponse({
description: 'Bad Request',
schema: {
type: 'String',
example: {
statusCode: 400,
message: ['name must be a string'],
error: 'Bad Request'
}
}
})
@ApiNotFoundResponse({
description: 'Not Found',
schema: {
type: 'String',
example: {
statusCode: 404,
message: 'Category not found!',
error: 'Bad Request'
}
}
})
async updateCategory(
@Param('id') id: string,
@Body() body: UpdateCategoryDTO
) {
const category = await this.categoryService.findOne(id);

if (category === undefined) {
throw new NotFoundException('Category not found!');
}

return this.categoryService.update(id, body);
}
}
10 changes: 10 additions & 0 deletions src/modules/category/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common';
import { UpdateResult } from 'typeorm';
import { Category } from '../../entities/Category';
import { CategoryRepository } from './category.repository';
import { CreateCategoryDTO } from './dto/CreateCategory';
import { UpdateCategoryDTO } from './dto/UpdateCategory';

@Injectable()
export class CategoryService {
Expand All @@ -15,4 +17,12 @@ export class CategoryService {
findByName(name: string): Promise<Category> {
return this.categoryRepository.findOne({ name });
}

update(id: string, category: UpdateCategoryDTO): Promise<UpdateResult> {
return this.categoryRepository.update(id, category);
}

findOne(id: string): Promise<Category> {
return this.categoryRepository.findOne(id);
}
}
8 changes: 8 additions & 0 deletions src/modules/category/dto/UpdateCategory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class UpdateCategoryDTO {
@ApiProperty()
@IsString()
name: string;
}

0 comments on commit 190a872

Please sign in to comment.