Skip to content

Commit a6c3b60

Browse files
committed
Fixed Swagger GUI page
1 parent 3691b28 commit a6c3b60

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

src/article/article.controller.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
2-
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
2+
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
33
import { User } from '../user/user.decorator';
44
import { IArticleRO, IArticlesRO, ICommentsRO } from './article.interface';
55
import { ArticleService } from './article.service';
@@ -38,18 +38,20 @@ export class ArticleController {
3838
}
3939

4040
@ApiOperation({ summary: 'Create article' })
41+
@ApiBody({ type: CreateArticleDto })
4142
@ApiResponse({ status: 201, description: 'The article has been successfully created.' })
4243
@ApiResponse({ status: 403, description: 'Forbidden.' })
4344
@Post()
44-
async create(@User('id') userId: number, @Body('article') articleData: CreateArticleDto) {
45+
async create(@User('id') userId: number, @Body() articleData: CreateArticleDto) {
4546
return this.articleService.create(userId, articleData);
4647
}
4748

4849
@ApiOperation({ summary: 'Update article' })
50+
@ApiBody({ type: CreateArticleDto })
4951
@ApiResponse({ status: 201, description: 'The article has been successfully updated.' })
5052
@ApiResponse({ status: 403, description: 'Forbidden.' })
5153
@Put(':slug')
52-
async update(@User('id') user: number, @Param() params, @Body('article') articleData: CreateArticleDto) {
54+
async update(@User('id') user: number, @Param() params, @Body() articleData: CreateArticleDto) {
5355
// Todo: update slug also when title gets changed
5456
return this.articleService.update(+user, params.slug, articleData);
5557
}
@@ -63,10 +65,11 @@ export class ArticleController {
6365
}
6466

6567
@ApiOperation({ summary: 'Create comment' })
68+
@ApiBody({ type: CreateCommentDto })
6669
@ApiResponse({ status: 201, description: 'The comment has been successfully created.' })
6770
@ApiResponse({ status: 403, description: 'Forbidden.' })
6871
@Post(':slug/comments')
69-
async createComment(@User('id') user: number, @Param('slug') slug, @Body('comment') commentData: CreateCommentDto) {
72+
async createComment(@User('id') user: number, @Param('slug') slug, @Body() commentData: CreateCommentDto) {
7073
return this.articleService.addComment(user, slug, commentData);
7174
}
7275

src/article/dto/create-article.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
13
export class CreateArticleDto {
4+
@ApiProperty()
25
readonly title!: string;
6+
@ApiProperty()
37
readonly description!: string;
8+
@ApiProperty()
49
readonly body!: string;
10+
@ApiProperty()
511
readonly tagList!: string[];
612
}

src/article/dto/create-comment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
13
export class CreateCommentDto {
4+
@ApiProperty()
25
readonly body!: string;
36
}

src/user/dto/create-user.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import { ApiProperty } from '@nestjs/swagger';
12
import { IsNotEmpty } from 'class-validator';
23

34
export class CreateUserDto {
45

56
@IsNotEmpty()
7+
@ApiProperty()
68
readonly username!: string;
79

810
@IsNotEmpty()
11+
@ApiProperty()
912
readonly email!: string;
1013

1114
@IsNotEmpty()
15+
@ApiProperty()
1216
readonly password!: string;
1317
}

src/user/dto/login-user.dto.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { ApiProperty } from '@nestjs/swagger';
12
import { IsNotEmpty } from 'class-validator';
23

34
export class LoginUserDto {
45

56
@IsNotEmpty()
7+
@ApiProperty()
68
readonly email!: string;
79

810
@IsNotEmpty()
11+
@ApiProperty()
912
readonly password!: string;
1013
}

src/user/dto/update-user.dto.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
13
export class UpdateUserDto {
4+
@ApiProperty()
25
readonly bio!: string;
6+
@ApiProperty()
37
readonly email!: string;
8+
@ApiProperty()
49
readonly image!: string;
10+
@ApiProperty()
511
readonly username!: string;
612
}

src/user/user.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { UserService } from './user.service';
77

88
import {
99
ApiBearerAuth,
10+
ApiBody,
1011
ApiTags,
12+
getSchemaPath
1113
} from '@nestjs/swagger';
1214

1315
@ApiBearerAuth()
@@ -28,8 +30,9 @@ export class UserController {
2830
}
2931

3032
@UsePipes(new ValidationPipe())
33+
@ApiBody({ schema: { type: 'object', items: { type: 'object', items: { $ref: getSchemaPath(CreateUserDto) } } } })
3134
@Post('users')
32-
async create(@Body('user') userData: CreateUserDto) {
35+
async create(@Body() userData: CreateUserDto) {
3336
return this.userService.create(userData);
3437
}
3538

@@ -39,6 +42,7 @@ export class UserController {
3942
}
4043

4144
@UsePipes(new ValidationPipe())
45+
@ApiBody({ schema: { type: 'object', items: { type: 'object', items: { $ref: getSchemaPath(LoginUserDto) } } } })
4246
@Post('users/login')
4347
async login(@Body('user') loginUserDto: LoginUserDto): Promise<IUserRO> {
4448
const foundUser = await this.userService.findOne(loginUserDto);
@@ -53,3 +57,4 @@ export class UserController {
5357
return { user };
5458
}
5559
}
60+

0 commit comments

Comments
 (0)