diff --git a/src/controllers/ScanStationController.ts b/src/controllers/ScanStationController.ts index d7b2095..d6edcd3 100644 --- a/src/controllers/ScanStationController.ts +++ b/src/controllers/ScanStationController.ts @@ -101,7 +101,7 @@ export class ScanStationController { scanController.remove(scan.id, force); } - const responseStation = await this.stationRepository.findOne({ id: station.id }, { relations: ["track", "scans"] }); + const responseStation = await this.stationRepository.findOne({ id: station.id }, { relations: ["track"] }); await this.stationRepository.delete(station); return responseStation.toResponse(); } diff --git a/src/controllers/TrackController.ts b/src/controllers/TrackController.ts index f03718f..094756c 100644 --- a/src/controllers/TrackController.ts +++ b/src/controllers/TrackController.ts @@ -1,12 +1,13 @@ -import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put } from 'routing-controllers'; +import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; -import { TrackIdsNotMatchingError, TrackLapTimeCantBeNegativeError, TrackNotFoundError } from "../errors/TrackErrors"; +import { TrackHasScanStationsError, TrackIdsNotMatchingError, TrackLapTimeCantBeNegativeError, TrackNotFoundError } from "../errors/TrackErrors"; import { CreateTrack } from '../models/actions/CreateTrack'; import { UpdateTrack } from '../models/actions/UpdateTrack'; import { Track } from '../models/entities/Track'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseTrack } from '../models/responses/ResponseTrack'; +import { ScanStationController } from './ScanStationController'; @JsonController('/tracks') @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) @@ -85,10 +86,19 @@ export class TrackController { @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) { + async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { let track = await this.trackRepository.findOne({ id: id }); if (!track) { 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); + } + await this.trackRepository.delete(track); return new ResponseTrack(track); } diff --git a/src/errors/TrackErrors.ts b/src/errors/TrackErrors.ts index e3d1902..a07f643 100644 --- a/src/errors/TrackErrors.ts +++ b/src/errors/TrackErrors.ts @@ -33,4 +33,12 @@ export class TrackLapTimeCantBeNegativeError extends NotAcceptableError { @IsString() message = "The minimum lap time you provided is negative - That isn't possible. \n If you wanted to disable it: Just set it to 0/null." +} + +export class TrackHasScanStationsError extends NotAcceptableError { + @IsString() + name = "TrackHasScanStationsError" + + @IsString() + message = "This track still has stations associated with it. \n If you want to delete this track with all it's stations and scans add `?force` to your query." } \ No newline at end of file