From eec528430682fdf2209825d511852141a1c6bd2b Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 7 Jan 2021 17:12:12 +0100 Subject: [PATCH] Implemented "normal" scan updateing ref #67 --- src/controllers/ScanController.ts | 41 +++++++++++---------- src/models/actions/UpdateScan.ts | 59 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 src/models/actions/UpdateScan.ts diff --git a/src/controllers/ScanController.ts b/src/controllers/ScanController.ts index 259727c..6674f36 100644 --- a/src/controllers/ScanController.ts +++ b/src/controllers/ScanController.ts @@ -1,8 +1,10 @@ -import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, QueryParam } from 'routing-controllers'; +import { Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; -import { ScanNotFoundError } from '../errors/ScanErrors'; +import { RunnerNotFoundError } from '../errors/RunnerErrors'; +import { ScanIdsNotMatchingError, ScanNotFoundError } from '../errors/ScanErrors'; import { CreateScan } from '../models/actions/CreateScan'; +import { UpdateScan } from '../models/actions/UpdateScan'; import { Scan } from '../models/entities/Scan'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseScan } from '../models/responses/ResponseScan'; @@ -57,26 +59,27 @@ export class ScanController { return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse(); } - // @Put('/:id') - // @Authorized("DONOR:UPDATE") - // @ResponseSchema(ResponseDonor) - // @ResponseSchema(DonorNotFoundError, { statusCode: 404 }) - // @ResponseSchema(DonorIdsNotMatchingError, { 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 }) donor: UpdateDonor) { - // let oldDonor = await this.donorRepository.findOne({ id: id }); + @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 (!oldDonor) { - // throw new DonorNotFoundError(); - // } + if (!oldScan) { + throw new ScanNotFoundError(); + } - // if (oldDonor.id != donor.id) { - // throw new DonorIdsNotMatchingError(); - // } + if (oldScan.id != scan.id) { + throw new ScanIdsNotMatchingError(); + } - // await this.donorRepository.save(await donor.updateDonor(oldDonor)); - // return new ResponseDonor(await this.donorRepository.findOne({ id: id })); - // } + await this.scanRepository.save(await scan.updateScan(oldScan)); + return (await this.scanRepository.findOne({ id: id }, { relations: ['runner'] })).toResponse(); + } @Delete('/:id') @Authorized("SCAN:DELETE") diff --git a/src/models/actions/UpdateScan.ts b/src/models/actions/UpdateScan.ts new file mode 100644 index 0000000..a51ccea --- /dev/null +++ b/src/models/actions/UpdateScan.ts @@ -0,0 +1,59 @@ +import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator'; +import { getConnection } from 'typeorm'; +import { RunnerNotFoundError } from '../../errors/RunnerErrors'; +import { Runner } from '../entities/Runner'; +import { Scan } from '../entities/Scan'; + +/** + * This classed is used to create a new Scan entity from a json body (post request). + */ +export abstract class UpdateScan { + /** + * The updated scan's id. + * This shouldn't have changed but it is here in case anyone ever wants to enable id changes (whyever they would want to). + */ + @IsInt() + id: number; + + /** + * The updated scan's associated runner. + * This is important to link ran distances to runners. + */ + @IsInt() + @IsPositive() + runner: number; + + /** + * Is the updated scan valid (for fraud reasons). + */ + @IsBoolean() + @IsOptional() + valid?: boolean = true; + + /** + * The updated scan's distance in meters. + */ + @IsInt() + @IsPositive() + public distance: number; + + /** + * Update a Scan entity based on this. + * @param scan The scan that shall be updated. + */ + public async updateScan(scan: Scan): Promise { + scan.distance = this.distance; + scan.valid = this.valid; + scan.runner = await this.getRunner(); + + return scan; + } + + public async getRunner(): Promise { + const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner }); + if (!runner) { + throw new RunnerNotFoundError(); + } + return runner; + } +} \ No newline at end of file