feature(database-connection): Adapt new requests to database
This commit is contained in:
@@ -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 { AppService } from './app.service';
|
||||||
import { DatabaseService } from './database/database.service';
|
import { DatabaseService } from './database/database.service';
|
||||||
import logger from './logger/Logger';
|
import logger from './logger/Logger';
|
||||||
import { TableEntry } from "./models/TableEntry";
|
import { TableEntry } from "./models/TableEntry";
|
||||||
import { SinusBotService } from "./services/sinusbot.service";
|
import { SinusBotService } from "./services/sinusbot.service";
|
||||||
|
import { UserStatsResponse } from './models/aliases';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class AppController {
|
export class AppController {
|
||||||
@@ -23,9 +24,9 @@ export class AppController {
|
|||||||
return await this.sinusBotService.fetchStats();
|
return await this.sinusBotService.fetchStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('/stats')
|
@Get('/stats/season/:id')
|
||||||
async getStats(): Promise<TableEntry[]> {
|
async getStats(@Param('id') id: string): Promise<UserStatsResponse> {
|
||||||
return this.databaseService.fetchStats()
|
return this.databaseService.fetchStats(id)
|
||||||
.then(value => { return value; })
|
.then(value => { return value; })
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
logger.error(`Error occured when fetching stats.`, err);
|
logger.error(`Error occured when fetching stats.`, err);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
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 logger from 'src/logger/Logger';
|
||||||
import { TableEntry } from 'src/models/TableEntry';
|
|
||||||
import { TimeTrackerPredicate } from './timetracking.predicate';
|
import { TimeTrackerPredicate } from './timetracking.predicate';
|
||||||
|
import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DatabaseService {
|
export class DatabaseService {
|
||||||
@@ -19,17 +19,78 @@ export class DatabaseService {
|
|||||||
private readonly timetrackerPredicate: TimeTrackerPredicate
|
private readonly timetrackerPredicate: TimeTrackerPredicate
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public fetchStats(): Promise<TableEntry[]> {
|
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
|
||||||
return new Promise((resolve, reject) => {
|
let seasons
|
||||||
this.prismaClient.timetracker.findMany({
|
return this.prismaClient.seasons.findOne({
|
||||||
include: {
|
where: {
|
||||||
user: true,
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
ranks: true
|
season_id: Number(seasonId)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(result => seasons = result)
|
||||||
|
.then(this.fetchMaxSeasonId)
|
||||||
|
.then(result => {
|
||||||
|
seasons.maxSeasonId = result
|
||||||
|
return seasons
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
public fetchStats = async (seasonId: string): Promise<UserStatsResponse> => {
|
||||||
|
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<TableEntry[]> => 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<TimeTrackerStats[]> = (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<number> = () => 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 => result.max.season_id)
|
||||||
.then(result => resolve(result))
|
.then(resolve)
|
||||||
.catch(err => reject(err));
|
.catch(reject)
|
||||||
});
|
}
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user