Fixed Creation of normal scans

ref #67
This commit is contained in:
Nicolai Ort 2021-01-07 16:32:16 +01:00
parent a2c3913601
commit 30502ec949
4 changed files with 62 additions and 8 deletions

View File

@ -52,8 +52,9 @@ export class ScanController {
@OpenAPI({ description: 'Create a new runner. <br> Please remeber to provide the runner\'s group\'s id.' }) @OpenAPI({ description: 'Create a new runner. <br> Please remeber to provide the runner\'s group\'s id.' })
async post(@Body({ validate: true }) createScan: CreateScan) { async post(@Body({ validate: true }) createScan: CreateScan) {
let scan = await createScan.toScan(); let scan = await createScan.toScan();
scan = await this.scanRepository.save(scan) scan = await this.scanRepository.save(scan);
return (await this.scanRepository.findOne(scan)).toResponse(); console.log(scan);
return (await this.scanRepository.findOne({ id: scan.id })).toResponse();
} }
// @Put('/:id') // @Put('/:id')

View File

@ -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'; import { Scan } from '../entities/Scan';
/** /**
* This classed is used to create a new Scan entity from a json body (post request). * This classed is used to create a new Scan entity from a json body (post request).
*/ */
export abstract class CreateScan { 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. * Creates a new Scan entity from this.
*/ */
public abstract toScan(): Promise<Scan>; public async toScan(): Promise<Scan> {
let newScan = new Scan();
newScan.distance = this.distance;
newScan.valid = this.valid;
newScan.runner = await this.getRunner();
return newScan;
}
public async getRunner(): Promise<Runner> {
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
if (!runner) {
throw new RunnerNotFoundError();
}
return runner;
}
} }

View File

@ -42,7 +42,7 @@ export class Scan {
/** /**
* The scan's distance in meters. * 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 }) @Column({ nullable: true })
@IsInt() @IsInt()
@ -58,6 +58,14 @@ export class Scan {
return this._distance; 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. * Turns this entity into it's response class.
*/ */

View File

@ -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 { 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 = new ResponseRunner(scan.runner); this.runner = null;
this.distance = scan.distance; this.distance = scan.distance;
this.valid = scan.valid; this.valid = scan.valid;
} }