From ec64ec3d6326c0afcdff64f782944554c2760b78 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 30 Dec 2020 14:41:07 +0100 Subject: [PATCH] Added a response class for team stats ref #56 --- src/controllers/StatsController.ts | 15 +++++- src/models/responses/ResponseStatsRunner.ts | 14 +++--- src/models/responses/ResponseStatsTeam.ts | 55 +++++++++++++++++++++ 3 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 src/models/responses/ResponseStatsTeam.ts diff --git a/src/controllers/StatsController.ts b/src/controllers/StatsController.ts index 3139dc4..ef4d508 100644 --- a/src/controllers/StatsController.ts +++ b/src/controllers/StatsController.ts @@ -9,6 +9,7 @@ import { RunnerTeam } from '../models/entities/RunnerTeam'; import { Scan } from '../models/entities/Scan'; import { User } from '../models/entities/User'; import { ResponseStatsRunner } from '../models/responses/ResponseStatsRunner'; +import { ResponseStatsTeam } from '../models/responses/ResponseStatsTeam'; @JsonController('/stats') export class StatsController { @@ -68,7 +69,12 @@ export class StatsController { @OpenAPI({ description: "Returns the top ten teams by distance.", security: [{ "StatsApiToken": [] }] }) async getTopTeamsByDistance() { 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(); + topTeams.forEach(team => { + responseTeams.push(new ResponseStatsTeam(team)); + }); + return responseTeams; } @Get("/teams/donations") @@ -76,7 +82,12 @@ export class StatsController { @OpenAPI({ description: "Returns the top ten teams by donations.", security: [{ "StatsApiToken": [] }] }) async getTopTeamsByDonations() { 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(); + topTeams.forEach(team => { + responseTeams.push(new ResponseStatsTeam(team)); + }); + return responseTeams; } @Get("/organisations/distance") diff --git a/src/models/responses/ResponseStatsRunner.ts b/src/models/responses/ResponseStatsRunner.ts index f4d2d2a..a219264 100644 --- a/src/models/responses/ResponseStatsRunner.ts +++ b/src/models/responses/ResponseStatsRunner.ts @@ -7,29 +7,30 @@ import { Runner } from '../entities/Runner'; import { RunnerGroup } from '../entities/RunnerGroup'; /** - * Defines the runner response. + * Defines the runner stats response. + * This differs from the normal runner responce. */ export class ResponseStatsRunner { /** - * The participant's id. + * The runner's id. */ @IsInt() id: number; /** - * The participant's first name. + * The runner's first name. */ @IsString() firstname: string; /** - * The participant's middle name. + * The runner's middle name. */ @IsString() middlename?: string; /** - * The participant's last name. + * The runner's last name. */ @IsString() lastname: string; @@ -57,10 +58,11 @@ export class ResponseStatsRunner { * @param runner The user the response shall be build for. */ public constructor(runner: Runner) { + this.id = runner.id; this.firstname = runner.firstname; this.middlename = runner.middlename; 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.group = runner.group; } diff --git a/src/models/responses/ResponseStatsTeam.ts b/src/models/responses/ResponseStatsTeam.ts new file mode 100644 index 0000000..df48362 --- /dev/null +++ b/src/models/responses/ResponseStatsTeam.ts @@ -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; + } +}