Skip to content

Commit d202126

Browse files
committed
feat: add req body validation & swagger docs to supply-categories routes
1 parent 89fdf44 commit d202126

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNotEmpty, IsString } from 'class-validator';
3+
4+
export class CreateSupplyCategoryDTO {
5+
@ApiProperty({ type: 'string', example: 'Nome da categoria do suprimento' })
6+
@IsNotEmpty()
7+
@IsString()
8+
readonly name = '';
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsOptional, IsString } from 'class-validator';
3+
4+
export class UpdateSupplyCategoryDTO {
5+
@ApiProperty({
6+
required: false,
7+
type: 'string',
8+
example: 'Nome da categoria do suprimento',
9+
})
10+
@IsOptional()
11+
@IsString()
12+
readonly name?: string;
13+
}

src/supply-categories/supply-categories.controller.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ import {
99
Put,
1010
UseGuards,
1111
} from '@nestjs/common';
12-
import { ApiTags } from '@nestjs/swagger';
12+
import {
13+
ApiBadRequestResponse,
14+
ApiBearerAuth,
15+
ApiInternalServerErrorResponse,
16+
ApiOkResponse,
17+
ApiTags,
18+
ApiUnauthorizedResponse,
19+
} from '@nestjs/swagger';
1320

1421
import { SupplyCategoriesService } from './supply-categories.service';
1522
import { ServerResponse } from '../utils';
1623
import { AdminGuard } from '@/guards/admin.guard';
24+
import { CreateSupplyCategoryDTO } from './dtos/CreateSupplyCategoryDTO';
25+
import { UpdateSupplyCategoryDTO } from './dtos/UpdateSupplyCategoryDTO';
1726

1827
@ApiTags('Categoria de Suprimentos')
28+
@ApiInternalServerErrorResponse()
1929
@Controller('supply-categories')
2030
export class SupplyCategoriesController {
2131
private logger = new Logger(SupplyCategoriesController.name);
@@ -24,6 +34,7 @@ export class SupplyCategoriesController {
2434
private readonly supplyCategoryServices: SupplyCategoriesService,
2535
) {}
2636

37+
@ApiOkResponse()
2738
@Get('')
2839
async index() {
2940
try {
@@ -39,9 +50,13 @@ export class SupplyCategoriesController {
3950
}
4051
}
4152

53+
@ApiBearerAuth()
54+
@ApiUnauthorizedResponse()
55+
@ApiBadRequestResponse()
56+
@ApiOkResponse()
4257
@Post('')
4358
@UseGuards(AdminGuard)
44-
async store(@Body() body) {
59+
async store(@Body() body: CreateSupplyCategoryDTO) {
4560
try {
4661
const data = await this.supplyCategoryServices.store(body);
4762
return new ServerResponse(
@@ -55,9 +70,13 @@ export class SupplyCategoriesController {
5570
}
5671
}
5772

73+
@ApiBearerAuth()
74+
@ApiUnauthorizedResponse()
75+
@ApiBadRequestResponse()
76+
@ApiOkResponse()
5877
@Put(':id')
5978
@UseGuards(AdminGuard)
60-
async update(@Param('id') id: string, @Body() body) {
79+
async update(@Param('id') id: string, @Body() body: UpdateSupplyCategoryDTO) {
6180
try {
6281
const data = await this.supplyCategoryServices.update(id, body);
6382
return new ServerResponse(

src/supply-categories/supply-categories.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { Injectable } from '@nestjs/common';
22

33
import { PrismaService } from '../prisma/prisma.service';
4-
import { z } from 'zod';
54
import {
65
CreateSupplyCategorySchema,
76
UpdateSupplyCategorySchema,
87
} from './types';
8+
import { CreateSupplyCategoryDTO } from './dtos/CreateSupplyCategoryDTO';
9+
import { UpdateSupplyCategoryDTO } from './dtos/UpdateSupplyCategoryDTO';
910

1011
@Injectable()
1112
export class SupplyCategoriesService {
1213
constructor(private readonly prismaService: PrismaService) {}
1314

14-
async store(body: z.infer<typeof CreateSupplyCategorySchema>) {
15+
async store(body: CreateSupplyCategoryDTO) {
1516
const payload = CreateSupplyCategorySchema.parse(body);
1617
await this.prismaService.supplyCategory.create({
1718
data: {
@@ -21,7 +22,7 @@ export class SupplyCategoriesService {
2122
});
2223
}
2324

24-
async update(id: string, body: z.infer<typeof UpdateSupplyCategorySchema>) {
25+
async update(id: string, body: UpdateSupplyCategoryDTO) {
2526
const payload = UpdateSupplyCategorySchema.parse(body);
2627
await this.prismaService.supplyCategory.update({
2728
where: {

0 commit comments

Comments
 (0)