diff --git a/backend/src/app.controller.ts b/backend/src/app.controller.ts index a168a4f..73651b5 100644 --- a/backend/src/app.controller.ts +++ b/backend/src/app.controller.ts @@ -1,9 +1,10 @@ -import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common'; +import { Controller, Get, HostParam, HttpException, HttpStatus, Param, Req } from '@nestjs/common'; import { AppService } from './app.service'; import { DatabaseService } from './database/database.service'; import logger from './logger/Logger'; import { TableEntry } from "./models/TableEntry"; import { SinusBotService } from "./services/sinusbot.service"; +import { UserStatsResponse } from './models/aliases'; @Controller() export class AppController { @@ -23,9 +24,9 @@ export class AppController { return await this.sinusBotService.fetchStats(); } - @Get('/stats') - async getStats(): Promise { - return this.databaseService.fetchStats() + @Get('/stats/season/:id') + async getStats(@Param('id') id: string): Promise { + return this.databaseService.fetchStats(id) .then(value => { return value; }) .catch(err => { logger.error(`Error occured when fetching stats.`, err); diff --git a/backend/src/database/database.service.ts b/backend/src/database/database.service.ts index ded8efd..a545261 100644 --- a/backend/src/database/database.service.ts +++ b/backend/src/database/database.service.ts @@ -1,8 +1,8 @@ import { Injectable } from '@nestjs/common'; -import { PrismaClient } from '@prisma/client'; +import { PrismaClient, seasons, timetracker, user, ranks } from '@prisma/client'; import logger from 'src/logger/Logger'; -import { TableEntry } from 'src/models/TableEntry'; import { TimeTrackerPredicate } from './timetracking.predicate'; +import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases'; @Injectable() export class DatabaseService { @@ -19,17 +19,78 @@ export class DatabaseService { private readonly timetrackerPredicate: TimeTrackerPredicate ) {} - public fetchStats(): Promise { - return new Promise((resolve, reject) => { - this.prismaClient.timetracker.findMany({ - include: { - user: true, - ranks: true + public fetchSeasonInfos = async (seasonId: string): Promise => { + let seasons + return this.prismaClient.seasons.findOne({ + where: { + // eslint-disable-next-line @typescript-eslint/camelcase + season_id: Number(seasonId) + } + }) + .then(result => seasons = result) + .then(this.fetchMaxSeasonId) + .then(result => { + seasons.maxSeasonId = result + return seasons + }) + }; + + public fetchStats = async (seasonId: string): Promise => { + let response + return this.fetchSeasonInfos(seasonId) + .then(result => { + response = { + seasonId: result.season_id, + maxSeasonid: result.maxSeasonId, + dates: { + start: result.start_date, + end: result.end_date + } + } + return result.season_id.toString() + }) + .then(this.fetchTimeTrackerStats) + .then(this.timetrackerPredicate.process) + .then(result => { + response.stats = result + return response + }) + }; + + // public fetchStats = async (seasonId: string): Promise => this.prismaClient.timetracker.findMany({ + // include: { + // user: true, + // ranks: true + // }, + // where: { + // // eslint-disable-next-line @typescript-eslint/camelcase + // season_id: Number(seasonId) + // } + // }) + // .then(this.timetrackerPredicate.process); + + fetchTimeTrackerStats: (string) => Promise = (seasonId: string) => this.prismaClient.timetracker.findMany({ + include: { + user: true, + ranks: true + }, + where: { + // eslint-disable-next-line @typescript-eslint/camelcase + season_id: Number(seasonId) + } + }) + + fetchMaxSeasonId: () => Promise = () => new Promise( + (resolve, reject) => { + this.prismaClient.seasons.aggregate({ + max: { + // eslint-disable-next-line @typescript-eslint/camelcase + season_id: true } }) - .then(this.timetrackerPredicate.process) - .then(result => resolve(result)) - .catch(err => reject(err)); - }); - } + .then(result => result.max.season_id) + .then(resolve) + .catch(reject) + } + ) }