From a5bfe4e3d5072718f29e2d4ca324d63bd7393291 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 9 Jan 2021 11:28:59 +0100 Subject: [PATCH] Added card deletion + errors ref #77 --- src/controllers/RunnerCardController.ts | 47 +++++++++++++------------ src/errors/RunnerCardErrors.ts | 13 ++++++- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/controllers/RunnerCardController.ts b/src/controllers/RunnerCardController.ts index 08942d8..a11b626 100644 --- a/src/controllers/RunnerCardController.ts +++ b/src/controllers/RunnerCardController.ts @@ -1,9 +1,11 @@ -import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers'; +import { Authorized, Delete, Get, JsonController, OnUndefined, Param, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; -import { RunnerCardNotFoundError } from '../errors/RunnerCardErrors'; +import { RunnerCardHasScansError, RunnerCardNotFoundError } from '../errors/RunnerCardErrors'; import { RunnerCard } from '../models/entities/RunnerCard'; +import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard'; +import { ScanController } from './ScanController'; @JsonController('/cards') @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) @@ -76,26 +78,27 @@ export class RunnerCardController { // return new ResponseTrack(await this.trackRepository.findOne({ id: id })); // } - // @Delete('/:id') - // @Authorized("CARD:DELETE") - // @ResponseSchema(ResponseTrack) - // @ResponseSchema(ResponseEmpty, { statusCode: 204 }) - // @OnUndefined(204) - // @OpenAPI({ description: "Delete the track whose id you provided.
If no track with this id exists it will just return 204(no content)." }) - // async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { - // let track = await this.trackRepository.findOne({ id: id }); - // if (!track) { return null; } + @Delete('/:id') + @Authorized("CARD:DELETE") + @ResponseSchema(ResponseRunnerCard) + @ResponseSchema(ResponseEmpty, { statusCode: 204 }) + @ResponseSchema(RunnerCardHasScansError, { statusCode: 406 }) + @OnUndefined(204) + @OpenAPI({ description: "Delete the card whose id you provided.
If no card with this id exists it will just return 204(no content).
If the card still has scans associated you have to provide the force=true query param (warning: this deletes all scans associated with by this card - please disable it instead or just remove the runner association)." }) + async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { + let card = await this.cardRepository.findOne({ id: id }); + if (!card) { return null; } - // const trackStations = (await this.trackRepository.findOne({ id: id }, { relations: ["stations"] })).stations; - // if (trackStations.length != 0 && !force) { - // throw new TrackHasScanStationsError(); - // } - // const scanController = new ScanStationController; - // for (let station of trackStations) { - // scanController.remove(station.id, force); - // } + const cardScans = (await this.cardRepository.findOne({ id: id }, { relations: ["scans"] })).scans; + if (cardScans.length != 0 && !force) { + throw new RunnerCardHasScansError(); + } + const scanController = new ScanController; + for (let scan of cardScans) { + scanController.remove(scan.id, force); + } - // await this.trackRepository.delete(track); - // return new ResponseTrack(track); - // } + await this.cardRepository.delete(card); + return card.toResponse(); + } } \ No newline at end of file diff --git a/src/errors/RunnerCardErrors.ts b/src/errors/RunnerCardErrors.ts index bb0c6f3..08edb44 100644 --- a/src/errors/RunnerCardErrors.ts +++ b/src/errors/RunnerCardErrors.ts @@ -22,4 +22,15 @@ export class RunnerCardIdsNotMatchingError extends NotAcceptableError { @IsString() message = "The ids don't match! \n And if you wanted to change a cards's id: This isn't allowed" -} \ No newline at end of file +} + +/** + * Error to throw when a station still has scans associated. + */ +export class RunnerCardHasScansError extends NotAcceptableError { + @IsString() + name = "RunnerCardHasScansError" + + @IsString() + message = "This card still has scans associated with it. \n If you want to delete this card with all it's scans add `?force` to your query. \n Otherwise please consider just diableing it." +}