From 87b1857c95a2ed747bd23e9a07bf0f2aa67971d3 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 25 May 2023 10:20:00 +0200 Subject: [PATCH] refactor database module --- .../ad/adapters/secondaries/ad.repository.ts | 144 +++++++++--------- .../tests/integration/ad.repository.spec.ts | 2 +- ...tract.ts => prisma.repository.abstract.ts} | 4 +- .../{prisma-service.ts => prisma.service.ts} | 0 src/modules/database/database.module.ts | 7 +- .../database/domain/database.repository.ts | 3 + .../database/domain/matcher-repository.ts | 3 - .../tests/unit/prisma-repository.spec.ts | 4 +- 8 files changed, 82 insertions(+), 85 deletions(-) rename src/modules/database/adapters/secondaries/{prisma-repository.abstract.ts => prisma.repository.abstract.ts} (98%) rename src/modules/database/adapters/secondaries/{prisma-service.ts => prisma.service.ts} (100%) create mode 100644 src/modules/database/domain/database.repository.ts delete mode 100644 src/modules/database/domain/matcher-repository.ts diff --git a/src/modules/ad/adapters/secondaries/ad.repository.ts b/src/modules/ad/adapters/secondaries/ad.repository.ts index ea580c2..51ff9e7 100644 --- a/src/modules/ad/adapters/secondaries/ad.repository.ts +++ b/src/modules/ad/adapters/secondaries/ad.repository.ts @@ -1,13 +1,13 @@ 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 { DatabaseException } from '../../../database/exceptions/database.exception'; @Injectable() -export class AdRepository extends MatcherRepository { +export class AdRepository extends DatabaseRepository { protected model = 'ad'; - async createAd(ad: Partial): Promise { + createAd = async (ad: Partial): Promise => { try { const affectedRowNumber = await this.createWithFields( this.createFields(ad), @@ -19,77 +19,75 @@ export class AdRepository extends MatcherRepository { } catch (e) { throw e; } - } + }; - private createFields(ad: Partial): Partial { - return { - uuid: `'${ad.uuid}'`, - userUuid: `'${ad.userUuid}'`, - driver: ad.driver ? 'true' : 'false', - passenger: ad.passenger ? 'true' : 'false', - frequency: `'${ad.frequency}'`, - fromDate: `'${ad.fromDate.getFullYear()}-${ - ad.fromDate.getMonth() + 1 - }-${ad.fromDate.getDate()}'`, - toDate: `'${ad.toDate.getFullYear()}-${ - ad.toDate.getMonth() + 1 - }-${ad.toDate.getDate()}'`, - monTime: ad.monTime - ? `'${ad.monTime.getFullYear()}-${ - ad.monTime.getMonth() + 1 - }-${ad.monTime.getDate()}T${ad.monTime.getHours()}:${ad.monTime.getMinutes()}Z'` - : 'NULL', - tueTime: ad.tueTime - ? `'${ad.tueTime.getFullYear()}-${ - ad.tueTime.getMonth() + 1 - }-${ad.tueTime.getDate()}T${ad.tueTime.getHours()}:${ad.tueTime.getMinutes()}Z'` - : 'NULL', - wedTime: ad.wedTime - ? `'${ad.wedTime.getFullYear()}-${ - ad.wedTime.getMonth() + 1 - }-${ad.wedTime.getDate()}T${ad.wedTime.getHours()}:${ad.wedTime.getMinutes()}Z'` - : 'NULL', - thuTime: ad.thuTime - ? `'${ad.thuTime.getFullYear()}-${ - ad.thuTime.getMonth() + 1 - }-${ad.thuTime.getDate()}T${ad.thuTime.getHours()}:${ad.thuTime.getMinutes()}Z'` - : 'NULL', - friTime: ad.friTime - ? `'${ad.friTime.getFullYear()}-${ - ad.friTime.getMonth() + 1 - }-${ad.friTime.getDate()}T${ad.friTime.getHours()}:${ad.friTime.getMinutes()}Z'` - : 'NULL', - satTime: ad.satTime - ? `'${ad.satTime.getFullYear()}-${ - ad.satTime.getMonth() + 1 - }-${ad.satTime.getDate()}T${ad.satTime.getHours()}:${ad.satTime.getMinutes()}Z'` - : 'NULL', - sunTime: ad.sunTime - ? `'${ad.sunTime.getFullYear()}-${ - ad.sunTime.getMonth() + 1 - }-${ad.sunTime.getDate()}T${ad.sunTime.getHours()}:${ad.sunTime.getMinutes()}Z'` - : 'NULL', - monMargin: ad.monMargin, - tueMargin: ad.tueMargin, - wedMargin: ad.wedMargin, - thuMargin: ad.thuMargin, - friMargin: ad.friMargin, - satMargin: ad.satMargin, - sunMargin: ad.sunMargin, - fwdAzimuth: ad.fwdAzimuth, - backAzimuth: ad.backAzimuth, - driverDuration: ad.driverDuration ?? 'NULL', - driverDistance: ad.driverDistance ?? 'NULL', - passengerDuration: ad.passengerDuration ?? 'NULL', - passengerDistance: ad.passengerDistance ?? 'NULL', - waypoints: ad.waypoints, - direction: ad.direction, - seatsDriver: ad.seatsDriver, - seatsPassenger: ad.seatsPassenger, - seatsUsed: ad.seatsUsed ?? 0, - strict: ad.strict, - }; - } + private createFields = (ad: Partial): Partial => ({ + uuid: `'${ad.uuid}'`, + userUuid: `'${ad.userUuid}'`, + driver: ad.driver ? 'true' : 'false', + passenger: ad.passenger ? 'true' : 'false', + frequency: `'${ad.frequency}'`, + fromDate: `'${ad.fromDate.getFullYear()}-${ + ad.fromDate.getMonth() + 1 + }-${ad.fromDate.getDate()}'`, + toDate: `'${ad.toDate.getFullYear()}-${ + ad.toDate.getMonth() + 1 + }-${ad.toDate.getDate()}'`, + monTime: ad.monTime + ? `'${ad.monTime.getFullYear()}-${ + ad.monTime.getMonth() + 1 + }-${ad.monTime.getDate()}T${ad.monTime.getHours()}:${ad.monTime.getMinutes()}Z'` + : 'NULL', + tueTime: ad.tueTime + ? `'${ad.tueTime.getFullYear()}-${ + ad.tueTime.getMonth() + 1 + }-${ad.tueTime.getDate()}T${ad.tueTime.getHours()}:${ad.tueTime.getMinutes()}Z'` + : 'NULL', + wedTime: ad.wedTime + ? `'${ad.wedTime.getFullYear()}-${ + ad.wedTime.getMonth() + 1 + }-${ad.wedTime.getDate()}T${ad.wedTime.getHours()}:${ad.wedTime.getMinutes()}Z'` + : 'NULL', + thuTime: ad.thuTime + ? `'${ad.thuTime.getFullYear()}-${ + ad.thuTime.getMonth() + 1 + }-${ad.thuTime.getDate()}T${ad.thuTime.getHours()}:${ad.thuTime.getMinutes()}Z'` + : 'NULL', + friTime: ad.friTime + ? `'${ad.friTime.getFullYear()}-${ + ad.friTime.getMonth() + 1 + }-${ad.friTime.getDate()}T${ad.friTime.getHours()}:${ad.friTime.getMinutes()}Z'` + : 'NULL', + satTime: ad.satTime + ? `'${ad.satTime.getFullYear()}-${ + ad.satTime.getMonth() + 1 + }-${ad.satTime.getDate()}T${ad.satTime.getHours()}:${ad.satTime.getMinutes()}Z'` + : 'NULL', + sunTime: ad.sunTime + ? `'${ad.sunTime.getFullYear()}-${ + ad.sunTime.getMonth() + 1 + }-${ad.sunTime.getDate()}T${ad.sunTime.getHours()}:${ad.sunTime.getMinutes()}Z'` + : 'NULL', + monMargin: ad.monMargin, + tueMargin: ad.tueMargin, + wedMargin: ad.wedMargin, + thuMargin: ad.thuMargin, + friMargin: ad.friMargin, + satMargin: ad.satMargin, + sunMargin: ad.sunMargin, + fwdAzimuth: ad.fwdAzimuth, + backAzimuth: ad.backAzimuth, + driverDuration: ad.driverDuration ?? 'NULL', + driverDistance: ad.driverDistance ?? 'NULL', + passengerDuration: ad.passengerDuration ?? 'NULL', + passengerDistance: ad.passengerDistance ?? 'NULL', + waypoints: ad.waypoints, + direction: ad.direction, + seatsDriver: ad.seatsDriver, + seatsPassenger: ad.seatsPassenger, + seatsUsed: ad.seatsUsed ?? 0, + strict: ad.strict, + }); } type AdFields = { diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 1947bac..7df5c09 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -1,6 +1,6 @@ import { TestingModule, Test } from '@nestjs/testing'; 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 { Ad } from '../../domain/entities/ad'; import { Frequency } from '../../domain/types/frequency.enum'; diff --git a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/adapters/secondaries/prisma.repository.abstract.ts similarity index 98% rename from src/modules/database/adapters/secondaries/prisma-repository.abstract.ts rename to src/modules/database/adapters/secondaries/prisma.repository.abstract.ts index 02c5cf9..1ab4b67 100644 --- a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/adapters/secondaries/prisma.repository.abstract.ts @@ -3,10 +3,10 @@ import { Prisma } from '@prisma/client'; import { DatabaseException } from '../../exceptions/database.exception'; import { ICollection } from '../../interfaces/collection.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() export abstract class PrismaRepository implements IRepository { diff --git a/src/modules/database/adapters/secondaries/prisma-service.ts b/src/modules/database/adapters/secondaries/prisma.service.ts similarity index 100% rename from src/modules/database/adapters/secondaries/prisma-service.ts rename to src/modules/database/adapters/secondaries/prisma.service.ts diff --git a/src/modules/database/database.module.ts b/src/modules/database/database.module.ts index 5b09ac3..f98fa94 100644 --- a/src/modules/database/database.module.ts +++ b/src/modules/database/database.module.ts @@ -1,9 +1,8 @@ import { Module } from '@nestjs/common'; -import { PrismaService } from './adapters/secondaries/prisma-service'; -import { AdRepository } from '../ad/adapters/secondaries/ad.repository'; +import { PrismaService } from './adapters/secondaries/prisma.service'; @Module({ - providers: [PrismaService, AdRepository], - exports: [PrismaService, AdRepository], + providers: [PrismaService], + exports: [PrismaService], }) export class DatabaseModule {} diff --git a/src/modules/database/domain/database.repository.ts b/src/modules/database/domain/database.repository.ts new file mode 100644 index 0000000..83f17e7 --- /dev/null +++ b/src/modules/database/domain/database.repository.ts @@ -0,0 +1,3 @@ +import { PrismaRepository } from '../adapters/secondaries/prisma.repository.abstract'; + +export class DatabaseRepository extends PrismaRepository {} diff --git a/src/modules/database/domain/matcher-repository.ts b/src/modules/database/domain/matcher-repository.ts deleted file mode 100644 index 553a100..0000000 --- a/src/modules/database/domain/matcher-repository.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { PrismaRepository } from '../adapters/secondaries/prisma-repository.abstract'; - -export class MatcherRepository extends PrismaRepository {} diff --git a/src/modules/database/tests/unit/prisma-repository.spec.ts b/src/modules/database/tests/unit/prisma-repository.spec.ts index eb3bad0..d27b1fe 100644 --- a/src/modules/database/tests/unit/prisma-repository.spec.ts +++ b/src/modules/database/tests/unit/prisma-repository.spec.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { PrismaService } from '../../adapters/secondaries/prisma-service'; -import { PrismaRepository } from '../../adapters/secondaries/prisma-repository.abstract'; +import { PrismaService } from '../../adapters/secondaries/prisma.service'; +import { PrismaRepository } from '../../adapters/secondaries/prisma.repository.abstract'; import { DatabaseException } from '../../exceptions/database.exception'; import { Prisma } from '@prisma/client';