users api

This commit is contained in:
vigdorov
2020-07-26 16:42:00 +03:00
parent 7c62c57f2d
commit baeded4a7a
6 changed files with 250 additions and 21 deletions

View File

@ -1,9 +1,10 @@
import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put, UseInterceptors, All} from '@nestjs/common';
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 {UserRequest} from './users.schema';
import {UserResponse, CreateUserRequest, UpdateUserRequest} from './users.schema';
import {Request} from 'express';
@Controller(USERS_CONTROLLER)
@ApiTags(USERS_CONTROLLER)
@ -17,12 +18,71 @@ export class UsersController {
@ApiResponse({
status: 200,
description: 'Список всех пользователей',
type: [UserRequest]
type: [UserResponse]
})
async findAll(): Promise<UserRequest[]> {
async findAll(): Promise<UserResponse[]> {
return this.userService.findAll();
}
@Get(':login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Получить пользователя по логину'
})
@ApiParam({
name: 'login',
description: 'Логин пользователя',
})
async findOne(@Req() request: Request<{login: string}>): Promise<UserResponse> {
return await this.userService.findOne(request.params.login);
}
@Post()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 201,
description: 'Создает пользователя в системе',
type: UserResponse,
})
@ApiBody({
type: CreateUserRequest,
description: 'Принимает объект для создания пользователя'
})
async createUser(@Req() request: Request<null, CreateUserRequest>): Promise<UserResponse> {
return await this.userService.create(request.body);
}
@Put()
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Обновляет данные пользователя',
type: UpdateUserRequest
})
@ApiBody({
type: UpdateUserRequest,
description: 'Принимает объект для обновления данных пользователя'
})
async updateUser(@Req() request: Request<null, UpdateUserRequest>): Promise<UserResponse> {
return await this.userService.update(request.body);
}
@Delete(':login')
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse({
status: 200,
description: 'Удаляет пользователя',
type: UpdateUserRequest,
})
@ApiParam({
name: 'login',
description: 'Логин пользователя',
})
async removeUser(@Req() request: Request<{login: string}>): Promise<UpdateUserRequest> {
return await this.userService.removeOne(request.params.login);
}
@Options()
@Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)