diff --git a/src/controllers/ScanController.ts b/src/controllers/ScanController.ts index fb99d3a..cefef0d 100644 --- a/src/controllers/ScanController.ts +++ b/src/controllers/ScanController.ts @@ -52,8 +52,9 @@ export class ScanController { @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(scan)).toResponse(); + scan = await this.scanRepository.save(scan); + console.log(scan); + return (await this.scanRepository.findOne({ id: scan.id })).toResponse(); } // @Put('/:id') diff --git a/src/models/actions/CreateScan.ts b/src/models/actions/CreateScan.ts index 170703b..ef6d0e4 100644 --- a/src/models/actions/CreateScan.ts +++ b/src/models/actions/CreateScan.ts @@ -1,11 +1,56 @@ +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 CreateScan { + /** + * The scan's associated runner. + * This is important to link ran distances to runners. + */ + @IsInt() + @IsPositive() + runner: number; + + /** + * Is the scan valid (for fraud reasons). + * The determination of validity will work differently for every child class. + * Default: true + */ + @IsBoolean() + @IsOptional() + valid?: boolean = true; + + /** + * The scan's distance in meters. + * Can be set manually or derived from another object. + */ + @IsInt() + @IsPositive() + public distance: number; + /** * Creates a new Scan entity from this. */ - public abstract toScan(): Promise; + public async toScan(): Promise { + let newScan = new Scan(); + + newScan.distance = this.distance; + newScan.valid = this.valid; + newScan.runner = await this.getRunner(); + + return newScan; + } + + 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 diff --git a/src/models/entities/Scan.ts b/src/models/entities/Scan.ts index c525eb3..2424a0c 100644 --- a/src/models/entities/Scan.ts +++ b/src/models/entities/Scan.ts @@ -42,7 +42,7 @@ export class Scan { /** * The scan's distance in meters. - * Can be set manually or derived from another object. + * This is the "real" value used by "normal" scans.. */ @Column({ nullable: true }) @IsInt() @@ -58,6 +58,14 @@ export class Scan { return this._distance; } + /** + * The scan's distance in meters. + * Can be set manually or derived from another object. + */ + public set distance(value: number) { + this._distance = value; + } + /** * Turns this entity into it's response class. */ diff --git a/src/models/responses/ResponseScan.ts b/src/models/responses/ResponseScan.ts index b5883bd..debda1e 100644 --- a/src/models/responses/ResponseScan.ts +++ b/src/models/responses/ResponseScan.ts @@ -1,4 +1,4 @@ -import { IsBoolean, IsInt, IsNotEmpty, IsPositive } from "class-validator"; +import { IsBoolean, IsInt, 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 = new ResponseRunner(scan.runner); + this.runner = null; this.distance = scan.distance; this.valid = scan.valid; }