Implemented trackscan updateing

ref #78
This commit is contained in:
2021-01-09 16:17:50 +01:00
parent e7cd68e1c8
commit 638898fa28
3 changed files with 113 additions and 7 deletions

View File

@@ -0,0 +1,79 @@
import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator';
import { getConnection } from 'typeorm';
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
import { ScanStationNotFoundError } from '../../errors/ScanStationErrors';
import { Runner } from '../entities/Runner';
import { ScanStation } from '../entities/ScanStation';
import { TrackScan } from '../entities/TrackScan';
/**
* This class is used to update a TrackScan entity (via put request)
*/
export abstract class UpdateTrackScan {
/**
* 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 associated station.
* This is important to link ran distances to runners.
*/
@IsInt()
@IsOptional()
public station?: number;
/**
* Update a TrackScan entity based on this.
* @param scan The scan that shall be updated.
*/
public async updateScan(scan: TrackScan): Promise<TrackScan> {
scan.valid = this.valid;
scan.runner = await this.getRunner();
if (this.station) {
scan.station = await this.getStation();
}
scan.track = scan.station.track;
return scan;
}
/**
* Gets a runner based on the runner id provided via this.runner.
*/
public async getRunner(): Promise<Runner> {
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
if (!runner) {
throw new RunnerNotFoundError();
}
return runner;
}
/**
* Gets a runner based on the runner id provided via this.runner.
*/
public async getStation(): Promise<ScanStation> {
const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station });
if (!station) {
throw new ScanStationNotFoundError();
}
return station;
}
}