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 { 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<TableEntry[]> {
|
||||
return this.databaseService.fetchStats()
|
||||
@Get('/stats/season/:id')
|
||||
async getStats(@Param('id') id: string): Promise<UserStatsResponse> {
|
||||
return this.databaseService.fetchStats(id)
|
||||
.then(value => { return value; })
|
||||
.catch(err => {
|
||||
logger.error(`Error occured when fetching stats.`, err);
|
||||
|
||||
@@ -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<TableEntry[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.prismaClient.timetracker.findMany({
|
||||
include: {
|
||||
user: true,
|
||||
ranks: true
|
||||
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
|
||||
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<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 => resolve(result))
|
||||
.catch(err => reject(err));
|
||||
});
|
||||
}
|
||||
.then(result => result.max.season_id)
|
||||
.then(resolve)
|
||||
.catch(reject)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user