Fixed bugs concerning posts

ref #13
This commit is contained in:
2020-12-05 10:59:15 +01:00
parent ef54dd5e9c
commit aca13f7308
5 changed files with 11 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
import { IsInt, IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
import { DistanceDonation } from "./DistanceDonation";
import { Participant } from "./Participant";
import { RunnerCard } from "./RunnerCard";
@@ -36,25 +36,18 @@ export class Runner extends Participant {
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
scans: Scan[];
/**
* Returns all scans associated with this runner.
*/
public async getScans(): Promise<Scan[]> {
return await getConnectionManager().get().getRepository(Scan).find({ runner: this });
}
/**
* Returns all valid scans associated with this runner.
*/
public async getValidScans(): Promise<Scan[]> {
return (await this.getScans()).filter(scan => { scan.valid === true });
public get validScans(): Scan[] {
return this.scans.filter(scan => { scan.valid === true });
}
/**
* Returns the total distance ran by this runner.
*/
@IsInt()
public async distance(): Promise<number> {
return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0);
public get distance(): number {
return this.validScans.reduce((sum, current) => sum + current.distance, 0);
}
}