Added a response class for team stats

ref #56
This commit is contained in:
Nicolai Ort 2020-12-30 14:41:07 +01:00
parent 35dbfeb5e7
commit ec64ec3d63
3 changed files with 76 additions and 8 deletions

View File

@ -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")

View File

@ -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;
} }

View 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;
}
}