Skip to content

Commit 5e16ded

Browse files
authored
Merge pull request #112 from boostcampwm-2024/feature-be-#106
swagger 적용 완료
2 parents ae74a35 + 9b15d3e commit 5e16ded

15 files changed

+256
-18
lines changed

backend/db_name

36 KB
Binary file not shown.

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@nestjs/core": "^10.0.0",
2626
"@nestjs/mapped-types": "*",
2727
"@nestjs/platform-express": "^10.0.0",
28+
"@nestjs/swagger": "^8.0.5",
2829
"@nestjs/typeorm": "^10.0.2",
2930
"class-transformer": "^0.5.1",
3031
"class-validator": "^0.14.1",

backend/src/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { NestFactory } from '@nestjs/core';
22
import { AppModule } from './app.module';
33
import { HttpExceptionFilter } from './filter/http-exception.filter';
4+
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
45

56
async function bootstrap() {
67
const app = await NestFactory.create(AppModule);
8+
9+
const config = new DocumentBuilder()
10+
.setTitle('OctoDocs')
11+
.setDescription('OctoDocs API 명세서')
12+
.build();
13+
const documentFactory = () => SwaggerModule.createDocument(app, config);
14+
SwaggerModule.setup('api', app, documentFactory);
715
app.useGlobalFilters(new HttpExceptionFilter());
816
await app.listen(3000);
917
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString, IsNumber, IsObject } from 'class-validator';
3+
class Coordinate {
4+
@IsNumber()
5+
x: number;
6+
@IsNumber()
7+
y: number;
8+
}
9+
export class CoordinateResponseDto {
10+
@ApiProperty({
11+
example: 'OO 생성에 성공했습니다.',
12+
description: 'api 요청 결과 메시지',
13+
})
14+
@IsString()
15+
message: string;
16+
17+
@ApiProperty({
18+
example: {
19+
x: 14,
20+
y: 14,
21+
},
22+
description: 'api 요청 결과 메시지',
23+
})
24+
@IsObject()
25+
coordinate: Coordinate;
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
import { ApiProperty } from '@nestjs/swagger';
12
import { IsString, IsNumber } from 'class-validator';
23

34
export class CreateNodeDto {
5+
@ApiProperty({
6+
example: '노드 제목',
7+
description: '노드 제목',
8+
})
49
@IsString()
510
title: string;
611

12+
@ApiProperty({
13+
example: '14',
14+
description: 'x 좌표입니다.',
15+
})
716
@IsNumber()
817
x: number;
918

19+
@ApiProperty({
20+
example: '14',
21+
description: 'y 좌표입니다.',
22+
})
1023
@IsNumber()
1124
y: number;
1225
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString } from 'class-validator';
3+
4+
export class MessageResponseDto {
5+
@ApiProperty({
6+
example: 'OO 생성에 성공했습니다.',
7+
description: 'api 요청 결과 메시지',
8+
})
9+
@IsString()
10+
message: string;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
import { IsString, IsNumber } from 'class-validator';
2+
import { ApiProperty } from '@nestjs/swagger';
23

34
export class UpdateNodeDto {
5+
@ApiProperty({
6+
example: '노드 제목',
7+
description: '노드 제목',
8+
})
49
@IsString()
510
title: string;
611

12+
@ApiProperty({
13+
example: '14',
14+
description: 'x 좌표입니다.',
15+
})
716
@IsNumber()
817
x: number;
918

19+
@ApiProperty({
20+
example: '14',
21+
description: 'y 좌표입니다.',
22+
})
1023
@IsNumber()
1124
y: number;
1225
}

backend/src/node/node.controller.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
import { NodeService } from './node.service';
1414
import { CreateNodeDto } from './dtos/createNode.dto';
1515
import { UpdateNodeDto } from './dtos/updateNode.dto';
16+
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
17+
import { MessageResponseDto } from './dtos/messageResponse.dto';
18+
import { CoordinateResponseDto } from './dtos/coordinateResponse.dto';
1619

1720
export enum NodeResponseMessage {
1821
NODE_CREATED = '노드와 페이지를 생성했습니다.',
@@ -25,6 +28,10 @@ export enum NodeResponseMessage {
2528
export class NodeController {
2629
constructor(private readonly nodeService: NodeService) {}
2730

31+
@ApiResponse({
32+
type: MessageResponseDto,
33+
})
34+
@ApiOperation({ summary: '노드를 생성하면서 페이지도 함께 생성합니다.' })
2835
@Post('/')
2936
@HttpCode(HttpStatus.CREATED)
3037
async createNode(@Body() body: CreateNodeDto): Promise<{ message: string }> {
@@ -33,7 +40,12 @@ export class NodeController {
3340
message: NodeResponseMessage.NODE_CREATED,
3441
};
3542
}
36-
43+
@ApiResponse({
44+
type: MessageResponseDto,
45+
})
46+
@ApiOperation({
47+
summary: '노드를 삭제하면서 페이지도 삭제합니다. (delete: cascade)',
48+
})
3749
@Delete('/:id')
3850
@HttpCode(HttpStatus.OK)
3951
async deleteNode(@Param('id') id: number): Promise<{ message: string }> {
@@ -42,7 +54,10 @@ export class NodeController {
4254
message: NodeResponseMessage.NODE_DELETED,
4355
};
4456
}
45-
57+
@ApiResponse({
58+
type: MessageResponseDto,
59+
})
60+
@ApiOperation({ summary: '노드의 제목, 좌표를 수정합니다.' })
4661
@Patch('/:id')
4762
@HttpCode(HttpStatus.OK)
4863
async updateNode(
@@ -54,7 +69,10 @@ export class NodeController {
5469
message: NodeResponseMessage.NODE_UPDATED,
5570
};
5671
}
57-
72+
@ApiResponse({
73+
type: CoordinateResponseDto,
74+
})
75+
@ApiOperation({ summary: '노드의 좌표를 가져옵니다.' })
5876
@Get(':id/coordinates')
5977
@HttpCode(HttpStatus.OK)
6078
async getCoordinates(@Param('id', ParseIntPipe) id: number) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1+
import { ApiProperty } from '@nestjs/swagger';
12
import { IsString, IsNumber, IsJSON } from 'class-validator';
23

34
export class CreatePageDto {
5+
@ApiProperty({
6+
example: 'nest.js 사용법',
7+
description: '페이지 제목입니다.',
8+
})
49
@IsString()
510
title: string;
611

12+
@ApiProperty({
13+
example: 'nest를 설치합니다.',
14+
description: '페이지 내용입니다.',
15+
})
716
@IsJSON()
817
content: JSON;
918

19+
@ApiProperty({
20+
example: '14',
21+
description: 'x 좌표입니다.',
22+
})
1023
@IsNumber()
1124
x: number;
1225

26+
@ApiProperty({
27+
example: '14',
28+
description: 'y 좌표입니다.',
29+
})
1330
@IsNumber()
1431
y: number;
1532
}
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 { IsString, IsArray, IsObject } from 'class-validator';
3+
import { Page } from '../page.entity';
4+
5+
export class FindPageResponseDto {
6+
@ApiProperty({
7+
example: 'OO 생성에 성공했습니다.',
8+
description: 'api 요청 결과 메시지',
9+
})
10+
@IsString()
11+
message: string;
12+
13+
@ApiProperty({
14+
example: {
15+
type: 'doc',
16+
content: {},
17+
},
18+
description: '모든 Page 배열',
19+
})
20+
@IsObject()
21+
page: Partial<Page>;
22+
}

0 commit comments

Comments
 (0)