diff --git a/e2e/Conduit.postman_collection.json b/e2e/Conduit.postman_collection.json index 82e3b3eb..ba6eea39 100644 --- a/e2e/Conduit.postman_collection.json +++ b/e2e/Conduit.postman_collection.json @@ -49,7 +49,7 @@ ], "body": { "mode": "raw", - "raw": "{\"user\":{\"email\":\"{{EMAIL}}\", \"password\":\"{{PASSWORD}}\", \"username\":\"{{USERNAME}}\"}}" + "raw": "{\"email\":\"{{EMAIL}}\", \"password\":\"{{PASSWORD}}\", \"username\":\"{{USERNAME}}\"}" }, "url": { "raw": "{{APIURL}}/users", @@ -101,7 +101,7 @@ ], "body": { "mode": "raw", - "raw": "{\"user\":{\"email\":\"{{EMAIL}}\", \"password\":\"{{PASSWORD}}\"}}" + "raw": "{\"email\":\"{{EMAIL}}\", \"password\":\"{{PASSWORD}}\"}" }, "url": { "raw": "{{APIURL}}/users/login", @@ -161,7 +161,7 @@ ], "body": { "mode": "raw", - "raw": "{\"user\":{\"email\":\"{{EMAIL}}\", \"password\":\"{{PASSWORD}}\"}}" + "raw": "{\"email\":\"{{EMAIL}}\", \"password\":\"{{PASSWORD}}\"}" }, "url": { "raw": "{{APIURL}}/users/login", @@ -662,7 +662,7 @@ ], "body": { "mode": "raw", - "raw": "{\"article\":{\"title\":\"How to train your dragon\", \"description\":\"Ever wonder how?\", \"body\":\"Very carefully.\", \"tagList\":[\"dragons\",\"training\"]}}" + "raw": "{\"title\":\"How to train your dragon\", \"description\":\"Ever wonder how?\", \"body\":\"Very carefully.\", \"tagList\":[\"dragons\",\"training\"]}" }, "url": { "raw": "{{APIURL}}/articles", @@ -1641,7 +1641,7 @@ ], "body": { "mode": "raw", - "raw": "{\"comment\":{\"body\":\"Thank you so much!\"}}" + "raw": "{\"body\":\"Thank you so much!\"}" }, "url": { "raw": "{{APIURL}}/articles/{{slug}}/comments", @@ -1864,7 +1864,7 @@ ], "body": { "mode": "raw", - "raw": "{\"user\":{\"email\":\"celeb_{{EMAIL}}\", \"password\":\"{{PASSWORD}}\", \"username\":\"celeb_{{USERNAME}}\"}}" + "raw": "{\"email\":\"celeb_{{EMAIL}}\", \"password\":\"{{PASSWORD}}\", \"username\":\"celeb_{{USERNAME}}\"}" }, "url": { "raw": "{{APIURL}}/users", diff --git a/e2e/swagger.json b/e2e/swagger.json index 008ddfcd..f527e55f 100644 --- a/e2e/swagger.json +++ b/e2e/swagger.json @@ -422,7 +422,7 @@ "operationId": "CreateArticle", "parameters": [ { - "name": "article", + "name": "body", "in": "body", "required": true, "description": "Article to create", @@ -848,13 +848,8 @@ "NewUserRequest": { "type": "object", "properties": { - "user": { - "$ref": "#/definitions/NewUser" - } - }, - "required": [ - "user" - ] + "$ref": "#/definitions/NewUser" + } }, "User": { "type": "object", @@ -886,13 +881,8 @@ "UserResponse": { "type": "object", "properties": { - "user": { - "$ref": "#/definitions/User" - } - }, - "required": [ - "user" - ] + "$ref": "#/definitions/User" + } }, "UpdateUser": { "type": "object", @@ -1068,13 +1058,8 @@ "NewArticleRequest": { "type": "object", "properties": { - "article": { - "$ref": "#/definitions/NewArticle" - } - }, - "required": [ - "article" - ] + "$ref": "#/definitions/NewArticle" + } }, "UpdateArticle": { "type": "object", diff --git a/src/article/article.controller.ts b/src/article/article.controller.ts index d9f2f403..634bc149 100644 --- a/src/article/article.controller.ts +++ b/src/article/article.controller.ts @@ -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'; @@ -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); } @@ -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); } diff --git a/src/article/dto/create-article.dto.ts b/src/article/dto/create-article.dto.ts index 57927517..3ec02133 100644 --- a/src/article/dto/create-article.dto.ts +++ b/src/article/dto/create-article.dto.ts @@ -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[]; } diff --git a/src/article/dto/create-comment.ts b/src/article/dto/create-comment.ts index 000773e4..57ae61db 100644 --- a/src/article/dto/create-comment.ts +++ b/src/article/dto/create-comment.ts @@ -1,3 +1,6 @@ +import { ApiProperty } from '@nestjs/swagger'; + export class CreateCommentDto { + @ApiProperty() readonly body!: string; } diff --git a/src/user/dto/create-user.dto.ts b/src/user/dto/create-user.dto.ts index addfb52b..c4f6e414 100644 --- a/src/user/dto/create-user.dto.ts +++ b/src/user/dto/create-user.dto.ts @@ -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; } diff --git a/src/user/dto/login-user.dto.ts b/src/user/dto/login-user.dto.ts index f47075df..96ba7a38 100644 --- a/src/user/dto/login-user.dto.ts +++ b/src/user/dto/login-user.dto.ts @@ -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; } diff --git a/src/user/dto/update-user.dto.ts b/src/user/dto/update-user.dto.ts index 1f400056..6add6ce0 100644 --- a/src/user/dto/update-user.dto.ts +++ b/src/user/dto/update-user.dto.ts @@ -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; } diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index fb969440..95290f0f 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -7,7 +7,9 @@ import { UserService } from './user.service'; import { ApiBearerAuth, + ApiBody, ApiTags, + getSchemaPath } from '@nestjs/swagger'; @ApiBearerAuth() @@ -28,8 +30,9 @@ export class UserController { } @UsePipes(new ValidationPipe()) + @ApiBody({ type: CreateUserDto }) @Post('users') - async create(@Body('user') userData: CreateUserDto) { + async create(@Body() userData: CreateUserDto) { return this.userService.create(userData); } @@ -39,8 +42,9 @@ export class UserController { } @UsePipes(new ValidationPipe()) + @ApiBody({ type: LoginUserDto }) @Post('users/login') - async login(@Body('user') loginUserDto: LoginUserDto): Promise { + async login(@Body() loginUserDto: LoginUserDto): Promise { const foundUser = await this.userService.findOne(loginUserDto); const errors = { User: ' not found' }; @@ -53,3 +57,4 @@ export class UserController { return { user }; } } +