parent
35dbfeb5e7
commit
ec64ec3d63
@ -9,6 +9,7 @@ import { RunnerTeam } from '../models/entities/RunnerTeam';
|
|||||||
import { Scan } from '../models/entities/Scan';
|
import { Scan } from '../models/entities/Scan';
|
||||||
import { User } from '../models/entities/User';
|
import { User } from '../models/entities/User';
|
||||||
import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner';
|
import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner';
|
||||||
|
import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam';
|
||||||
|
|
||||||
@JsonController('/stats')
|
@JsonController('/stats')
|
||||||
export class StatsController {
|
export class StatsController {
|
||||||
@ -68,7 +69,12 @@ export class StatsController {
|
|||||||
@OpenAPI({ description: "Returns the top ten teams by distance.", security: [{ "StatsApiToken": [] }] })
|
@OpenAPI({ description: "Returns the top ten teams by distance.", security: [{ "StatsApiToken": [] }] })
|
||||||
async getTopTeamsByDistance() {
|
async getTopTeamsByDistance() {
|
||||||
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ["runners", "runners.scans"] });
|
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ["runners", "runners.scans"] });
|
||||||
return teams.sort((team1, team2) => team1.distance - team2.distance).slice(0, 9);
|
let topTeams = teams.sort((team1, team2) => team1.distance - team2.distance).slice(0, 9);
|
||||||
|
let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
|
||||||
|
topTeams.forEach(team => {
|
||||||
|
responseTeams.push(new ResponseStatsTeam(team));
|
||||||
|
});
|
||||||
|
return responseTeams;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/teams/donations")
|
@Get("/teams/donations")
|
||||||
@ -76,7 +82,12 @@ export class StatsController {
|
|||||||
@OpenAPI({ description: "Returns the top ten teams by donations.", security: [{ "StatsApiToken": [] }] })
|
@OpenAPI({ description: "Returns the top ten teams by donations.", security: [{ "StatsApiToken": [] }] })
|
||||||
async getTopTeamsByDonations() {
|
async getTopTeamsByDonations() {
|
||||||
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ["runners", "runners.scans", "runners.distanceDonations"] });
|
let teams = await getConnection().getRepository(RunnerTeam).find({ relations: ["runners", "runners.scans", "runners.distanceDonations"] });
|
||||||
return teams.sort((team1, team2) => team1.distanceDonationAmount - team2.distanceDonationAmount).slice(0, 9);
|
let topTeams = teams.sort((team1, team2) => team1.distanceDonationAmount - team2.distanceDonationAmount).slice(0, 9);
|
||||||
|
let responseTeams: ResponseStatsTeam[] = new Array<ResponseStatsTeam>();
|
||||||
|
topTeams.forEach(team => {
|
||||||
|
responseTeams.push(new ResponseStatsTeam(team));
|
||||||
|
});
|
||||||
|
return responseTeams;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/organisations/distance")
|
@Get("/organisations/distance")
|
||||||
|
@ -7,29 +7,30 @@ import { Runner } from '../entities/Runner';
|
|||||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the runner response.
|
* Defines the runner stats response.
|
||||||
|
* This differs from the normal runner responce.
|
||||||
*/
|
*/
|
||||||
export class ResponseStatsRunner {
|
export class ResponseStatsRunner {
|
||||||
/**
|
/**
|
||||||
* The participant's id.
|
* The runner's id.
|
||||||
*/
|
*/
|
||||||
@IsInt()
|
@IsInt()
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The participant's first name.
|
* The runner's first name.
|
||||||
*/
|
*/
|
||||||
@IsString()
|
@IsString()
|
||||||
firstname: string;
|
firstname: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The participant's middle name.
|
* The runner's middle name.
|
||||||
*/
|
*/
|
||||||
@IsString()
|
@IsString()
|
||||||
middlename?: string;
|
middlename?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The participant's last name.
|
* The runner's last name.
|
||||||
*/
|
*/
|
||||||
@IsString()
|
@IsString()
|
||||||
lastname: string;
|
lastname: string;
|
||||||
@ -57,10 +58,11 @@ export class ResponseStatsRunner {
|
|||||||
* @param runner The user the response shall be build for.
|
* @param runner The user the response shall be build for.
|
||||||
*/
|
*/
|
||||||
public constructor(runner: Runner) {
|
public constructor(runner: Runner) {
|
||||||
|
this.id = runner.id;
|
||||||
this.firstname = runner.firstname;
|
this.firstname = runner.firstname;
|
||||||
this.middlename = runner.middlename;
|
this.middlename = runner.middlename;
|
||||||
this.lastname = runner.lastname;
|
this.lastname = runner.lastname;
|
||||||
this.distance = runner.scans.filter(scan => { scan.valid === true }).reduce((sum, current) => sum + current.distance, 0);
|
this.distance = runner.distance;
|
||||||
this.donationAmount = runner.distanceDonationAmount;
|
this.donationAmount = runner.distanceDonationAmount;
|
||||||
this.group = runner.group;
|
this.group = runner.group;
|
||||||
}
|
}
|
||||||
|
55
src/models/responses/ResponseStatsTeam.ts
Normal file
55
src/models/responses/ResponseStatsTeam.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import {
|
||||||
|
IsInt,
|
||||||
|
IsObject,
|
||||||
|
IsString
|
||||||
|
} from "class-validator";
|
||||||
|
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||||
|
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the team stats response.
|
||||||
|
* This differs from the normal team responce.
|
||||||
|
*/
|
||||||
|
export class ResponseStatsTeam {
|
||||||
|
/**
|
||||||
|
* The team's id.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The team's name.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The teams's currently ran distance in meters.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
distance: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The teams's currently collected donations.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
donationAmount: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The teams's parent group.
|
||||||
|
*/
|
||||||
|
@IsObject()
|
||||||
|
parent: RunnerGroup;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a ResponseRunner object from a runner.
|
||||||
|
* @param runner The user the response shall be build for.
|
||||||
|
*/
|
||||||
|
public constructor(team: RunnerTeam) {
|
||||||
|
this.name = team.name;
|
||||||
|
this.id = team.id;
|
||||||
|
this.parent = team.parentGroup;
|
||||||
|
this.distance = team.distance;
|
||||||
|
this.donationAmount = team.distanceDonationAmount;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user