HM-98. Описаны все ошибки api для swagger'a

This commit is contained in:
vigdorov
2020-08-01 00:46:28 +03:00
parent 3711ef5f8f
commit b4e2ded4df
6 changed files with 237 additions and 66 deletions

View File

@ -1,10 +1,34 @@
import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put} from '@nestjs/common';
import {ApiResponse, ApiTags, ApiParam, ApiBody} from '@nestjs/swagger';
import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, USERS_CONTROLLER} from '../consts';
import {UserService} from './users.service';
import {UserResponse, CreateUserRequest, UpdateUserRequest} from './users.schema';
import {Request} from 'express';
import { ApiResponse, ApiTags, ApiParam, ApiBody } from '@nestjs/swagger';
import {
ALLOW_ORIGIN_ALL,
ALLOW_METHOD,
ALLOW_CREDENTIALS,
CONTENT_LENGTH,
ALLOW_HEADERS,
USERS_CONTROLLER,
} from '../consts';
import { UserService } from './users.service';
import {
UserResponse,
CreateUserRequest,
UpdateUserRequest,
} from './users.schema';
import { Request } from 'express';
import {
FIND_ALL_SUCCESS,
FIND_ONE_SUCCESS,
FIND_ONE_NOT_FOUND,
CREATE_SUCCESS,
CREATE_CONFLICT,
CREATE_NOT_VALID,
UPDATE_SUCCESS,
UPDATE_NOT_FOUND,
UPDATE_NOT_VALID,
REMOVE_SUCCESS,
REMOVE_NOT_FOUND,
} from './users.responses';
@Controller(USERS_CONTROLLER)
@ApiTags(USERS_CONTROLLER)
@ -15,21 +39,15 @@ export class UsersController {
@Get()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Список всех пользователей',
type: [UserResponse]
})
@ApiResponse(FIND_ALL_SUCCESS)
async findAll(): Promise<UserResponse[]> {
return this.userService.findAll();
}
@Get(':login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Получить пользователя по логину'
})
@ApiResponse(FIND_ONE_SUCCESS)
@ApiResponse(FIND_ONE_NOT_FOUND)
@ApiParam({
name: 'login',
description: 'Логин пользователя',
@ -40,14 +58,12 @@ export class UsersController {
@Post()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 201,
description: 'Создает пользователя в системе',
type: UserResponse,
})
@ApiResponse(CREATE_SUCCESS)
@ApiResponse(CREATE_CONFLICT)
@ApiResponse(CREATE_NOT_VALID)
@ApiBody({
type: CreateUserRequest,
description: 'Принимает объект для создания пользователя'
description: 'Объект для создания пользователя'
})
async createUser(@Req() request: Request<null, CreateUserRequest>): Promise<UserResponse> {
return await this.userService.create(request.body);
@ -55,14 +71,12 @@ export class UsersController {
@Put()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Обновляет данные пользователя',
type: UpdateUserRequest
})
@ApiResponse(UPDATE_SUCCESS)
@ApiResponse(UPDATE_NOT_FOUND)
@ApiResponse(UPDATE_NOT_VALID)
@ApiBody({
type: UpdateUserRequest,
description: 'Принимает объект для обновления данных пользователя'
description: 'Объект обновления данных пользователя'
})
async updateUser(@Req() request: Request<null, UpdateUserRequest>): Promise<UserResponse> {
return await this.userService.update(request.body);
@ -70,11 +84,8 @@ export class UsersController {
@Delete(':login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Удаляет пользователя',
type: UpdateUserRequest,
})
@ApiResponse(REMOVE_SUCCESS)
@ApiResponse(REMOVE_NOT_FOUND)
@ApiParam({
name: 'login',
description: 'Логин пользователя',