This commit is contained in:
Николай Вигдоров
2025-03-02 12:13:01 +03:00
parent 12b3d5b23c
commit 5e91f028d9
7 changed files with 200 additions and 12 deletions

View File

@ -1,15 +1,24 @@
import {
Controller,
Delete,
Get,
Header,
HttpCode,
Options,
Post,
Put,
Req,
} from '@nestjs/common';
import { Request } from 'express';
import { AppService } from './app.service';
import { ApiBody, ApiResponse, ApiSecurity, ApiTags } from '@nestjs/swagger';
import {
ApiBody,
ApiExcludeEndpoint,
ApiParam,
ApiResponse,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import {
ALLOW_CREDENTIALS,
ALLOW_HEADERS,
@ -20,10 +29,11 @@ import {
import {
AUTH_ERROR,
AUTH_SUCCESS,
MANIPULATE_STORAGE_SUCCESS,
GET_STORAGES_LIST_SUCCESS,
} from './api.responses';
import { AuthRequest } from './schemas';
import { Storage } from './types';
import { AuthRequest, StorageCreateRequest } from './schemas';
import { Storage, StorageCreate, StorageList, StorageUpdate } from './types';
@Controller()
@ApiTags('storage-app')
@ -48,14 +58,89 @@ export class AppController {
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(GET_STORAGES_LIST_SUCCESS)
@ApiResponse(AUTH_ERROR)
async getStorageList(@Req() request: Request): Promise<Storage[]> {
async getStorageList(@Req() request: Request): Promise<StorageList> {
const { login } = await this.appService.checkRequest(
request.headers.authorization,
);
return this.appService.getStorageList(login);
}
@Options(['', '/auth'])
@Get('/storages/:id')
@ApiSecurity('apiKey')
@ApiParam({
name: 'id',
description: 'id storage',
})
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(MANIPULATE_STORAGE_SUCCESS)
@ApiResponse(AUTH_ERROR)
async getImageById(
@Req() request: Request<{ id: string }>,
): Promise<Storage> {
const { login } = await this.appService.checkRequest(
request.headers.authorization,
);
return this.appService.getStorageById(login, request.params.id);
}
@Post('/storages')
@ApiSecurity('apiKey')
@ApiBody({
type: StorageCreateRequest,
description: 'Объект создания storage',
})
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(MANIPULATE_STORAGE_SUCCESS)
@ApiResponse(AUTH_ERROR)
async createImage(
@Req() request: Request<null, null, StorageCreate>,
): Promise<Storage> {
const { login } = await this.appService.checkRequest(
request.headers.authorization,
);
return this.appService.addStorage(login, request.body);
}
@Put('/storages/:id')
@ApiSecurity('apiKey')
@ApiParam({
name: 'id',
description: 'id storage',
})
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(MANIPULATE_STORAGE_SUCCESS)
@ApiResponse(AUTH_ERROR)
async toggleLike(
@Req() request: Request<{ id: string }, null, StorageUpdate>,
): Promise<Storage> {
const { login } = await this.appService.checkRequest(
request.headers.authorization,
);
return this.appService.updateStorage(
login,
request.params.id,
request.body,
);
}
@Delete('/storages/:id')
@ApiSecurity('apiKey')
@ApiParam({
name: 'id',
description: 'id картинки',
})
@Header(...ALLOW_ORIGIN_ALL)
@ApiResponse(MANIPULATE_STORAGE_SUCCESS)
@ApiResponse(AUTH_ERROR)
async deleteImage(@Req() request: Request<{ id: string }>): Promise<Storage> {
const { login } = await this.appService.checkRequest(
request.headers.authorization,
);
return this.appService.deleteStorageById(login, request.params.id);
}
@ApiExcludeEndpoint()
@Options(['', '/auth', '/storages', '/storages/:id'])
@Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)