Skip to content

Commit 16f7c36

Browse files
committed
integração com swagger-ui
1 parent 72e7cb2 commit 16f7c36

9 files changed

+90
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
- salvar cpf sem pontuação e retornar com pontuação [ok]
2121
- inserir filtro por bairro em /endereco (GET) [ok]
2222
- usar typeORM [ok]
23-
- usar docker [ToDo]
23+
- usar docker [ok]
2424

package-lock.json

+29-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
"@nestjs/common": "^7.0.0",
2525
"@nestjs/core": "^7.0.0",
2626
"@nestjs/platform-express": "^7.0.0",
27+
"@nestjs/swagger": "^4.6.1",
2728
"@nestjs/typeorm": "^7.1.4",
2829
"pg": "^8.4.0",
2930
"postgres": "^1.0.2",
3031
"reflect-metadata": "^0.1.13",
3132
"rimraf": "^3.0.2",
3233
"rxjs": "^6.5.4",
34+
"swagger-ui-express": "^4.1.4",
3335
"typeorm": "^0.2.28"
3436
},
3537
"devDependencies": {

src/aluno/aluno.controller.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common';
2+
import { ApiParam, ApiProperty, ApiResponse } from '@nestjs/swagger';
23
import { Endereco } from 'src/endereco/endereco';
34
import { Aluno } from './aluno';
45
import { AlunoService } from './aluno.service';
@@ -9,16 +10,19 @@ export class AlunoController {
910
constructor(private alunoService : AlunoService){}
1011

1112
@Post()
12-
async criar(@Body() aluno : Aluno, @Body() endereco : Endereco) {
13-
return await this.alunoService.criarAluno(aluno, endereco);
13+
@ApiResponse({description : "True ou False, dependendo se foi inserido ou não"})
14+
async criar(@Body() aluno : Aluno) {
15+
return await this.alunoService.criarAluno(aluno);
1416
}
1517

1618
@Put(':id')
19+
@ApiResponse({description : "True ou False, dependendo se foi inserido ou não"})
1720
async atualizar(@Param('id') id : number, @Body() aluno : Aluno){
1821
return await this.alunoService.atualizarAluno(id, aluno);
1922
}
2023

2124
@Get(':id')
25+
@ApiResponse({description : "Retorna o aluno, caso não exista retorna false.\n Se o parametro for 'media' retorna os alunos cuja nota é maior que a media de todos os alunos"})
2226
async getPorId(@Param('id') id : number){
2327
if(id.toString() === 'media'){
2428
return await this.alunoService.getAlunosPorMedia();
@@ -27,16 +31,19 @@ export class AlunoController {
2731
}
2832

2933
@Get()
34+
@ApiResponse({description : "Retorna a lista de alunos, caso de errado retorna false"})
3035
async getTodos(){
3136
return await this.alunoService.getAlunos();
3237
}
3338

3439
@Get(':id/endereco')
40+
@ApiResponse({description : "Retorna os endereços de um aluno"})
3541
async getEnderecos(@Param('id') id : number){
3642
return await this.alunoService.getAlunoEnderecos(id);
3743
}
3844

3945
@Get('/:nota/criterio/:criterio')
46+
@ApiResponse({description : "Retorna os alunos cuja nota é maior ou menor que o critério passado"})
4047
async getPorNota(@Param('nota') nota : number, @Param('criterio') criterio : string){
4148
return await this.alunoService.getAlunosPorNota(nota, criterio);
4249
}

src/aluno/aluno.service.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2+
import { ApiOperation, ApiProperty, ApiResponse } from '@nestjs/swagger';
23
import { InjectRepository } from '@nestjs/typeorm';
34
import { connectableObservableDescriptor } from 'rxjs/internal/observable/ConnectableObservable';
45
import { Endereco } from 'src/endereco/endereco';
@@ -16,8 +17,8 @@ export class AlunoService {
1617
@InjectRepository(EnderecoEntity)
1718
private enderecoRepository : Repository<EnderecoEntity>){}
1819

19-
20-
async criarAluno(aluno: Aluno, endereco : Endereco){
20+
21+
async criarAluno(aluno: Aluno){
2122
try{
2223
if(! await this.alunoRepository.findOne({CPF : aluno.CPF})){
2324
if(!this.validarCPF(aluno.CPF.toString())) {
@@ -35,10 +36,10 @@ export class AlunoService {
3536

3637
await this.alunoRepository.save(novoAluno);
3738

38-
novoEndereco.bairro = endereco.bairro;
39-
novoEndereco.complemento = endereco.complemento;
40-
novoEndereco.numero = endereco.numero;
41-
novoEndereco.rua = endereco.rua;
39+
novoEndereco.bairro = aluno.endereco.bairro;
40+
novoEndereco.complemento = aluno.endereco.complemento;
41+
novoEndereco.numero = aluno.endereco.numero;
42+
novoEndereco.rua = aluno.endereco.rua;
4243
novoEndereco.aluno = novoAluno;
4344

4445
await this.enderecoRepository.save(novoEndereco);
@@ -153,6 +154,7 @@ export class AlunoService {
153154

154155
}
155156

157+
@ApiResponse({description : "Retorna os alunos cuja nota é maior que a média de todos os alunos"})
156158
async getAlunosPorMedia(){
157159
try{
158160

@@ -181,7 +183,7 @@ export class AlunoService {
181183
}
182184
}
183185

184-
formatarCPF(aluno : Aluno){
186+
formatarCPF(aluno : AlunoEntity){
185187

186188
aluno.CPF = `${aluno.CPF.substring(0,3)}.${aluno.CPF.substring(3,6)}.${aluno.CPF.substring(6,9)}-${aluno.CPF.substring(9,11)}`;
187189

src/aluno/aluno.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
import { ApiProperty } from "@nestjs/swagger";
2+
import { Endereco } from "src/endereco/endereco";
3+
14
export class Aluno{
25
id: number;
6+
7+
@ApiProperty()
38
nome : string;
9+
10+
@ApiProperty()
411
dataNascimento: Date;
12+
13+
@ApiProperty()
514
CPF: string;
15+
16+
@ApiProperty()
617
nota: number;
18+
19+
@ApiProperty()
20+
endereco : Endereco;
721

822
}

src/endereco/endereco.controller.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Param, Controller, Get, Post, Body } from '@nestjs/common';
2+
import { ApiResponse } from '@nestjs/swagger';
23
import { Endereco } from './endereco';
34
import { EnderecoService } from './endereco.service';
45

@@ -9,16 +10,19 @@ export class EnderecoController {
910

1011

1112
@Post()
13+
@ApiResponse({description : "Retorna o endereço criado, caso contrário retorna false"})
1214
async create(@Body() endereco : Endereco){
1315
return await this.enderecoService.createEndereco(endereco);
1416
}
1517

1618
@Get()
19+
@ApiResponse({description : "Retorna todos os endereços "})
1720
async getEndereco(){
1821
return await this.enderecoService.getEnderecos();
1922
}
2023

2124
@Get(':bairro')
25+
@ApiResponse({description : "Retorna os endereços que pertecem a um bairro"})
2226
async getEnderecoPorBairro(@Param('bairro') bairro : string){
2327
return await this.enderecoService.getEnderecosPorBairro(bairro);
2428
}

src/endereco/endereco.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import { ApiProperty } from "@nestjs/swagger";
2+
13
export class Endereco{
24
id: number;
5+
6+
@ApiProperty()
37
rua: string;
8+
9+
@ApiProperty()
410
numero: string;
11+
12+
@ApiProperty()
513
complemento: string;
14+
15+
@ApiProperty()
616
bairro: string;
17+
718
alunoId: number;
819
}

src/main.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { NestFactory } from '@nestjs/core';
2+
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
23
import { AppModule } from './app.module';
34

45
async function bootstrap() {
56
const app = await NestFactory.create(AppModule);
7+
const options = new DocumentBuilder()
8+
.setTitle('Desafio Lemobs')
9+
.setDescription('Descrição da API')
10+
.setVersion('1.0')
11+
.addTag('Alunos')
12+
.build();
13+
14+
const document = SwaggerModule.createDocument(app,options);
15+
SwaggerModule.setup('api',app, document);
16+
617
await app.listen(3000);
718
}
819
bootstrap();

0 commit comments

Comments
 (0)