From b9c0a328628cb6a68460d4c3264487ec18004a8b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 7 Jan 2021 18:35:19 +0100 Subject: [PATCH] Implemented single scan station get +e errors ref #67 --- src/controllers/ScanStationController.ts | 26 ++++++++++++------------ src/errors/ScanStationErrors.ts | 25 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/errors/ScanStationErrors.ts diff --git a/src/controllers/ScanStationController.ts b/src/controllers/ScanStationController.ts index 665a010..fc13028 100644 --- a/src/controllers/ScanStationController.ts +++ b/src/controllers/ScanStationController.ts @@ -1,6 +1,7 @@ -import { Authorized, Get, JsonController } from 'routing-controllers'; +import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; +import { ScanStationNotFoundError } from '../errors/ScanStationErrors'; import { ScanStation } from '../models/entities/ScanStation'; import { ResponseScanStation } from '../models/responses/ResponseScanStation'; @@ -29,18 +30,17 @@ export class ScanStationController { return responseStations; } - // @Get('/:id') - // @Authorized("SCAN:GET") - // @ResponseSchema(ResponseScan) - // @ResponseSchema(ResponseTrackScan) - // @ResponseSchema(ScanNotFoundError, { statusCode: 404 }) - // @OnUndefined(ScanNotFoundError) - // @OpenAPI({ description: 'Lists all information about the runner whose id got provided.' }) - // async getOne(@Param('id') id: number) { - // let scan = await this.scanRepository.findOne({ id: id }, { relations: ['runner'] }) - // if (!scan) { throw new ScanNotFoundError(); } - // return scan.toResponse(); - // } + @Get('/:id') + @Authorized("STATION:GET") + @ResponseSchema(ResponseScanStation) + @ResponseSchema(ScanStationNotFoundError, { statusCode: 404 }) + @OnUndefined(ScanStationNotFoundError) + @OpenAPI({ description: 'Lists all information about the runner whose id got provided.' }) + async getOne(@Param('id') id: number) { + let scan = await this.stationRepository.findOne({ id: id }, { relations: ['track'] }) + if (!scan) { throw new ScanStationNotFoundError(); } + return scan.toResponse(); + } // @Post() // @Authorized("SCAN:CREATE") diff --git a/src/errors/ScanStationErrors.ts b/src/errors/ScanStationErrors.ts new file mode 100644 index 0000000..8cc6d88 --- /dev/null +++ b/src/errors/ScanStationErrors.ts @@ -0,0 +1,25 @@ +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; + +/** + * Error to throw, when a non-existant scan station get's loaded. + */ +export class ScanStationNotFoundError extends NotFoundError { + @IsString() + name = "ScanStationNotFoundError" + + @IsString() + message = "The scan station you provided couldn't be located in the system. \n Please check your request." +} + +/** + * Error to throw when two scan stations' ids don't match. + * Usually occurs when a user tries to change a scan station's id. + */ +export class ScanStationIdsNotMatchingError extends NotAcceptableError { + @IsString() + name = "ScanStationIdsNotMatchingError" + + @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