Merge branch 'refactorDatabaseModule' into 'main'

refactor database module

See merge request v3/service/matcher!8
This commit is contained in:
Sylvain Briat 2023-05-25 08:32:08 +00:00
commit a14ac74d5b
8 changed files with 82 additions and 85 deletions

View File

@ -1,13 +1,13 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { MatcherRepository } from '../../../database/domain/matcher-repository'; import { DatabaseRepository } from '../../../database/domain/database.repository';
import { Ad } from '../../domain/entities/ad'; import { Ad } from '../../domain/entities/ad';
import { DatabaseException } from '../../../database/exceptions/database.exception'; import { DatabaseException } from '../../../database/exceptions/database.exception';
@Injectable() @Injectable()
export class AdRepository extends MatcherRepository<Ad> { export class AdRepository extends DatabaseRepository<Ad> {
protected model = 'ad'; protected model = 'ad';
async createAd(ad: Partial<Ad>): Promise<Ad> { createAd = async (ad: Partial<Ad>): Promise<Ad> => {
try { try {
const affectedRowNumber = await this.createWithFields( const affectedRowNumber = await this.createWithFields(
this.createFields(ad), this.createFields(ad),
@ -19,77 +19,75 @@ export class AdRepository extends MatcherRepository<Ad> {
} catch (e) { } catch (e) {
throw e; throw e;
} }
} };
private createFields(ad: Partial<Ad>): Partial<AdFields> { private createFields = (ad: Partial<Ad>): Partial<AdFields> => ({
return { uuid: `'${ad.uuid}'`,
uuid: `'${ad.uuid}'`, userUuid: `'${ad.userUuid}'`,
userUuid: `'${ad.userUuid}'`, driver: ad.driver ? 'true' : 'false',
driver: ad.driver ? 'true' : 'false', passenger: ad.passenger ? 'true' : 'false',
passenger: ad.passenger ? 'true' : 'false', frequency: `'${ad.frequency}'`,
frequency: `'${ad.frequency}'`, fromDate: `'${ad.fromDate.getFullYear()}-${
fromDate: `'${ad.fromDate.getFullYear()}-${ ad.fromDate.getMonth() + 1
ad.fromDate.getMonth() + 1 }-${ad.fromDate.getDate()}'`,
}-${ad.fromDate.getDate()}'`, toDate: `'${ad.toDate.getFullYear()}-${
toDate: `'${ad.toDate.getFullYear()}-${ ad.toDate.getMonth() + 1
ad.toDate.getMonth() + 1 }-${ad.toDate.getDate()}'`,
}-${ad.toDate.getDate()}'`, monTime: ad.monTime
monTime: ad.monTime ? `'${ad.monTime.getFullYear()}-${
? `'${ad.monTime.getFullYear()}-${ ad.monTime.getMonth() + 1
ad.monTime.getMonth() + 1 }-${ad.monTime.getDate()}T${ad.monTime.getHours()}:${ad.monTime.getMinutes()}Z'`
}-${ad.monTime.getDate()}T${ad.monTime.getHours()}:${ad.monTime.getMinutes()}Z'` : 'NULL',
: 'NULL', tueTime: ad.tueTime
tueTime: ad.tueTime ? `'${ad.tueTime.getFullYear()}-${
? `'${ad.tueTime.getFullYear()}-${ ad.tueTime.getMonth() + 1
ad.tueTime.getMonth() + 1 }-${ad.tueTime.getDate()}T${ad.tueTime.getHours()}:${ad.tueTime.getMinutes()}Z'`
}-${ad.tueTime.getDate()}T${ad.tueTime.getHours()}:${ad.tueTime.getMinutes()}Z'` : 'NULL',
: 'NULL', wedTime: ad.wedTime
wedTime: ad.wedTime ? `'${ad.wedTime.getFullYear()}-${
? `'${ad.wedTime.getFullYear()}-${ ad.wedTime.getMonth() + 1
ad.wedTime.getMonth() + 1 }-${ad.wedTime.getDate()}T${ad.wedTime.getHours()}:${ad.wedTime.getMinutes()}Z'`
}-${ad.wedTime.getDate()}T${ad.wedTime.getHours()}:${ad.wedTime.getMinutes()}Z'` : 'NULL',
: 'NULL', thuTime: ad.thuTime
thuTime: ad.thuTime ? `'${ad.thuTime.getFullYear()}-${
? `'${ad.thuTime.getFullYear()}-${ ad.thuTime.getMonth() + 1
ad.thuTime.getMonth() + 1 }-${ad.thuTime.getDate()}T${ad.thuTime.getHours()}:${ad.thuTime.getMinutes()}Z'`
}-${ad.thuTime.getDate()}T${ad.thuTime.getHours()}:${ad.thuTime.getMinutes()}Z'` : 'NULL',
: 'NULL', friTime: ad.friTime
friTime: ad.friTime ? `'${ad.friTime.getFullYear()}-${
? `'${ad.friTime.getFullYear()}-${ ad.friTime.getMonth() + 1
ad.friTime.getMonth() + 1 }-${ad.friTime.getDate()}T${ad.friTime.getHours()}:${ad.friTime.getMinutes()}Z'`
}-${ad.friTime.getDate()}T${ad.friTime.getHours()}:${ad.friTime.getMinutes()}Z'` : 'NULL',
: 'NULL', satTime: ad.satTime
satTime: ad.satTime ? `'${ad.satTime.getFullYear()}-${
? `'${ad.satTime.getFullYear()}-${ ad.satTime.getMonth() + 1
ad.satTime.getMonth() + 1 }-${ad.satTime.getDate()}T${ad.satTime.getHours()}:${ad.satTime.getMinutes()}Z'`
}-${ad.satTime.getDate()}T${ad.satTime.getHours()}:${ad.satTime.getMinutes()}Z'` : 'NULL',
: 'NULL', sunTime: ad.sunTime
sunTime: ad.sunTime ? `'${ad.sunTime.getFullYear()}-${
? `'${ad.sunTime.getFullYear()}-${ ad.sunTime.getMonth() + 1
ad.sunTime.getMonth() + 1 }-${ad.sunTime.getDate()}T${ad.sunTime.getHours()}:${ad.sunTime.getMinutes()}Z'`
}-${ad.sunTime.getDate()}T${ad.sunTime.getHours()}:${ad.sunTime.getMinutes()}Z'` : 'NULL',
: 'NULL', monMargin: ad.monMargin,
monMargin: ad.monMargin, tueMargin: ad.tueMargin,
tueMargin: ad.tueMargin, wedMargin: ad.wedMargin,
wedMargin: ad.wedMargin, thuMargin: ad.thuMargin,
thuMargin: ad.thuMargin, friMargin: ad.friMargin,
friMargin: ad.friMargin, satMargin: ad.satMargin,
satMargin: ad.satMargin, sunMargin: ad.sunMargin,
sunMargin: ad.sunMargin, fwdAzimuth: ad.fwdAzimuth,
fwdAzimuth: ad.fwdAzimuth, backAzimuth: ad.backAzimuth,
backAzimuth: ad.backAzimuth, driverDuration: ad.driverDuration ?? 'NULL',
driverDuration: ad.driverDuration ?? 'NULL', driverDistance: ad.driverDistance ?? 'NULL',
driverDistance: ad.driverDistance ?? 'NULL', passengerDuration: ad.passengerDuration ?? 'NULL',
passengerDuration: ad.passengerDuration ?? 'NULL', passengerDistance: ad.passengerDistance ?? 'NULL',
passengerDistance: ad.passengerDistance ?? 'NULL', waypoints: ad.waypoints,
waypoints: ad.waypoints, direction: ad.direction,
direction: ad.direction, seatsDriver: ad.seatsDriver,
seatsDriver: ad.seatsDriver, seatsPassenger: ad.seatsPassenger,
seatsPassenger: ad.seatsPassenger, seatsUsed: ad.seatsUsed ?? 0,
seatsUsed: ad.seatsUsed ?? 0, strict: ad.strict,
strict: ad.strict, });
};
}
} }
type AdFields = { type AdFields = {

View File

@ -1,6 +1,6 @@
import { TestingModule, Test } from '@nestjs/testing'; import { TestingModule, Test } from '@nestjs/testing';
import { DatabaseModule } from '../../../database/database.module'; import { DatabaseModule } from '../../../database/database.module';
import { PrismaService } from '../../../database/adapters/secondaries/prisma-service'; import { PrismaService } from '../../../database/adapters/secondaries/prisma.service';
import { AdRepository } from '../../adapters/secondaries/ad.repository'; import { AdRepository } from '../../adapters/secondaries/ad.repository';
import { Ad } from '../../domain/entities/ad'; import { Ad } from '../../domain/entities/ad';
import { Frequency } from '../../domain/types/frequency.enum'; import { Frequency } from '../../domain/types/frequency.enum';

View File

@ -3,10 +3,10 @@ import { Prisma } from '@prisma/client';
import { DatabaseException } from '../../exceptions/database.exception'; import { DatabaseException } from '../../exceptions/database.exception';
import { ICollection } from '../../interfaces/collection.interface'; import { ICollection } from '../../interfaces/collection.interface';
import { IRepository } from '../../interfaces/repository.interface'; import { IRepository } from '../../interfaces/repository.interface';
import { PrismaService } from './prisma-service'; import { PrismaService } from './prisma.service';
/** /**
* Child classes MUST redefined _model property with appropriate model name * Child classes MUST redefined model property with appropriate model name
*/ */
@Injectable() @Injectable()
export abstract class PrismaRepository<T> implements IRepository<T> { export abstract class PrismaRepository<T> implements IRepository<T> {

View File

@ -1,9 +1,8 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { PrismaService } from './adapters/secondaries/prisma-service'; import { PrismaService } from './adapters/secondaries/prisma.service';
import { AdRepository } from '../ad/adapters/secondaries/ad.repository';
@Module({ @Module({
providers: [PrismaService, AdRepository], providers: [PrismaService],
exports: [PrismaService, AdRepository], exports: [PrismaService],
}) })
export class DatabaseModule {} export class DatabaseModule {}

View File

@ -0,0 +1,3 @@
import { PrismaRepository } from '../adapters/secondaries/prisma.repository.abstract';
export class DatabaseRepository<T> extends PrismaRepository<T> {}

View File

@ -1,3 +0,0 @@
import { PrismaRepository } from '../adapters/secondaries/prisma-repository.abstract';
export class MatcherRepository<T> extends PrismaRepository<T> {}

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../../adapters/secondaries/prisma-service'; import { PrismaService } from '../../adapters/secondaries/prisma.service';
import { PrismaRepository } from '../../adapters/secondaries/prisma-repository.abstract'; import { PrismaRepository } from '../../adapters/secondaries/prisma.repository.abstract';
import { DatabaseException } from '../../exceptions/database.exception'; import { DatabaseException } from '../../exceptions/database.exception';
import { Prisma } from '@prisma/client'; import { Prisma } from '@prisma/client';