From eea656bd7b09c8a878235b88793a7a2aa4baf41b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 7 Jan 2021 17:16:36 +0100 Subject: [PATCH] Added a barebones scanstation controller ref #67 --- src/controllers/ScanStationController.ts | 91 ++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/controllers/ScanStationController.ts diff --git a/src/controllers/ScanStationController.ts b/src/controllers/ScanStationController.ts new file mode 100644 index 0000000..ac4aa11 --- /dev/null +++ b/src/controllers/ScanStationController.ts @@ -0,0 +1,91 @@ +import { JsonController } from 'routing-controllers'; +import { OpenAPI } from 'routing-controllers-openapi'; +import { getConnectionManager, Repository } from 'typeorm'; +import { ScanStation } from '../models/entities/ScanStation'; + +@JsonController('/stations') +@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) +export class ScanStationController { + private stationRepository: Repository; + + /** + * Gets the repository of this controller's model/entity. + */ + constructor() { + this.stationRepository = getConnectionManager().get().getRepository(ScanStation); + } + + // @Get() + // @Authorized("SCAN:GET") + // @ResponseSchema(ResponseScan, { isArray: true }) + // @ResponseSchema(ResponseTrackScan, { isArray: true }) + // @OpenAPI({ description: 'Lists all scans (normal or track) from all runners.
This includes the runner\'s group and distance ran.' }) + // async getAll() { + // let responseScans: ResponseScan[] = new Array(); + // const scans = await this.scanRepository.find({ relations: ['runner'] }); + // scans.forEach(scan => { + // responseScans.push(scan.toResponse()); + // }); + // return responseScans; + // } + + // @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(); + // } + + // @Post() + // @Authorized("SCAN:CREATE") + // @ResponseSchema(ResponseScan) + // @OpenAPI({ description: 'Create a new runner.
Please remeber to provide the runner\'s group\'s id.' }) + // async post(@Body({ validate: true }) createScan: CreateScan) { + // let scan = await createScan.toScan(); + // scan = await this.scanRepository.save(scan); + // return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse(); + // } + + // @Put('/:id') + // @Authorized("SCAN:UPDATE") + // @ResponseSchema(ResponseScan) + // @ResponseSchema(ScanNotFoundError, { statusCode: 404 }) + // @ResponseSchema(RunnerNotFoundError, { statusCode: 404 }) + // @ResponseSchema(ScanIdsNotMatchingError, { statusCode: 406 }) + // @OpenAPI({ description: "Update the runner whose id you provided.
Please remember that ids can't be changed." }) + // async put(@Param('id') id: number, @Body({ validate: true }) scan: UpdateScan) { + // let oldScan = await this.scanRepository.findOne({ id: id }); + + // if (!oldScan) { + // throw new ScanNotFoundError(); + // } + + // if (oldScan.id != scan.id) { + // throw new ScanIdsNotMatchingError(); + // } + + // await this.scanRepository.save(await scan.updateScan(oldScan)); + // 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.
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"] }); + + // await this.scanRepository.delete(scan); + // return responseScan.toResponse(); + // } +}