From 8237d5f21067c0872a7eff7c8d1506edf44ec10c Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Wed, 9 Apr 2025 10:23:01 +0200 Subject: [PATCH] feat(RunnerCardController): putByCode --- src/controllers/RunnerCardController.ts | 23 +++++++++ .../actions/update/UpdateRunnerCardByCode.ts | 50 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/models/actions/update/UpdateRunnerCardByCode.ts diff --git a/src/controllers/RunnerCardController.ts b/src/controllers/RunnerCardController.ts index fbe21f8..4ffa340 100644 --- a/src/controllers/RunnerCardController.ts +++ b/src/controllers/RunnerCardController.ts @@ -5,6 +5,7 @@ import { RunnerCardHasScansError, RunnerCardIdsNotMatchingError, RunnerCardNotFo import { RunnerNotFoundError } from '../errors/RunnerErrors'; import { CreateRunnerCard } from '../models/actions/create/CreateRunnerCard'; import { UpdateRunnerCard } from '../models/actions/update/UpdateRunnerCard'; +import { UpdateRunnerCardByCode } from '../models/actions/update/UpdateRunnerCardByCode'; import { RunnerCard } from '../models/entities/RunnerCard'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard'; @@ -112,6 +113,28 @@ export class RunnerCardController { return (await this.cardRepository.findOne({ id: id }, { relations: ['runner', 'runner.group', 'runner.group.parentGroup'] })).toResponse(); } + @Put('/:code') + @Authorized("CARD:UPDATE") + @ResponseSchema(ResponseRunnerCard) + @ResponseSchema(RunnerCardNotFoundError, { statusCode: 404 }) + @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) + @ResponseSchema(RunnerCardIdsNotMatchingError, { statusCode: 406 }) + @OpenAPI({ description: "Update the card whose code you provided." }) + async putByCode(@Param('code') code: string, @Body({ validate: true }) card: UpdateRunnerCardByCode) { + let oldCard = await this.cardRepository.findOne({ code: code }); + + if (!oldCard) { + throw new RunnerCardNotFoundError(); + } + + if (oldCard.code != card.code) { + throw new RunnerCardIdsNotMatchingError(); + } + + await this.cardRepository.save(await card.update(oldCard)); + return (await this.cardRepository.findOne({ code: code }, { relations: ['runner', 'runner.group', 'runner.group.parentGroup'] })).toResponse(); + } + @Delete('/:id') @Authorized("CARD:DELETE") @ResponseSchema(ResponseRunnerCard) diff --git a/src/models/actions/update/UpdateRunnerCardByCode.ts b/src/models/actions/update/UpdateRunnerCardByCode.ts new file mode 100644 index 0000000..7689ec5 --- /dev/null +++ b/src/models/actions/update/UpdateRunnerCardByCode.ts @@ -0,0 +1,50 @@ +import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { getConnection } from 'typeorm'; +import { RunnerNotFoundError } from '../../../errors/RunnerErrors'; +import { Runner } from '../../entities/Runner'; +import { RunnerCard } from '../../entities/RunnerCard'; + +/** + * This class is used to update a RunnerCard entity (via put request). + */ +export class UpdateRunnerCardByCode { + /** + * The card's code. + */ + @IsString() + @IsNotEmpty() + code?: string; + + /** + * The runner's id. + */ + @IsInt() + @IsOptional() + runner?: number; + + /** + * Is the updated card enabled (for fraud reasons)? + * Default: true + */ + @IsBoolean() + enabled: boolean = true; + + /** + * Creates a new RunnerCard entity from this. + */ + public async update(card: RunnerCard): Promise { + card.enabled = this.enabled; + card.runner = await this.getRunner(); + + return card; + } + + public async getRunner(): Promise { + if (!this.runner) { return null; } + const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner }); + if (!runner) { + throw new RunnerNotFoundError(); + } + return runner; + } +} \ No newline at end of file