parent
2628f69651
commit
9b9ee70288
@ -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 { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
import { getConnectionManager, Repository } from 'typeorm';
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
import { ScanStationNotFoundError } from '../errors/ScanStationErrors';
|
import { ScanStationHasScansError, ScanStationNotFoundError } from '../errors/ScanStationErrors';
|
||||||
import { TrackNotFoundError } from '../errors/TrackErrors';
|
import { TrackNotFoundError } from '../errors/TrackErrors';
|
||||||
import { CreateScanStation } from '../models/actions/CreateScanStation';
|
import { CreateScanStation } from '../models/actions/CreateScanStation';
|
||||||
import { ScanStation } from '../models/entities/ScanStation';
|
import { ScanStation } from '../models/entities/ScanStation';
|
||||||
|
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||||
import { ResponseScanStation } from '../models/responses/ResponseScanStation';
|
import { ResponseScanStation } from '../models/responses/ResponseScanStation';
|
||||||
|
import { ScanController } from './ScanController';
|
||||||
|
|
||||||
@JsonController('/stations')
|
@JsonController('/stations')
|
||||||
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||||
@ -79,18 +81,28 @@ export class ScanStationController {
|
|||||||
// return (await this.scanRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse();
|
// return (await this.scanRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// @Delete('/:id')
|
@Delete('/:id')
|
||||||
// @Authorized("SCAN:DELETE")
|
@Authorized("STATION:DELETE")
|
||||||
// @ResponseSchema(ResponseScan)
|
@ResponseSchema(ResponseScanStation)
|
||||||
// @ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||||
// @OnUndefined(204)
|
@ResponseSchema(ScanStationHasScansError, { statusCode: 406 })
|
||||||
// @OpenAPI({ description: 'Delete the runner whose id you provided. <br> If no runner 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 runner whose id you provided. <br> If no runner with this id exists it will just return 204(no content).' })
|
||||||
// let scan = await this.scanRepository.findOne({ id: id });
|
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||||
// if (!scan) { return null; }
|
let station = await this.stationRepository.findOne({ id: id });
|
||||||
// const responseScan = await this.scanRepository.findOne({ id: scan.id }, { relations: ["runner"] });
|
if (!station) { return null; }
|
||||||
|
|
||||||
// await this.scanRepository.delete(scan);
|
const stationScans = (await this.stationRepository.findOne({ id: station.id }, { relations: ["scans"] })).scans;
|
||||||
// return responseScan.toResponse();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,3 +23,14 @@ export class ScanStationIdsNotMatchingError extends NotAcceptableError {
|
|||||||
@IsString()
|
@IsString()
|
||||||
message = "The ids don't match! \n And if you wanted to change a scan station's id: This isn't allowed!"
|
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."
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user