-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
920a911
commit 60597c2
Showing
10 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- CreateTable | ||
CREATE TABLE "Place" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"type" TEXT NOT NULL, | ||
"tag" TEXT NOT NULL, | ||
"location" geometry(Point, 4326) NOT NULL, | ||
|
||
CONSTRAINT "Place_pkey" PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { IsString, IsNumber, IsLatitude, IsLongitude, ValidateIf, IsNotEmpty, IsArray, IsOptional } from 'class-validator'; | ||
|
||
export class CreatePlaceDto { | ||
@IsString() | ||
name: string; | ||
|
||
@IsString() | ||
type: string; | ||
|
||
@IsString() | ||
tag: string; | ||
|
||
@IsLatitude() | ||
lat: number; | ||
|
||
@IsLongitude() | ||
lon: number; | ||
} | ||
|
||
export class SearchPlaceDto { | ||
@IsArray() | ||
@IsNotEmpty() | ||
geofenceBoundary: number[][]; // Array of [lon, lat] pairs defining the geofence (polygon) | ||
|
||
@IsOptional() | ||
@IsString() | ||
name?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
tag?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
type?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Body, Controller, Get, Post } from "@nestjs/common"; | ||
import { ApiTags } from "@nestjs/swagger"; | ||
import { PlaceService } from "./place.service"; | ||
import { CreatePlaceDto, SearchPlaceDto } from "./dto/place.dto"; | ||
|
||
@ApiTags('/place') | ||
@Controller('place') | ||
export class PlaceController { | ||
constructor(private readonly placeService: PlaceService) {} | ||
|
||
@Post('search') | ||
async searchPlace(@Body() searchPlaceDto: SearchPlaceDto) { | ||
return this.placeService.searchPlaces(searchPlaceDto); | ||
} | ||
|
||
@Post() | ||
async addPlace(@Body() createPlaceDto: CreatePlaceDto) { | ||
return this.placeService.createPlace(createPlaceDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PlaceController } from './place.controller'; | ||
import { PlaceService } from './place.service'; | ||
|
||
@Module({ | ||
controllers: [PlaceController], | ||
providers: [PlaceService], | ||
}) | ||
export class PlaceModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable, Logger } from "@nestjs/common"; | ||
import { CreatePlaceDto, SearchPlaceDto } from "./dto/place.dto"; | ||
import { PrismaService } from "../prisma/prisma.service"; | ||
|
||
@Injectable() | ||
export class PlaceService { | ||
private readonly logger = new Logger(PlaceService.name); | ||
|
||
constructor(private readonly prisma: PrismaService) { } | ||
|
||
async createPlace(createPlaceDto: CreatePlaceDto): Promise<any> { | ||
const { name, type, tag, lat, lon } = createPlaceDto; | ||
const point = `ST_SetSRID(ST_MakePoint(${lon}, ${lat}), 4326)`; | ||
this.logger.debug(`Adding place ${createPlaceDto}`) | ||
return this.prisma.$executeRawUnsafe( | ||
`INSERT INTO "Place" (name, type, tag, location) VALUES ($1, $2, $3, ${point})`, | ||
name, | ||
type, | ||
tag, | ||
); | ||
} | ||
|
||
async searchPlaces(searchPlaceDto: SearchPlaceDto): Promise<any> { | ||
const { geofenceBoundary, name, tag, type } = searchPlaceDto; | ||
|
||
// Create a polygon for the geofence | ||
const polygon = `ST_MakePolygon(ST_GeomFromText('LINESTRING(${geofenceBoundary | ||
.map((point) => point.join(' ')) | ||
.join(', ')}, ${geofenceBoundary[0].join(' ')})', 4326))`; | ||
|
||
// Base query for filtering places | ||
let query = ` | ||
SELECT id, name, type, tag, ST_AsText(location::geometry) as location | ||
FROM "Place" | ||
WHERE ST_Within(location, ${polygon}) | ||
`; | ||
|
||
// Add optional filters | ||
if (name) { | ||
query += ` AND name ILIKE '%${name}%'`; | ||
} | ||
else if (tag) { | ||
query += ` AND tag ILIKE '%${tag}%'`; | ||
} | ||
else if (type) { | ||
query += ` AND type ILIKE '%${type}%'`; | ||
} | ||
|
||
// Execute the query | ||
return this.prisma.$queryRawUnsafe(query); | ||
} | ||
} |