Still broken distance, we'll fix this together

ref #13
This commit is contained in:
Nicolai Ort 2020-12-04 19:31:20 +01:00
parent 8beb658bcc
commit c30922e325
2 changed files with 23 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import { Entity, Column, ManyToOne, ChildEntity } from "typeorm";
import { IsInt, IsNotEmpty, IsPositive, } from "class-validator";
import { IsInt, IsNotEmpty, IsPositive } from "class-validator";
import { ChildEntity, Column, ManyToOne } from "typeorm";
import { Donation } from "./Donation";
import { Runner } from "./Runner";

View File

@ -36,13 +36,25 @@ export class Runner extends Participant {
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
scans: Scan[];
@IsInt()
public get distance(): number {
getConnectionManager().get().getRepository(Scan).find({ runner: this })
.then(myScans => {
return myScans.reduce((sum, current) => sum + current.distance, 0);
})
.catch(err => { throw err })
.finally(do => { return -1; });
/**
* 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 });
}
/**
* 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);
}
}