From c514337feaacc689fe637fb9827db58fee2f3564 Mon Sep 17 00:00:00 2001 From: Humenius Date: Tue, 12 Jan 2021 14:14:48 +0100 Subject: [PATCH] feature(database-connection): Add TimeTrackerPredicate and PrismaService to providers --- backend/src/app.module.ts | 8 +- backend/src/database/database.service.ts | 174 +++++++++++----------- backend/src/prisma/prisma.service.spec.ts | 18 +++ backend/src/prisma/prisma.service.ts | 14 ++ 4 files changed, 126 insertions(+), 88 deletions(-) create mode 100644 backend/src/prisma/prisma.service.spec.ts create mode 100644 backend/src/prisma/prisma.service.ts diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 9cc6f08..2f8adf8 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -1,17 +1,19 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { SinusBotService } from "./services/sinusbot.service"; +import { SinusBotService } from './services/sinusbot.service'; import { DatabaseService } from './database/database.service'; import { LoggerMiddleware } from './logger.middleware'; +import { PrismaService } from './prisma/prisma.service'; +import { TimeTrackerPredicate } from './database/timetracking.predicate'; @Module({ imports: [], controllers: [AppController], - providers: [AppService, SinusBotService, DatabaseService], + providers: [AppService, SinusBotService, DatabaseService, PrismaService, TimeTrackerPredicate], }) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer): any { - consumer.apply(LoggerMiddleware).forRoutes('*') + consumer.apply(LoggerMiddleware).forRoutes('*'); } } diff --git a/backend/src/database/database.service.ts b/backend/src/database/database.service.ts index a545261..e242f4a 100644 --- a/backend/src/database/database.service.ts +++ b/backend/src/database/database.service.ts @@ -1,96 +1,100 @@ import { Injectable } from '@nestjs/common'; -import { PrismaClient, seasons, timetracker, user, ranks } from '@prisma/client'; -import logger from 'src/logger/Logger'; import { TimeTrackerPredicate } from './timetracking.predicate'; import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases'; +import { PrismaService } from '../prisma/prisma.service'; @Injectable() export class DatabaseService { - // private host = process.env.MYSQL_HOST - // private port = process.env.MYSQL_PORT - // private credentials = { - // username: process.env.MYSQL_USERNAME, - // password: process.env.MYSQL_PASSWORD - // } - // private database = process.env.MYSQL_DATABASE + // private host = process.env.MYSQL_HOST + // private port = process.env.MYSQL_PORT + // private credentials = { + // username: process.env.MYSQL_USERNAME, + // password: process.env.MYSQL_PASSWORD + // } + // private database = process.env.MYSQL_DATABASE - constructor( - private readonly prismaClient: PrismaClient, - private readonly timetrackerPredicate: TimeTrackerPredicate - ) {} + constructor( + private readonly prismaClient: PrismaService, + private readonly timetrackerPredicate: TimeTrackerPredicate, + ) { + } - 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 - }, + 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) - } - }) + // 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; + }); + }; - fetchMaxSeasonId: () => Promise = () => new Promise( - (resolve, reject) => { - this.prismaClient.seasons.aggregate({ - max: { - // eslint-disable-next-line @typescript-eslint/camelcase - season_id: true - } - }) - .then(result => result.max.season_id) - .then(resolve) - .catch(reject) - } - ) + 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(result => result.max.season_id) + .then(resolve) + .catch(reject); + }); } diff --git a/backend/src/prisma/prisma.service.spec.ts b/backend/src/prisma/prisma.service.spec.ts new file mode 100644 index 0000000..a68cb9e --- /dev/null +++ b/backend/src/prisma/prisma.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaService } from './prisma.service'; + +describe('PrismaService', () => { + let service: PrismaService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PrismaService], + }).compile(); + + service = module.get(PrismaService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/backend/src/prisma/prisma.service.ts b/backend/src/prisma/prisma.service.ts new file mode 100644 index 0000000..89ee778 --- /dev/null +++ b/backend/src/prisma/prisma.service.ts @@ -0,0 +1,14 @@ +import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'; +import { PrismaClient } from '@prisma/client'; + +@Injectable() +export class PrismaService extends PrismaClient + implements OnModuleInit, OnModuleDestroy { + async onModuleInit() { + await this.$connect(); + } + + async onModuleDestroy() { + await this.$disconnect(); + } +} \ No newline at end of file