Skip to content

Commit 3239cc7

Browse files
committed
feat: add req body validation & swagger docs to supplies routes
1 parent 63437e8 commit 3239cc7

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

src/supply/dtos/CreateSupplyDTO.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNotEmpty, IsString } from 'class-validator';
3+
4+
export class CreateSupplyDTO {
5+
@ApiProperty({ type: 'string', example: 'ID da Categoria do Suprimento' })
6+
@IsNotEmpty()
7+
@IsString()
8+
readonly supplyCategoryId = '';
9+
10+
@ApiProperty({ type: 'string', example: 'Nome do Suprimento' })
11+
@IsNotEmpty()
12+
@IsString()
13+
readonly name = '';
14+
}

src/supply/dtos/UpdateSupplyDTO.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsOptional, IsString } from 'class-validator';
3+
4+
export class UpdateSupplyDTO {
5+
@ApiProperty({
6+
required: false,
7+
type: 'string',
8+
example: 'ID da Categoria do Suprimento',
9+
})
10+
@IsOptional()
11+
@IsString()
12+
readonly supplyCategoryId?: string;
13+
14+
@ApiProperty({
15+
required: false,
16+
type: 'string',
17+
example: 'Nome do Suprimento',
18+
})
19+
@IsOptional()
20+
@IsString()
21+
readonly name?: string;
22+
}

src/supply/supply.controller.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@ import {
88
Post,
99
Put,
1010
} from '@nestjs/common';
11-
import { ApiTags } from '@nestjs/swagger';
11+
import {
12+
ApiBadRequestResponse,
13+
ApiInternalServerErrorResponse,
14+
ApiOkResponse,
15+
ApiTags,
16+
} from '@nestjs/swagger';
1217

1318
import { ServerResponse } from '../utils';
1419
import { SupplyService } from './supply.service';
20+
import { CreateSupplyDTO } from './dtos/CreateSupplyDTO';
21+
import { UpdateSupplyDTO } from './dtos/UpdateSupplyDTO';
1522

1623
@ApiTags('Suprimentos')
24+
@ApiInternalServerErrorResponse()
1725
@Controller('supplies')
1826
export class SupplyController {
1927
private logger = new Logger(SupplyController.name);
2028

2129
constructor(private readonly supplyServices: SupplyService) {}
2230

31+
@ApiOkResponse()
2332
@Get('')
2433
async index() {
2534
try {
@@ -31,8 +40,10 @@ export class SupplyController {
3140
}
3241
}
3342

43+
@ApiBadRequestResponse()
44+
@ApiOkResponse()
3445
@Post('')
35-
async store(@Body() body) {
46+
async store(@Body() body: CreateSupplyDTO) {
3647
try {
3748
const data = await this.supplyServices.store(body);
3849
return new ServerResponse(200, 'Successfully created supply', data);
@@ -42,8 +53,10 @@ export class SupplyController {
4253
}
4354
}
4455

56+
@ApiBadRequestResponse()
57+
@ApiOkResponse()
4558
@Put(':id')
46-
async update(@Param('id') id: string, @Body() body) {
59+
async update(@Param('id') id: string, @Body() body: UpdateSupplyDTO) {
4760
try {
4861
const data = await this.supplyServices.update(id, body);
4962
return new ServerResponse(200, 'Successfully updated supply', data);

src/supply/supply.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import z from 'zod';
21
import { Injectable } from '@nestjs/common';
32

43
import { PrismaService } from '../prisma/prisma.service';
54
import { CreateSupplySchema, UpdateSupplySchema } from './types';
5+
import { CreateSupplyDTO } from './dtos/CreateSupplyDTO';
6+
import { UpdateSupplyDTO } from './dtos/UpdateSupplyDTO';
67

78
@Injectable()
89
export class SupplyService {
910
constructor(private readonly prismaService: PrismaService) {}
1011

11-
async store(body: z.infer<typeof CreateSupplySchema>) {
12+
async store(body: CreateSupplyDTO) {
1213
const payload = CreateSupplySchema.parse(body);
1314
return await this.prismaService.supply.create({
1415
data: {
@@ -18,7 +19,7 @@ export class SupplyService {
1819
});
1920
}
2021

21-
async update(id: string, body: z.infer<typeof UpdateSupplySchema>) {
22+
async update(id: string, body: UpdateSupplyDTO) {
2223
const payload = UpdateSupplySchema.parse(body);
2324
await this.prismaService.supply.update({
2425
where: {

0 commit comments

Comments
 (0)