140 lines
4.5 KiB
TypeScript
140 lines
4.5 KiB
TypeScript
import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put, UseInterceptors} from '@nestjs/common';
|
|
import {StoreService} from './store.service';
|
|
import {Store, StoreRequest} from './store.schema';
|
|
import {ApiResponse, ApiTags, ApiParam, ApiBody, ApiBearerAuth, ApiSecurity} from '@nestjs/swagger';
|
|
import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, COLLECTION_STORE} from 'src/consts';
|
|
import {Request} from 'express';
|
|
import {LoggingInterceptor} from 'src/logs/logging.interceptor';
|
|
import {
|
|
FIND_ALL_SUCCESS,
|
|
CREATE_SUCCESS,
|
|
CREATE_CONFLICT,
|
|
CREATE_NOT_VALID,
|
|
FIND_ONE_SUCCESS,
|
|
FIND_ONE_NOT_FOUND,
|
|
UPDATE_SUCCESS,
|
|
UPDATE_NOT_FOUND,
|
|
UPDATE_NOT_VALID,
|
|
REMOVE_SUCCESS,
|
|
REMOVE_NOT_FOUND,
|
|
} from './store.responses';
|
|
import {AuthService} from 'src/services/auth.service';
|
|
|
|
const prepareStoreToStoreRequest = ({
|
|
key, value, description, service_name, author
|
|
}: Store): StoreRequest => ({
|
|
key, value, description, service_name, author,
|
|
});
|
|
|
|
const makeApiHeader = (request: Request): string => {
|
|
const apiHeader = request.headers?.['api-name'];
|
|
return typeof apiHeader === 'string' ? apiHeader : '';
|
|
};
|
|
|
|
@ApiSecurity('apiKey')
|
|
@UseInterceptors(LoggingInterceptor)
|
|
@Controller(COLLECTION_STORE)
|
|
@ApiTags(COLLECTION_STORE)
|
|
export class StoreController {
|
|
constructor(
|
|
private readonly storeService: StoreService,
|
|
private readonly authService: AuthService,
|
|
) {}
|
|
|
|
|
|
@Get()
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@ApiResponse(FIND_ALL_SUCCESS)
|
|
async findAll(@Req() request: Request): Promise<StoreRequest[]> {
|
|
await this.authService.checkRequest(request);
|
|
|
|
const api = makeApiHeader(request);
|
|
const storeList = await this.storeService.findAll(api);
|
|
return storeList.map(prepareStoreToStoreRequest);
|
|
}
|
|
|
|
@Get(':key')
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@ApiResponse(FIND_ONE_SUCCESS)
|
|
@ApiResponse(FIND_ONE_NOT_FOUND)
|
|
@ApiParam({
|
|
name: 'key',
|
|
description: 'Ключ для поиска хранилища',
|
|
})
|
|
async findOne(@Req() request: Request<{key: string}>): Promise<StoreRequest> {
|
|
await this.authService.checkRequest(request);
|
|
|
|
const {key} = request.params;
|
|
const api = makeApiHeader(request);
|
|
const store = await this.storeService.findOne(api, key);
|
|
return prepareStoreToStoreRequest(store);
|
|
}
|
|
|
|
@Post()
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@ApiResponse(CREATE_SUCCESS)
|
|
@ApiResponse(CREATE_CONFLICT)
|
|
@ApiResponse(CREATE_NOT_VALID)
|
|
@ApiBody({
|
|
type: StoreRequest,
|
|
description: 'Объект для создания хранилища'
|
|
})
|
|
async create(@Req() request: Request<null, StoreRequest>): Promise<StoreRequest> {
|
|
const api = makeApiHeader(request);
|
|
const store = await this.storeService.create(api, request.body, request.headers.authorization);
|
|
return prepareStoreToStoreRequest(store);
|
|
}
|
|
|
|
@Put()
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@ApiResponse(UPDATE_SUCCESS)
|
|
@ApiResponse(UPDATE_NOT_FOUND)
|
|
@ApiResponse(UPDATE_NOT_VALID)
|
|
@ApiBody({
|
|
type: StoreRequest,
|
|
description: 'Объект для обновления хранилища'
|
|
})
|
|
async update(@Req() request: Request<null, StoreRequest>): Promise<StoreRequest> {
|
|
const api = makeApiHeader(request);
|
|
const store = await this.storeService.update(api, request.body);
|
|
return prepareStoreToStoreRequest(store);
|
|
}
|
|
|
|
@Delete(':key')
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@ApiResponse(REMOVE_SUCCESS)
|
|
@ApiResponse(REMOVE_NOT_FOUND)
|
|
@ApiParam({
|
|
name: 'key',
|
|
description: 'Ключ для удаления хранилища',
|
|
})
|
|
async removeOne(@Req() request: Request<{key: string}>): Promise<StoreRequest> {
|
|
const {key} = request.params;
|
|
const api = makeApiHeader(request);
|
|
const store = await this.storeService.removeOne(api, key);
|
|
return prepareStoreToStoreRequest(store);
|
|
}
|
|
|
|
@Options('')
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@Header(...ALLOW_METHOD)
|
|
@Header(...ALLOW_CREDENTIALS)
|
|
@Header(...CONTENT_LENGTH)
|
|
@Header(...ALLOW_HEADERS)
|
|
@HttpCode(204)
|
|
async options(): Promise<string> {
|
|
return '';
|
|
}
|
|
|
|
@Options(':key')
|
|
@Header(...ALLOW_ORIGIN_ALL)
|
|
@Header(...ALLOW_METHOD)
|
|
@Header(...ALLOW_CREDENTIALS)
|
|
@Header(...CONTENT_LENGTH)
|
|
@Header(...ALLOW_HEADERS)
|
|
@HttpCode(204)
|
|
async optionsKey(): Promise<string> {
|
|
return '';
|
|
}
|
|
}
|