From 9b9ee702882730bc765d4e684ff85ec9e9b1ceb1 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 7 Jan 2021 18:48:58 +0100 Subject: [PATCH] Implemented cascading station deletion ref #67 --- src/controllers/ScanStationController.ts | 42 +++++++++++++++--------- src/errors/ScanStationErrors.ts | 13 +++++++- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/controllers/ScanStationController.ts b/src/controllers/ScanStationController.ts index 536c265..d7b2095 100644 --- a/src/controllers/ScanStationController.ts +++ b/src/controllers/ScanStationController.ts @@ -1,11 +1,13 @@ -import { Authorized, Body, Get, JsonController, OnUndefined, Param, Post } from 'routing-controllers'; +import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; -import { ScanStationNotFoundError } from '../errors/ScanStationErrors'; +import { ScanStationHasScansError, ScanStationNotFoundError } from '../errors/ScanStationErrors'; import { TrackNotFoundError } from '../errors/TrackErrors'; import { CreateScanStation } from '../models/actions/CreateScanStation'; import { ScanStation } from '../models/entities/ScanStation'; +import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseScanStation } from '../models/responses/ResponseScanStation'; +import { ScanController } from './ScanController'; @JsonController('/stations') @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) @@ -79,18 +81,28 @@ export class ScanStationController { // return (await this.scanRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse(); // } - // @Delete('/:id') - // @Authorized("SCAN:DELETE") - // @ResponseSchema(ResponseScan) - // @ResponseSchema(ResponseEmpty, { statusCode: 204 }) - // @OnUndefined(204) - // @OpenAPI({ description: 'Delete the runner whose id you provided.
If no runner with this id exists it will just return 204(no content).' }) - // async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { - // let scan = await this.scanRepository.findOne({ id: id }); - // if (!scan) { return null; } - // const responseScan = await this.scanRepository.findOne({ id: scan.id }, { relations: ["runner"] }); + @Delete('/:id') + @Authorized("STATION:DELETE") + @ResponseSchema(ResponseScanStation) + @ResponseSchema(ResponseEmpty, { statusCode: 204 }) + @ResponseSchema(ScanStationHasScansError, { statusCode: 406 }) + @OnUndefined(204) + @OpenAPI({ description: 'Delete the runner whose id you provided.
If no runner with this id exists it will just return 204(no content).' }) + async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { + let station = await this.stationRepository.findOne({ id: id }); + if (!station) { return null; } - // await this.scanRepository.delete(scan); - // return responseScan.toResponse(); - // } + const stationScans = (await this.stationRepository.findOne({ id: station.id }, { relations: ["scans"] })).scans; + if (stationScans.length != 0 && !force) { + throw new ScanStationHasScansError(); + } + const scanController = new ScanController; + for (let scan of stationScans) { + scanController.remove(scan.id, force); + } + + const responseStation = await this.stationRepository.findOne({ id: station.id }, { relations: ["track", "scans"] }); + await this.stationRepository.delete(station); + return responseStation.toResponse(); + } } diff --git a/src/errors/ScanStationErrors.ts b/src/errors/ScanStationErrors.ts index 8cc6d88..c013cf5 100644 --- a/src/errors/ScanStationErrors.ts +++ b/src/errors/ScanStationErrors.ts @@ -22,4 +22,15 @@ export class ScanStationIdsNotMatchingError extends NotAcceptableError { @IsString() message = "The ids don't match! \n And if you wanted to change a scan station's id: This isn't allowed!" -} \ No newline at end of file +} + +/** + * Error to throw when a station still has scans associated. + */ +export class ScanStationHasScansError extends NotAcceptableError { + @IsString() + name = "ScanStationHasScansError" + + @IsString() + message = "This station still has scans associated with it. \n If you want to delete this station with all it's scans add `?force` to your query." +}