mirror of
https://gitlab.com/mobicoop/v3/service/matcher.git
synced 2026-01-01 14:02:39 +00:00
refactor database module
This commit is contained in:
@@ -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<Ad> {
|
||||
export class AdRepository extends DatabaseRepository<Ad> {
|
||||
protected model = 'ad';
|
||||
|
||||
async createAd(ad: Partial<Ad>): Promise<Ad> {
|
||||
createAd = async (ad: Partial<Ad>): Promise<Ad> => {
|
||||
try {
|
||||
const affectedRowNumber = await this.createWithFields(
|
||||
this.createFields(ad),
|
||||
@@ -19,77 +19,75 @@ export class AdRepository extends MatcherRepository<Ad> {
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private createFields(ad: Partial<Ad>): Partial<AdFields> {
|
||||
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<Ad>): Partial<AdFields> => ({
|
||||
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 = {
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user