diff --git a/server/src/modules/points-of-interest/points-of-interest.controller.ts b/server/src/modules/points-of-interest/points-of-interest.controller.ts index d0d1ee8..df8793b 100644 --- a/server/src/modules/points-of-interest/points-of-interest.controller.ts +++ b/server/src/modules/points-of-interest/points-of-interest.controller.ts @@ -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 { @@ -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); } } diff --git a/server/src/modules/routes/routes.controller.ts b/server/src/modules/routes/routes.controller.ts index 2de2e97..bee462e 100644 --- a/server/src/modules/routes/routes.controller.ts +++ b/server/src/modules/routes/routes.controller.ts @@ -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'; @@ -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); } @@ -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); + } } diff --git a/server/src/modules/routes/routes.service.ts b/server/src/modules/routes/routes.service.ts index f9f7539..5884cba 100644 --- a/server/src/modules/routes/routes.service.ts +++ b/server/src/modules/routes/routes.service.ts @@ -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 @@ -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, @@ -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(); + } + } }