From 8dc8b896150236142a907834922bffe8454f0fd9 Mon Sep 17 00:00:00 2001 From: JensForstmann <19289807+JensForstmann@users.noreply.github.com> Date: Fri, 20 Oct 2023 02:58:24 +0200 Subject: [PATCH] Reorganize preset backend --- backend/src/index.ts | 2 + backend/src/preset.ts | 29 ------------ backend/src/presets.ts | 79 ++++++++++++++++++++++++++++++++ backend/src/presetsController.ts | 21 +++------ backend/src/routes.ts | 1 + backend/swagger.json | 6 ++- common/types/preset.ts | 2 + frontend/src/pages/create.tsx | 11 ++++- 8 files changed, 104 insertions(+), 47 deletions(-) delete mode 100644 backend/src/preset.ts create mode 100644 backend/src/presets.ts diff --git a/backend/src/index.ts b/backend/src/index.ts index fb08a14..73dcf9c 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -6,6 +6,7 @@ import path from 'path'; import * as Auth from './auth'; import * as Election from './election'; import * as ManagedGameServers from './managedGameServers'; +import * as Presets from './presets'; import * as Match from './match'; import { checkAndNormalizeLogAddress } from './match'; import * as MatchMap from './matchMap'; @@ -111,6 +112,7 @@ const main = async () => { await Auth.setup(); await WebSocket.setup(httpServer); await ManagedGameServers.setup(); + await Presets.setup(); Match.registerCommandHandlers(); MatchMap.registerCommandHandlers(); Election.registerCommandHandlers(); diff --git a/backend/src/preset.ts b/backend/src/preset.ts deleted file mode 100644 index ea280e1..0000000 --- a/backend/src/preset.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { IPreset } from '../../common/types/preset'; -import * as Storage from './storage'; - -const FILE_NAME = 'presets.json'; - -export const getPresets = async () => { - return await Storage.read(FILE_NAME, []); -}; - -export const writePresets = async (presets: IPreset[]) => { - return await Storage.write(FILE_NAME, presets); -}; - -export const setPreset = async (data: IPreset) => { - const presets = await getPresets(); - presets.push(data); - writePresets(presets); -}; - -export const deletePreset = async (id: string) => { - const oldPresets = await getPresets(); - const newPresets = oldPresets.filter((preset) => preset.id !== id); - if (oldPresets.length === newPresets.length) { - return false; - } else { - writePresets(newPresets); - return true; - } -}; diff --git a/backend/src/presets.ts b/backend/src/presets.ts new file mode 100644 index 0000000..5e1ee54 --- /dev/null +++ b/backend/src/presets.ts @@ -0,0 +1,79 @@ +import { generate as shortUuid } from 'short-uuid'; +import { IPreset, IPresetCreateDto, IPresetUpdateDto } from '../../common/types/preset'; +import * as Storage from './storage'; + +const FILE_NAME = 'presets.json'; +const presets = new Map(); + +const write = async () => { + await Storage.write(FILE_NAME, Array.from(presets.values())); +}; + +export const setup = async () => { + const data = await Storage.read(FILE_NAME, [] as IPreset[]); + data.forEach((preset) => presets.set(preset.id, preset)); +}; + +export const getAll = () => { + return Array.from(presets.values()); +}; + +export const add = async (dto: IPresetCreateDto) => { + let id: string; + do { + id = shortUuid(); + } while (presets.has(id)); + const preset = { + id: id, + name: dto.name, + data: dto.data, + }; + presets.set(id, preset); + await write(); + return preset; +}; + +export const update = async (dto: IPresetUpdateDto) => { + const preset = presets.get(dto.id); + if (!preset) { + return false; + } + presets.set(dto.id, dto); + await write(); + return true; +}; + +export const remove = async (id: string) => { + const removed = presets.delete(id); + if (removed) { + await write(); + } + return removed; +}; +/* + +export const getPresets = async () => { + return await Storage.read(FILE_NAME, []); +}; + +export const writePresets = async (presets: IPreset[]) => { + return await Storage.write(FILE_NAME, presets); +}; + +export const setPreset = async (data: IPreset) => { + const presets = await getPresets(); + presets.push(data); + writePresets(presets); +}; + +export const deletePreset = async (id: string) => { + const oldPresets = await getPresets(); + const newPresets = oldPresets.filter((preset) => preset.id !== id); + if (oldPresets.length === newPresets.length) { + return false; + } else { + writePresets(newPresets); + return true; + } +}; +*/ diff --git a/backend/src/presetsController.ts b/backend/src/presetsController.ts index 53c0072..d513e05 100644 --- a/backend/src/presetsController.ts +++ b/backend/src/presetsController.ts @@ -3,7 +3,6 @@ import { Controller, Delete, Get, - NoSecurity, Post, Put, Request, @@ -11,18 +10,16 @@ import { Security, SuccessResponse, } from '@tsoa/runtime'; -import { generate as shortUuid } from 'short-uuid'; import { IPreset, IPresetCreateDto } from '../../common/types/preset'; import { IAuthResponse } from './auth'; -import { deletePreset, getPresets, setPreset } from './preset'; +import * as Presets from './presets'; @Route('/api/presets') @Security('bearer_token') export class PresetsController extends Controller { @Get() - @NoSecurity() async getPresets(@Request() { user }: { user: IAuthResponse }): Promise { - return getPresets(); + return Presets.getAll(); } @Post() @@ -31,27 +28,21 @@ export class PresetsController extends Controller { @Body() requestBody: IPresetCreateDto, @Request() { user }: { user: IAuthResponse } ): Promise { - const id = shortUuid(); - const preset: IPreset = { ...requestBody, id: id }; - setPreset(preset); + const preset = Presets.add(requestBody); this.setStatus(201); return preset; } - @Put('') + @Put() async updatePreset(@Body() requestBody: IPreset, @Request() { user }: { user: IAuthResponse }) { - const presets = await getPresets(); - const preset = presets.find((preset) => preset.id === requestBody.id); - if (preset) { - setPreset({ ...requestBody }); - } else { + if (!(await Presets.update(requestBody))) { this.setStatus(404); } } @Delete('{id}') async deletePreset(id: string, @Request() { user }: { user: IAuthResponse }): Promise { - if (!(await deletePreset(id))) { + if (!(await Presets.remove(id))) { this.setStatus(404); } } diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 219ead2..887c0f2 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -1677,6 +1677,7 @@ export function RegisterRoutes(app: Router) { // WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa app.get( '/api/presets', + authenticateMiddleware([{ bearer_token: [] }]), ...fetchMiddlewares(PresetsController), ...fetchMiddlewares(PresetsController.prototype.getPresets), diff --git a/backend/swagger.json b/backend/swagger.json index c2488e6..7efcff4 100644 --- a/backend/swagger.json +++ b/backend/swagger.json @@ -2816,7 +2816,11 @@ } } }, - "security": [], + "security": [ + { + "bearer_token": [] + } + ], "parameters": [] }, "post": { diff --git a/common/types/preset.ts b/common/types/preset.ts index a185c0e..e01023e 100644 --- a/common/types/preset.ts +++ b/common/types/preset.ts @@ -8,3 +8,5 @@ export interface IPresetCreateDto { export interface IPreset extends IPresetCreateDto { id: string; } + +export interface IPresetUpdateDto extends IPreset {} diff --git a/frontend/src/pages/create.tsx b/frontend/src/pages/create.tsx index 2e5e235..74cf13f 100644 --- a/frontend/src/pages/create.tsx +++ b/frontend/src/pages/create.tsx @@ -224,7 +224,7 @@ export const CreatePage: Component = () => { const setMatchDataFromPreset = (presetId: string) => { const preset = getPresetById(presetId); if (preset) { - setMatchCreateDto(preset.data); + setMatchCreateDto(JSON.parse(JSON.stringify(preset.data))); } }; @@ -249,7 +249,14 @@ export const CreatePage: Component = () => { > - {(preset) => } + {(preset) => ( + + )}