Fixed scan runner in response

ref #67
This commit is contained in:
Nicolai Ort 2021-01-07 16:38:41 +01:00
parent 30502ec949
commit e67d1c5697
3 changed files with 10 additions and 9 deletions

View File

@ -26,7 +26,7 @@ export class ScanController {
@OpenAPI({ description: 'Lists all scans (normal or track) from all runners. <br> This includes the runner\'s group and distance ran.' }) @OpenAPI({ description: 'Lists all scans (normal or track) from all runners. <br> This includes the runner\'s group and distance ran.' })
async getAll() { async getAll() {
let responseScans: ResponseScan[] = new Array<ResponseScan>(); let responseScans: ResponseScan[] = new Array<ResponseScan>();
const scans = await this.scanRepository.find(); const scans = await this.scanRepository.find({ relations: ['runner'] });
scans.forEach(scan => { scans.forEach(scan => {
responseScans.push(scan.toResponse()); responseScans.push(scan.toResponse());
}); });
@ -41,9 +41,9 @@ export class ScanController {
@OnUndefined(ScanNotFoundError) @OnUndefined(ScanNotFoundError)
@OpenAPI({ description: 'Lists all information about the runner whose id got provided.' }) @OpenAPI({ description: 'Lists all information about the runner whose id got provided.' })
async getOne(@Param('id') id: number) { 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(); } if (!scan) { throw new ScanNotFoundError(); }
return scan; return scan.toResponse();
} }
@Post() @Post()
@ -54,7 +54,7 @@ export class ScanController {
let scan = await createScan.toScan(); let scan = await createScan.toScan();
scan = await this.scanRepository.save(scan); scan = await this.scanRepository.save(scan);
console.log(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') // @Put('/:id')

View File

@ -29,7 +29,8 @@ export class ResponseRunner extends ResponseParticipant {
*/ */
public constructor(runner: Runner) { public constructor(runner: Runner) {
super(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; this.group = runner.group;
} }
} }

View File

@ -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 { Scan } from '../entities/Scan';
import { ResponseRunner } from './ResponseRunner'; import { ResponseRunner } from './ResponseRunner';
@ -16,8 +16,8 @@ export class ResponseScan {
* The scan's associated runner. * The scan's associated runner.
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
// @IsNotEmpty() @IsNotEmpty()
runner?: ResponseRunner; runner: ResponseRunner;
/** /**
* Is the scan valid (for fraud reasons). * Is the scan valid (for fraud reasons).
@ -39,7 +39,7 @@ export class ResponseScan {
*/ */
public constructor(scan: Scan) { public constructor(scan: Scan) {
this.id = scan.id; this.id = scan.id;
this.runner = null; this.runner = scan.runner.toResponse();
this.distance = scan.distance; this.distance = scan.distance;
this.valid = scan.valid; this.valid = scan.valid;
} }