add georouter

This commit is contained in:
sbriat 2023-04-12 17:09:31 +02:00
parent a30b19e2f3
commit 4c3195390e
8 changed files with 119 additions and 36 deletions

View File

@ -93,7 +93,8 @@
".presenter.ts",
".profile.ts",
".exception.ts",
"main.ts"
"main.ts",
"prisma-service.ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
@ -110,7 +111,8 @@
".presenter.ts",
".profile.ts",
".exception.ts",
"main.ts"
"main.ts",
"prisma-service.ts"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"

View File

@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
import { Prisma } from '@prisma/client';
import { DatabaseException } from '../../exceptions/database.exception';
import { ICollection } from '../../interfaces/collection.interface';
import { IRepository } from '../../interfaces/repository.interface';
@ -45,9 +45,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return entity;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -66,8 +66,11 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return entity;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
throw new DatabaseException(PrismaClientKnownRequestError.name, e.code);
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
Prisma.PrismaClientKnownRequestError.name,
e.code,
);
} else {
throw new DatabaseException();
}
@ -85,9 +88,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return res;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -105,9 +108,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
});
return updatedEntity;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -131,9 +134,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return updatedEntity;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -151,9 +154,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return entity;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -171,9 +174,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
return entity;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -204,9 +207,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
)}) VALUES (${Object.values(fields).join(',')})`;
return await this._prisma.$executeRawUnsafe(command);
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -225,9 +228,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
)} WHERE uuid = '${uuid}'`;
return await this._prisma.$executeRawUnsafe(command);
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);
@ -242,9 +245,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
await this._prisma.$queryRaw`SELECT 1`;
return true;
} catch (e) {
if (e instanceof PrismaClientKnownRequestError) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
throw new DatabaseException(
PrismaClientKnownRequestError.name,
Prisma.PrismaClientKnownRequestError.name,
e.code,
e.message,
);

View File

@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract';
import { DatabaseException } from '../../src/exceptions/database.exception';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
import { Prisma } from '@prisma/client';
class FakeEntity {
uuid?: string;
@ -66,7 +66,7 @@ const mockPrismaService = {
.mockResolvedValueOnce(fakeEntityCreated)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((fields: object) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -78,7 +78,7 @@ const mockPrismaService = {
.mockResolvedValueOnce(fakeEntityCreated)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((fields: object) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -90,7 +90,7 @@ const mockPrismaService = {
$queryRaw: jest
.fn()
.mockImplementationOnce(() => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -99,7 +99,7 @@ const mockPrismaService = {
return true;
})
.mockImplementation(() => {
throw new PrismaClientKnownRequestError('Database unavailable', {
throw new Prisma.PrismaClientKnownRequestError('Database unavailable', {
code: 'code',
clientVersion: 'version',
});
@ -110,7 +110,7 @@ const mockPrismaService = {
.mockResolvedValueOnce(fakeEntityCreated)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -139,7 +139,7 @@ const mockPrismaService = {
}
if (!entity && params?.where?.uuid == 'unknown') {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -161,7 +161,7 @@ const mockPrismaService = {
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -175,14 +175,14 @@ const mockPrismaService = {
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -212,7 +212,7 @@ const mockPrismaService = {
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});
@ -236,7 +236,7 @@ const mockPrismaService = {
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((params?: any) => {
throw new PrismaClientKnownRequestError('unknown request', {
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
code: 'code',
clientVersion: 'version',
});

View File

@ -0,0 +1,13 @@
import { Georouter } from '../../domain/interfaces/georouter.interface';
import { GraphhopperGeorouter } from './graphhopper-georouter';
export class GeorouterCreator {
create(type: string, url: string): Georouter {
switch (type) {
case 'graphhopper':
return new GraphhopperGeorouter(url);
default:
throw new Error('Unknown geocoder');
}
}
}

View File

@ -0,0 +1,19 @@
import { Route } from '../../domain/entities/route';
import { Georouter } from '../../domain/interfaces/georouter.interface';
export class GraphhopperGeorouter implements Georouter {
_url: string;
constructor(url: string) {
this._url = url + '/route?';
}
route(
routesRequested: [],
withPoints: boolean,
withTime: boolean,
withDistance: boolean,
): Route[] {
throw new Error('Method not implemented.');
}
}

View File

@ -1,3 +1,10 @@
import { Route } from '../entities/route';
export interface Georouter {
type: string;
route(
routesRequested: [],
withPoints: boolean,
withTime: boolean,
withDistance: boolean,
): Array<Route>;
}

View File

@ -0,0 +1,23 @@
import { GeorouterCreator } from '../../adapters/secondaries/georouter-creator';
import { GraphhopperGeorouter } from '../../adapters/secondaries/graphhopper-georouter';
describe('Georouter creator', () => {
it('should be defined', () => {
const georouterCreator: GeorouterCreator = new GeorouterCreator();
expect(georouterCreator).toBeDefined();
});
it('should create a graphhopper georouter', () => {
const georouterCreator: GeorouterCreator = new GeorouterCreator();
const georouter = georouterCreator.create(
'graphhopper',
'http://localhost',
);
expect(georouter).toBeInstanceOf(GraphhopperGeorouter);
});
it('should throw an exception if georouter type is unknown', () => {
const georouterCreator: GeorouterCreator = new GeorouterCreator();
expect(() =>
georouterCreator.create('unknown', 'http://localhost'),
).toThrow();
});
});

View File

@ -0,0 +1,16 @@
import { GraphhopperGeorouter } from '../../adapters/secondaries/graphhopper-georouter';
describe('Graphhopper Georouter', () => {
it('should be defined', () => {
const graphhopperGeorouter: GraphhopperGeorouter = new GraphhopperGeorouter(
'http://localhost',
);
expect(graphhopperGeorouter).toBeDefined();
});
it('should throw an exception when calling route', () => {
const graphhopperGeorouter: GraphhopperGeorouter = new GraphhopperGeorouter(
'http://localhost',
);
expect(() => graphhopperGeorouter.route([], false, false, false)).toThrow();
});
});