Skip to content

Commit

Permalink
Reorganize preset backend
Browse files Browse the repository at this point in the history
  • Loading branch information
JensForstmann committed Oct 20, 2023
1 parent bed4208 commit 8dc8b89
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 47 deletions.
2 changes: 2 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 0 additions & 29 deletions backend/src/preset.ts

This file was deleted.

79 changes: 79 additions & 0 deletions backend/src/presets.ts
Original file line number Diff line number Diff line change
@@ -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<string, IPreset>();

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<IPreset[]>(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;
}
};
*/
21 changes: 6 additions & 15 deletions backend/src/presetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@ import {
Controller,
Delete,
Get,
NoSecurity,
Post,
Put,
Request,
Route,
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<IPreset[]> {
return getPresets();
return Presets.getAll();
}

@Post()
Expand All @@ -31,27 +28,21 @@ export class PresetsController extends Controller {
@Body() requestBody: IPresetCreateDto,
@Request() { user }: { user: IAuthResponse }
): Promise<IPreset> {
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<void> {
if (!(await deletePreset(id))) {
if (!(await Presets.remove(id))) {
this.setStatus(404);
}
}
Expand Down
1 change: 1 addition & 0 deletions backend/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestHandler>(PresetsController),
...fetchMiddlewares<RequestHandler>(PresetsController.prototype.getPresets),

Expand Down
6 changes: 5 additions & 1 deletion backend/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,11 @@
}
}
},
"security": [],
"security": [
{
"bearer_token": []
}
],
"parameters": []
},
"post": {
Expand Down
2 changes: 2 additions & 0 deletions common/types/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export interface IPresetCreateDto {
export interface IPreset extends IPresetCreateDto {
id: string;
}

export interface IPresetUpdateDto extends IPreset {}
11 changes: 9 additions & 2 deletions frontend/src/pages/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
};

Expand All @@ -249,7 +249,14 @@ export const CreatePage: Component = () => {
>
<option value=""></option>
<For each={presets()}>
{(preset) => <option value={preset.id}>{preset.name}</option>}
{(preset) => (
<option
value={preset.id}
selected={selectedPresetId() === preset.id}
>
{preset.name}
</option>
)}
</For>
</SelectInput>
</div>
Expand Down

0 comments on commit 8dc8b89

Please sign in to comment.