Skip to content

Commit

Permalink
Fixed Swagger GUI page
Browse files Browse the repository at this point in the history
  • Loading branch information
popov654 committed Apr 2, 2024
1 parent 3691b28 commit a6c3b60
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/article/article.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { User } from '../user/user.decorator';
import { IArticleRO, IArticlesRO, ICommentsRO } from './article.interface';
import { ArticleService } from './article.service';
Expand Down Expand Up @@ -38,18 +38,20 @@ export class ArticleController {
}

@ApiOperation({ summary: 'Create article' })
@ApiBody({ type: CreateArticleDto })
@ApiResponse({ status: 201, description: 'The article has been successfully created.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Post()
async create(@User('id') userId: number, @Body('article') articleData: CreateArticleDto) {
async create(@User('id') userId: number, @Body() articleData: CreateArticleDto) {
return this.articleService.create(userId, articleData);
}

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

@ApiOperation({ summary: 'Create comment' })
@ApiBody({ type: CreateCommentDto })
@ApiResponse({ status: 201, description: 'The comment has been successfully created.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Post(':slug/comments')
async createComment(@User('id') user: number, @Param('slug') slug, @Body('comment') commentData: CreateCommentDto) {
async createComment(@User('id') user: number, @Param('slug') slug, @Body() commentData: CreateCommentDto) {
return this.articleService.addComment(user, slug, commentData);
}

Expand Down
6 changes: 6 additions & 0 deletions src/article/dto/create-article.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';

export class CreateArticleDto {
@ApiProperty()
readonly title!: string;
@ApiProperty()
readonly description!: string;
@ApiProperty()
readonly body!: string;
@ApiProperty()
readonly tagList!: string[];
}
3 changes: 3 additions & 0 deletions src/article/dto/create-comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class CreateCommentDto {
@ApiProperty()
readonly body!: string;
}
4 changes: 4 additions & 0 deletions src/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator';

export class CreateUserDto {

@IsNotEmpty()
@ApiProperty()
readonly username!: string;

@IsNotEmpty()
@ApiProperty()
readonly email!: string;

@IsNotEmpty()
@ApiProperty()
readonly password!: string;
}
3 changes: 3 additions & 0 deletions src/user/dto/login-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator';

export class LoginUserDto {

@IsNotEmpty()
@ApiProperty()
readonly email!: string;

@IsNotEmpty()
@ApiProperty()
readonly password!: string;
}
6 changes: 6 additions & 0 deletions src/user/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';

export class UpdateUserDto {
@ApiProperty()
readonly bio!: string;
@ApiProperty()
readonly email!: string;
@ApiProperty()
readonly image!: string;
@ApiProperty()
readonly username!: string;
}
7 changes: 6 additions & 1 deletion src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { UserService } from './user.service';

import {
ApiBearerAuth,
ApiBody,
ApiTags,
getSchemaPath
} from '@nestjs/swagger';

@ApiBearerAuth()
Expand All @@ -28,8 +30,9 @@ export class UserController {
}

@UsePipes(new ValidationPipe())
@ApiBody({ schema: { type: 'object', items: { type: 'object', items: { $ref: getSchemaPath(CreateUserDto) } } } })
@Post('users')
async create(@Body('user') userData: CreateUserDto) {
async create(@Body() userData: CreateUserDto) {
return this.userService.create(userData);
}

Expand All @@ -39,6 +42,7 @@ export class UserController {
}

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

0 comments on commit a6c3b60

Please sign in to comment.