Implemented cascading station deletion

ref #67
This commit is contained in:
Nicolai Ort 2021-01-07 18:48:58 +01:00
parent 2628f69651
commit 9b9ee70288
2 changed files with 39 additions and 16 deletions

View File

@ -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. <br> 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. <br> 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();
}
}

View File

@ -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!"
}
}
/**
* 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."
}