From e67d1c56976994ffeae47f038d453123908dc08d Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 7 Jan 2021 16:38:41 +0100 Subject: [PATCH] Fixed scan runner in response ref #67 --- src/controllers/ScanController.ts | 8 ++++---- src/models/responses/ResponseRunner.ts | 3 ++- src/models/responses/ResponseScan.ts | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/controllers/ScanController.ts b/src/controllers/ScanController.ts index cefef0d..d82195e 100644 --- a/src/controllers/ScanController.ts +++ b/src/controllers/ScanController.ts @@ -26,7 +26,7 @@ export class ScanController { @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(); + const scans = await this.scanRepository.find({ relations: ['runner'] }); scans.forEach(scan => { responseScans.push(scan.toResponse()); }); @@ -41,9 +41,9 @@ export class ScanController { @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 }) + let scan = await this.scanRepository.findOne({ id: id }, { relations: ['runner'] }) if (!scan) { throw new ScanNotFoundError(); } - return scan; + return scan.toResponse(); } @Post() @@ -54,7 +54,7 @@ export class ScanController { let scan = await createScan.toScan(); scan = await this.scanRepository.save(scan); console.log(scan); - return (await this.scanRepository.findOne({ id: scan.id })).toResponse(); + return (await this.scanRepository.findOne({ id: scan.id }, { relations: ['runner'] })).toResponse(); } // @Put('/:id') diff --git a/src/models/responses/ResponseRunner.ts b/src/models/responses/ResponseRunner.ts index 5fae2ee..82402a8 100644 --- a/src/models/responses/ResponseRunner.ts +++ b/src/models/responses/ResponseRunner.ts @@ -29,7 +29,8 @@ export class ResponseRunner extends ResponseParticipant { */ public constructor(runner: Runner) { super(runner); - this.distance = runner.scans.filter(scan => { scan.valid === true }).reduce((sum, current) => sum + current.distance, 0); + if (!runner.scans) { this.distance = 0 } + else { this.distance = runner.scans.filter(scan => { scan.valid === true }).reduce((sum, current) => sum + current.distance, 0); } this.group = runner.group; } } diff --git a/src/models/responses/ResponseScan.ts b/src/models/responses/ResponseScan.ts index debda1e..e666442 100644 --- a/src/models/responses/ResponseScan.ts +++ b/src/models/responses/ResponseScan.ts @@ -1,4 +1,4 @@ -import { IsBoolean, IsInt, IsPositive } from "class-validator"; +import { IsBoolean, IsInt, IsNotEmpty, IsPositive } from "class-validator"; import { Scan } from '../entities/Scan'; import { ResponseRunner } from './ResponseRunner'; @@ -16,8 +16,8 @@ export class ResponseScan { * The scan's associated runner. * This is important to link ran distances to runners. */ - // @IsNotEmpty() - runner?: ResponseRunner; + @IsNotEmpty() + runner: ResponseRunner; /** * Is the scan valid (for fraud reasons). @@ -39,7 +39,7 @@ export class ResponseScan { */ public constructor(scan: Scan) { this.id = scan.id; - this.runner = null; + this.runner = scan.runner.toResponse(); this.distance = scan.distance; this.valid = scan.valid; }