parent
8beb658bcc
commit
c30922e325
|
@ -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";
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue