From 8225dce0c503d44287a5b86658ee0fcf2c2adbca Mon Sep 17 00:00:00 2001 From: vigdorov Date: Mon, 19 Apr 2021 08:19:17 +0300 Subject: [PATCH] add users point --- src/api.responses.ts | 7 +++++++ src/app.controller.ts | 21 +++++++++++++++++++-- src/app.service.ts | 10 ++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/api.responses.ts b/src/api.responses.ts index 3b06588..c605207 100644 --- a/src/api.responses.ts +++ b/src/api.responses.ts @@ -25,6 +25,13 @@ export const GET_IMAGE_LIST_SUCCESS: ApiResponseOptions = { isArray: true }; +export const GET_USER_LIST_SUCCESS: ApiResponseOptions = { + status: 200, + description: 'Список всех пользователей', + type: String, + isArray: true +}; + export const MANIPULATE_IMAGE_SUCCESS: ApiResponseOptions = { status: 200, description: 'Картинка', diff --git a/src/app.controller.ts b/src/app.controller.ts index be4e1a3..7795a49 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -2,10 +2,10 @@ import {Controller, Delete, Get, Header, HttpCode, Options, Post, Put, Req} from import {AppService} from './app.service'; import {AuthRequest, ImageCreateRequest} from './schemas'; import {Request} from 'express'; -import {ApiBody, ApiParam, ApiResponse, ApiSecurity, ApiTags} from '@nestjs/swagger'; +import {ApiBody, ApiParam, ApiQuery, ApiResponse, ApiSecurity, ApiTags} from '@nestjs/swagger'; import {Image, ImageCreate} from './types'; import {ALLOW_CREDENTIALS, ALLOW_HEADERS, ALLOW_METHOD, ALLOW_ORIGIN_ALL, CONTENT_LENGTH} from './consts'; -import {AUTH_ERROR, AUTH_SUCCESS, GET_IMAGE_LIST_SUCCESS, MANIPULATE_IMAGE_SUCCESS} from './api.responses'; +import {AUTH_ERROR, AUTH_SUCCESS, GET_IMAGE_LIST_SUCCESS, GET_USER_LIST_SUCCESS, MANIPULATE_IMAGE_SUCCESS} from './api.responses'; @Controller() @ApiTags('image-app') @@ -37,6 +37,23 @@ export class AppController { return this.appService.getImageList(); } + @Get('/users') + @ApiSecurity('apiKey') + @Header(...ALLOW_ORIGIN_ALL) + @ApiQuery({ + name: 'login', + description: 'Часть логина пользователя', + required: false, + }) + @ApiResponse(GET_USER_LIST_SUCCESS) + @ApiResponse(AUTH_ERROR) + async getUserList( + @Req() request: Request + ): Promise { + await this.appService.checkRequest(request.headers.authorization); + return this.appService.getUserList(request.query.login ?? ''); + } + @Get('/list/:id') @ApiSecurity('apiKey') @ApiParam({ diff --git a/src/app.service.ts b/src/app.service.ts index eac2c7e..20801f5 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -34,6 +34,16 @@ export class AppService { })); } + async getUserList(searchLogin: string): Promise { + const authorList = await this.authorModel.find().exec(); + return authorList.reduce((acc, {login}) => { + if (login.includes(searchLogin)) { + acc.push(login); + } + return acc; + }, []); + } + async authUser(login: string): Promise { const authorList = await this.authorModel.find().exec(); const searchAuthor = authorList.find((author) => author.login === login);