mirror of
https://gitlab.com/mobicoop/v3/service/territory.git
synced 2026-03-22 16:55:50 +00:00
basic insert
This commit is contained in:
@@ -5,6 +5,7 @@ import { DatabaseException } from '../../exceptions/database.exception';
|
||||
import { ICollection } from '../../interfaces/collection.interface';
|
||||
import { IRepository } from '../../interfaces/repository.interface';
|
||||
import { PrismaService } from './prisma-service';
|
||||
import { Territory } from 'src/modules/territories/domain/entities/territory';
|
||||
|
||||
/**
|
||||
* Child classes MUST redefined _model property with appropriate model name
|
||||
@@ -185,11 +186,22 @@ export abstract class PrismaRepository<T> implements IRepository<T> {
|
||||
}
|
||||
|
||||
async findForPoint(point: Point): Promise<ICollection<T>> {
|
||||
const strPoint = `SELECT uuid, name FROM ${this._model} WHERE ST_Intersects('POINT(${point.lon} ${point.lat})',shape) = true`;
|
||||
const strPoint = `SELECT uuid, name FROM ${this._model} WHERE ST_Intersects(ST_GeomFromText('POINT(${point.lon} ${point.lat})',4326),shape) = true`;
|
||||
const territories: Array<T> = await this._prisma.$queryRawUnsafe(strPoint);
|
||||
return Promise.resolve({
|
||||
data: territories,
|
||||
total: territories.length,
|
||||
});
|
||||
}
|
||||
|
||||
async createTerritory(territory: Territory): Promise<T> {
|
||||
const command = `INSERT INTO ${this._model} VALUES ('bb281075-1b98-4456-89d6-c643d3044a91','${territory.name}', ST_GeomFromGeoJSON('{"type":"MultiPolygon","coordinates":${territory.shape}}'),'2023-02-07 15:58:00','2023-02-07 15:58:00')`;
|
||||
const affectedRowNumber = await this._prisma.$executeRawUnsafe(command);
|
||||
if (affectedRowNumber == 1) {
|
||||
return this.findOne({
|
||||
name: territory.name,
|
||||
});
|
||||
}
|
||||
throw new DatabaseException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user