add georouter
This commit is contained in:
parent
a30b19e2f3
commit
4c3195390e
|
@ -93,7 +93,8 @@
|
||||||
".presenter.ts",
|
".presenter.ts",
|
||||||
".profile.ts",
|
".profile.ts",
|
||||||
".exception.ts",
|
".exception.ts",
|
||||||
"main.ts"
|
"main.ts",
|
||||||
|
"prisma-service.ts"
|
||||||
],
|
],
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"testRegex": ".*\\.spec\\.ts$",
|
"testRegex": ".*\\.spec\\.ts$",
|
||||||
|
@ -110,7 +111,8 @@
|
||||||
".presenter.ts",
|
".presenter.ts",
|
||||||
".profile.ts",
|
".profile.ts",
|
||||||
".exception.ts",
|
".exception.ts",
|
||||||
"main.ts"
|
"main.ts",
|
||||||
|
"prisma-service.ts"
|
||||||
],
|
],
|
||||||
"coverageDirectory": "../coverage",
|
"coverageDirectory": "../coverage",
|
||||||
"testEnvironment": "node"
|
"testEnvironment": "node"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
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';
|
||||||
|
@ -45,9 +45,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -66,8 +66,11 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(PrismaClientKnownRequestError.name, e.code);
|
throw new DatabaseException(
|
||||||
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
|
e.code,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new DatabaseException();
|
throw new DatabaseException();
|
||||||
}
|
}
|
||||||
|
@ -85,9 +88,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -105,9 +108,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
});
|
});
|
||||||
return updatedEntity;
|
return updatedEntity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -131,9 +134,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return updatedEntity;
|
return updatedEntity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -151,9 +154,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -171,9 +174,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -204,9 +207,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
)}) VALUES (${Object.values(fields).join(',')})`;
|
)}) VALUES (${Object.values(fields).join(',')})`;
|
||||||
return await this._prisma.$executeRawUnsafe(command);
|
return await this._prisma.$executeRawUnsafe(command);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -225,9 +228,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
)} WHERE uuid = '${uuid}'`;
|
)} WHERE uuid = '${uuid}'`;
|
||||||
return await this._prisma.$executeRawUnsafe(command);
|
return await this._prisma.$executeRawUnsafe(command);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
@ -242,9 +245,9 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||||
await this._prisma.$queryRaw`SELECT 1`;
|
await this._prisma.$queryRaw`SELECT 1`;
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
PrismaClientKnownRequestError.name,
|
Prisma.PrismaClientKnownRequestError.name,
|
||||||
e.code,
|
e.code,
|
||||||
e.message,
|
e.message,
|
||||||
);
|
);
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
|
import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
|
||||||
import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract';
|
import { PrismaRepository } from '../../src/adapters/secondaries/prisma-repository.abstract';
|
||||||
import { DatabaseException } from '../../src/exceptions/database.exception';
|
import { DatabaseException } from '../../src/exceptions/database.exception';
|
||||||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime';
|
import { Prisma } from '@prisma/client';
|
||||||
|
|
||||||
class FakeEntity {
|
class FakeEntity {
|
||||||
uuid?: string;
|
uuid?: string;
|
||||||
|
@ -66,7 +66,7 @@ const mockPrismaService = {
|
||||||
.mockResolvedValueOnce(fakeEntityCreated)
|
.mockResolvedValueOnce(fakeEntityCreated)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((fields: object) => {
|
.mockImplementationOnce((fields: object) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -78,7 +78,7 @@ const mockPrismaService = {
|
||||||
.mockResolvedValueOnce(fakeEntityCreated)
|
.mockResolvedValueOnce(fakeEntityCreated)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((fields: object) => {
|
.mockImplementationOnce((fields: object) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -90,7 +90,7 @@ const mockPrismaService = {
|
||||||
$queryRaw: jest
|
$queryRaw: jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -99,7 +99,7 @@ const mockPrismaService = {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.mockImplementation(() => {
|
.mockImplementation(() => {
|
||||||
throw new PrismaClientKnownRequestError('Database unavailable', {
|
throw new Prisma.PrismaClientKnownRequestError('Database unavailable', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -110,7 +110,7 @@ const mockPrismaService = {
|
||||||
.mockResolvedValueOnce(fakeEntityCreated)
|
.mockResolvedValueOnce(fakeEntityCreated)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -139,7 +139,7 @@ const mockPrismaService = {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!entity && params?.where?.uuid == 'unknown') {
|
if (!entity && params?.where?.uuid == 'unknown') {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -161,7 +161,7 @@ const mockPrismaService = {
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -175,14 +175,14 @@ const mockPrismaService = {
|
||||||
.fn()
|
.fn()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -212,7 +212,7 @@ const mockPrismaService = {
|
||||||
.fn()
|
.fn()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
@ -236,7 +236,7 @@ const mockPrismaService = {
|
||||||
.fn()
|
.fn()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
.mockImplementationOnce((params?: any) => {
|
.mockImplementationOnce((params?: any) => {
|
||||||
throw new PrismaClientKnownRequestError('unknown request', {
|
throw new Prisma.PrismaClientKnownRequestError('unknown request', {
|
||||||
code: 'code',
|
code: 'code',
|
||||||
clientVersion: 'version',
|
clientVersion: 'version',
|
||||||
});
|
});
|
||||||
|
|
|
@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.');
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,10 @@
|
||||||
|
import { Route } from '../entities/route';
|
||||||
|
|
||||||
export interface Georouter {
|
export interface Georouter {
|
||||||
type: string;
|
route(
|
||||||
|
routesRequested: [],
|
||||||
|
withPoints: boolean,
|
||||||
|
withTime: boolean,
|
||||||
|
withDistance: boolean,
|
||||||
|
): Array<Route>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue