Added card deletion + errors

ref #77
This commit is contained in:
Nicolai Ort 2021-01-09 11:28:59 +01:00
parent 4faeddc3f3
commit a5bfe4e3d5
2 changed files with 37 additions and 23 deletions

View File

@ -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 { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { RunnerCardNotFoundError } from '../errors/RunnerCardErrors'; import { RunnerCardHasScansError, RunnerCardNotFoundError } from '../errors/RunnerCardErrors';
import { RunnerCard } from '../models/entities/RunnerCard'; import { RunnerCard } from '../models/entities/RunnerCard';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard'; import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard';
import { ScanController } from './ScanController';
@JsonController('/cards') @JsonController('/cards')
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
@ -76,26 +78,27 @@ export class RunnerCardController {
// return new ResponseTrack(await this.trackRepository.findOne({ id: id })); // return new ResponseTrack(await this.trackRepository.findOne({ id: id }));
// } // }
// @Delete('/:id') @Delete('/:id')
// @Authorized("CARD:DELETE") @Authorized("CARD:DELETE")
// @ResponseSchema(ResponseTrack) @ResponseSchema(ResponseRunnerCard)
// @ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(ResponseEmpty, { statusCode: 204 })
// @OnUndefined(204) @ResponseSchema(RunnerCardHasScansError, { statusCode: 406 })
// @OpenAPI({ description: "Delete the track whose id you provided. <br> If no track with this id exists it will just return 204(no content)." }) @OnUndefined(204)
// async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { @OpenAPI({ description: "Delete the card whose id you provided. <br> If no card with this id exists it will just return 204(no content). <br> 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)." })
// let track = await this.trackRepository.findOne({ id: id }); async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
// if (!track) { return null; } let card = await this.cardRepository.findOne({ id: id });
if (!card) { return null; }
// const trackStations = (await this.trackRepository.findOne({ id: id }, { relations: ["stations"] })).stations; const cardScans = (await this.cardRepository.findOne({ id: id }, { relations: ["scans"] })).scans;
// if (trackStations.length != 0 && !force) { if (cardScans.length != 0 && !force) {
// throw new TrackHasScanStationsError(); throw new RunnerCardHasScansError();
// } }
// const scanController = new ScanStationController; const scanController = new ScanController;
// for (let station of trackStations) { for (let scan of cardScans) {
// scanController.remove(station.id, force); scanController.remove(scan.id, force);
// } }
// await this.trackRepository.delete(track); await this.cardRepository.delete(card);
// return new ResponseTrack(track); return card.toResponse();
// } }
} }

View File

@ -22,4 +22,15 @@ export class RunnerCardIdsNotMatchingError extends NotAcceptableError {
@IsString() @IsString()
message = "The ids don't match! \n And if you wanted to change a cards's id: This isn't allowed" message = "The ids don't match! \n And if you wanted to change a cards's id: This isn't allowed"
} }
/**
* 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."
}