Skip to content

Commit

Permalink
Merge pull request #17 from moevm/routes
Browse files Browse the repository at this point in the history
Add route deletion
  • Loading branch information
jonx8 authored Jan 13, 2025
2 parents 8e2c8c0 + b25b06d commit c78e39f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Body, Controller, Get, Param, Post, UsePipes, ValidationPipe} from '@nestjs/common';
import {PointsOfInterestService} from './points-of-interest.service';
import {CreatePointOfInterestDto} from "./dto/create-point-of-interest.dto";
import {Public} from "../authorization/public.decorator";
import { Body, Controller, Get, Param, Post, UsePipes, ValidationPipe } from '@nestjs/common';
import { PointsOfInterestService } from './points-of-interest.service';
import { CreatePointOfInterestDto } from "./dto/create-point-of-interest.dto";
import { Public } from "../authorization/public.decorator";

@Controller("/api/poi")
export class PointsOfInterestController {
Expand All @@ -16,13 +16,13 @@ export class PointsOfInterestController {

@Public()
@Get()
findAll() {
async findAll() {
return this.pointsOfInterestService.findAll();
}

@Public()
@Get(':id')
findOne(@Param('id') id: string) {
async findOne(@Param('id') id: string) {
return this.pointsOfInterestService.findOne(id);
}
}
9 changes: 7 additions & 2 deletions server/src/modules/routes/routes.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Post, Req, UsePipes, ValidationPipe } from '@nestjs/common';
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Req, UsePipes, ValidationPipe } from '@nestjs/common';
import { RoutesService } from './routes.service';
import { Public } from '../authorization/public.decorator';
import { CreateRouteDto } from './dto/create-route.dto';
Expand All @@ -11,7 +11,6 @@ export class RoutesController {
@UsePipes(new ValidationPipe())
@Post()
async create(@Body() createRouteDto: CreateRouteDto, @Req() req: any) {
console.log(req.user)
return this.RoutesService.create(createRouteDto, req.user.sub);
}

Expand All @@ -31,4 +30,10 @@ export class RoutesController {
async findOne(@Param('id') id: string) {
return this.RoutesService.findOne(id);
}

@HttpCode(HttpStatus.NO_CONTENT)
@Delete(':id')
async delete(@Param('id') id: string, @Req() req: any) {
this.RoutesService.delete(id, req.user.sub);
}
}
21 changes: 17 additions & 4 deletions server/src/modules/routes/routes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export class RoutesService {
CALL () {
MATCH
(route)-[inc1: INCLUDE]->
(: PointOfInterest)-[:CLOSE_TO_THE]->(i1: Intersection),
(:PointOfInterest)-[:CLOSE_TO_THE]->(i1: Intersection),
(route)-[inc2: INCLUDE{order: inc1.order + 1}]->
(: PointOfInterest)-[:CLOSE_TO_THE]->(i2: Intersection)
(:PointOfInterest)-[:CLOSE_TO_THE]->(i2: Intersection)
RETURN i1, i2
ORDER BY inc1.order
}
WITH route, i1, i2
WITH DISTINCT route, i1, i2
CALL apoc.algo.aStarConfig(i1, i2, "ROAD_SEGMENT", {pointPropName: "location", weight: "length"})
YIELD weight
WITH SUM(weight) as total_distance, route
Expand Down Expand Up @@ -144,7 +144,6 @@ export class RoutesService {
RETURN route, collect(poi) AS poi_list
`
)
console.log(result.records)
const node = result.records.at(0)?.get('route');
const poiList: PointOfInterest[] = result.records.at(0)?.get('poi_list').map(poi => new PointOfInterest(
poi.elementId,
Expand All @@ -170,4 +169,18 @@ export class RoutesService {
}
}

async delete(id: string, userId: string) {
const session = this.neo4jService.getWriteSession();
try {
await session.run(`
MATCH (user: User WHERE elementId(user) = $userId),
(author: User)-[:CREATED]->(route :Route WHERE elementId(route) = $routeId)
DETACH DELETE CASE elementId(user) = elementId(author) OR user.role = 'ADMIN' WHEN true THEN route END
`,
{ routeId: id, userId: userId }
);
} finally {
await session.close();
}
}
}

0 comments on commit c78e39f

Please sign in to comment.