Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add request body validation with DTOs & Swagger docs to the routes #96

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add req body validation & swagger docs to supply-categories routes
ocsoares committed May 14, 2024
commit 48a3e36bcf7fc0a506b77274282d22b0a3249dc4
9 changes: 9 additions & 0 deletions src/supply-categories/dtos/CreateSupplyCategoryDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class CreateSupplyCategoryDTO {
@ApiProperty({ type: 'string', example: 'Nome da categoria do suprimento' })
@IsNotEmpty()
@IsString()
readonly name = '';
}
13 changes: 13 additions & 0 deletions src/supply-categories/dtos/UpdateSupplyCategoryDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsOptional, IsString } from 'class-validator';

export class UpdateSupplyCategoryDTO {
@ApiProperty({
required: false,
type: 'string',
example: 'Nome da categoria do suprimento',
})
@IsOptional()
@IsString()
readonly name?: string;
}
25 changes: 22 additions & 3 deletions src/supply-categories/supply-categories.controller.ts
Original file line number Diff line number Diff line change
@@ -9,13 +9,23 @@ import {
Put,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiInternalServerErrorResponse,
ApiOkResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';

import { SupplyCategoriesService } from './supply-categories.service';
import { ServerResponse } from '../utils';
import { AdminGuard } from '@/guards/admin.guard';
import { CreateSupplyCategoryDTO } from './dtos/CreateSupplyCategoryDTO';
import { UpdateSupplyCategoryDTO } from './dtos/UpdateSupplyCategoryDTO';

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

@ApiOkResponse()
@Get('')
async index() {
try {
@@ -39,9 +50,13 @@ export class SupplyCategoriesController {
}
}

@ApiBearerAuth()
@ApiUnauthorizedResponse()
@ApiBadRequestResponse()
@ApiOkResponse()
@Post('')
@UseGuards(AdminGuard)
async store(@Body() body) {
async store(@Body() body: CreateSupplyCategoryDTO) {
try {
const data = await this.supplyCategoryServices.store(body);
return new ServerResponse(
@@ -55,9 +70,13 @@ export class SupplyCategoriesController {
}
}

@ApiBearerAuth()
@ApiUnauthorizedResponse()
@ApiBadRequestResponse()
@ApiOkResponse()
@Put(':id')
@UseGuards(AdminGuard)
async update(@Param('id') id: string, @Body() body) {
async update(@Param('id') id: string, @Body() body: UpdateSupplyCategoryDTO) {
try {
const data = await this.supplyCategoryServices.update(id, body);
return new ServerResponse(
7 changes: 4 additions & 3 deletions src/supply-categories/supply-categories.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Injectable } from '@nestjs/common';

import { PrismaService } from '../prisma/prisma.service';
import { z } from 'zod';
import {
CreateSupplyCategorySchema,
UpdateSupplyCategorySchema,
} from './types';
import { CreateSupplyCategoryDTO } from './dtos/CreateSupplyCategoryDTO';
import { UpdateSupplyCategoryDTO } from './dtos/UpdateSupplyCategoryDTO';

@Injectable()
export class SupplyCategoriesService {
constructor(private readonly prismaService: PrismaService) {}

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

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