Skip to content

Commit

Permalink
Merge pull request #9 from moevm/Dima
Browse files Browse the repository at this point in the history
Dima
  • Loading branch information
DimaDmitrii authored Dec 20, 2024
2 parents c416f40 + 123d56d commit 1c8b424
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server/src/modules/dot/dots.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Body, Controller, Get, Param, Post, UsePipes, ValidationPipe} from '@nestjs/common';
import {DotsService} from './dots.service';
import {CreateDotDto} from "./dto/create-dot.dto";
import {Public} from "../authorization/public.decorator";

@Controller("/api/dot")
export class DotsController {
constructor(private readonly DotsService: DotsService) {
}

@UsePipes(new ValidationPipe())
@Post()
async create(@Body() createDotDto: CreateDotDto) {
return await this.DotsService.create(createDotDto);
}

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

@Public()
@Get(':id')
findOne(@Param('id') id: string) {
return this.DotsService.findOne(id);
}
}
9 changes: 9 additions & 0 deletions server/src/modules/dot/dots.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { DotsService } from './dots.service';
import { DotsController } from './dots.controller';

@Module({
controllers: [DotsController],
providers: [DotsService],
})
export class DotsModule {}
84 changes: 84 additions & 0 deletions server/src/modules/dot/dots.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {Injectable, NotFoundException} from "@nestjs/common";
import {CreateDotDto} from "./dto/create-dot.dto";
import {Neo4jService} from "../neo4j/neo4j.service";
import Dot from "./entities/dot.entity";


@Injectable()
export class DotsService {

constructor(private readonly neo4jService: Neo4jService) {
}

async create(dto: CreateDotDto) {
const session = this.neo4jService.getWriteSession();
try {
// const result = await session.run(
// `WITH point({latitude: ${dto.location.latitude}, longitude: ${dto.location.longitude}}) AS l
// CALL (l) {
// MATCH (i: Intersection WHERE point.distance(i.location, l) < ${(DotsService.FIND_INTERSECTION_RADIUS)})
// RETURN i
// ORDER BY point.distance(i.location, l) ASC
// LIMIT 1
// }
// WITH i, l
// CREATE (poi :Dot {
// name: "${dto.name}",
// description: "${dto.description}",
// location: l,
// created_at: Datetime()}
// )-[r: CLOSE_TO_THE]->(i)
// RETURN poi`);
// const node = result.records.at(0).get('poi');
// return new Dot(
// node.elementId,
// node.properties.name,
// node.properties.description,
// node.properties.location,
// node.properties.created_at.toString(),
// );
} finally {
await session.close();
}
}

async findAll() {
const session = this.neo4jService.getReadSession();
try {
const result = await session.run(
`MATCH (dot :Dot) RETURN dot`
)
return result.records.map(record => {
const node = record.get('dot');
return new Dot(
node.elementId,
node.properties.location,
node.properties.time.toString()
);
});
} finally {
await session.close();
}
}

async findOne(id: string) {
const session = this.neo4jService.getReadSession();
try {
const result = await session.run(
`MATCH (dot :Dot WHERE elementId(dot) = "${id}") RETURN dot`
)
const node = result.records.at(0)?.get('dot');
if (!node) {
throw new NotFoundException(`Dot with id: ${id} not found`);
}
return new Dot(
node.elementId,
node.properties.location,
node.properties.dot.toString(),
);
} finally {
await session.close();
}
}

}
10 changes: 10 additions & 0 deletions server/src/modules/dot/dto/create-dot.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Point from "./point";
import {IsObject} from "class-validator";
import {Type} from "class-transformer";

export class CreateDotDto {
@IsObject()
@ValidateNested()
@Type(() => Point)
location: Point;
}
10 changes: 10 additions & 0 deletions server/src/modules/dot/dto/point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {IsNumber} from "class-validator";

export default class Point {

@IsNumber()
latitude: number;

@IsNumber()
longitude: number;
}
14 changes: 14 additions & 0 deletions server/src/modules/dot/entities/dot.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Point from "../dto/point";

export default class Dot {
id: string;
time: string;
location: Point;

constructor(id: string, location: Point, time: string) {
this.id = id;
this.location = location;
this.time = time;
}

}
18 changes: 18 additions & 0 deletions server/src/modules/routes/dto/create-route.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {IsObject, IsString, IsNumber, MaxLength, MinLength, ValidateNested} from "class-validator";

export class CreateRouteDto {
@IsString()
@MinLength(4)
@MaxLength(50)
name: string;

@IsString()
@MaxLength(2000)
description: string;

@IsNumber()
length: number;

@IsNumber()
duration: number;
}
19 changes: 19 additions & 0 deletions server/src/modules/routes/entities/routes.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

export default class Route {
id: string;
name: string;
description: string;
length: number;
duration: number;
createdAt: string;

constructor(id: string, name: string, description: string, length: number, duration: number, createdAt: string) {
this.id = id;
this.name = name;
this.description = description;
this.length = length;
this.duration = duration;
this.createdAt = createdAt;
}

}
28 changes: 28 additions & 0 deletions server/src/modules/routes/routes.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Body, Controller, Get, Param, Post, UsePipes, ValidationPipe} from '@nestjs/common';
import {RoutesService} from './routes.service';
import {CreateRouteDto} from "./dto/create-route.dto";
import {Public} from "../authorization/public.decorator";

@Controller("/api/route")
export class RoutesController {
constructor(private readonly RoutesService: RoutesService) {
}

@UsePipes(new ValidationPipe())
@Post()
async create(@Body() createRoutesDto: CreateRouteDto) {
return await this.RoutesService.create(createRoutesDto);
}

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

@Public()
@Get(':id')
findOne(@Param('id') id: string) {
return this.RoutesService.findOne(id);
}
}
9 changes: 9 additions & 0 deletions server/src/modules/routes/routes.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { RoutesService } from './routes.service';
import { RoutesController } from './routes.controller';

@Module({
controllers: [RoutesController],
providers: [RoutesService],
})
export class RoutesModule {}
92 changes: 92 additions & 0 deletions server/src/modules/routes/routes.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {Injectable, NotFoundException} from "@nestjs/common";
import {CreateRouteDto} from "./dto/create-route.dto";
import {Neo4jService} from "../neo4j/neo4j.service";
import Route from "./entities/routes.entity";


@Injectable()
export class RoutesService {
// private static readonly FIND_INTERSECTION_RADIUS: number = 100000;

constructor(private readonly neo4jService: Neo4jService) {
}

async create(dto: CreateRouteDto) {
const session = this.neo4jService.getWriteSession();
try {
// const result = await session.run(
// `WITH point({latitude: ${dto.location.latitude}, longitude: ${dto.location.longitude}}) AS l
// CALL (l) {
// MATCH (i: Intersection WHERE point.distance(i.location, l) < ${(PointsOfInterestService.FIND_INTERSECTION_RADIUS)})
// RETURN i
// ORDER BY point.distance(i.location, l) ASC
// LIMIT 1
// }
// WITH i, l
// CREATE (poi :Route {
// name: "${dto.name}",
// description: "${dto.description}",
// location: l,
// created_at: Datetime()}
// )-[r: CLOSE_TO_THE]->(i)
// RETURN poi`);
// const node = result.records.at(0).get('poi');
// return new Route(
// node.elementId,
// node.properties.name,
// node.properties.description,
// node.properties.length,
// node.properties.duration,
// node.properties.created_at.toString()
// );
} finally {
await session.close();
}
}

async findAll() {
const session = this.neo4jService.getReadSession();
try {
const result = await session.run(
`MATCH (route :Route) RETURN route`
)
return result.records.map(record => {
const node = record.get('route');
return new Route(
node.elementId,
node.properties.name,
node.properties.description,
node.properties.length,
node.properties.duration,
node.properties.created_at.toString()
);
});
} finally {
await session.close();
}
}

async findOne(id: string) {
const session = this.neo4jService.getReadSession();
try {
const result = await session.run(
`MATCH (route :Route WHERE elementId(route) = "${id}") RETURN route`
)
const node = result.records.at(0)?.get('route');
if (!node) {
throw new NotFoundException(`Route with id: ${id} not found`);
}
return new Route(
node.elementId,
node.properties.name,
node.properties.description,
node.properties.length,
node.properties.duration,
node.properties.created_at.toString()
);
} finally {
await session.close();
}
}

}
6 changes: 6 additions & 0 deletions server/src/modules/walks/dto/create-walk.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {IsNumber} from "class-validator";

export class CreateWalkDto {
@IsNumber()
length: number;
}
15 changes: 15 additions & 0 deletions server/src/modules/walks/entities/walks.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

export default class Walk {
id: string;
startTime: string;
endTime: string;
length: number;

constructor(id: string, startTime: string, endTime: string, length: number) {
this.id = id;
this.startTime = startTime;
this.endTime = endTime;
this.length = length;
}

}
28 changes: 28 additions & 0 deletions server/src/modules/walks/walks.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Body, Controller, Get, Param, Post, UsePipes, ValidationPipe} from '@nestjs/common';
import {WalksService} from './walks.service';
import {CreateWalkDto} from "./dto/create-walk.dto";
import {Public} from "../authorization/public.decorator";

@Controller("/api/walk")
export class WalksController {
constructor(private readonly WalksService: WalksService) {
}

@UsePipes(new ValidationPipe())
@Post()
async create(@Body() createWalkDto: CreateWalkDto) {
return await this.WalksService.create(createWalkDto);
}

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

@Public()
@Get(':id')
findOne(@Param('id') id: string) {
return this.WalksService.findOne(id);
}
}
9 changes: 9 additions & 0 deletions server/src/modules/walks/walks.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { WalksService } from './walks.service';
import { WalksController } from './walks.controller';

@Module({
controllers: [WalksController],
providers: [WalksService],
})
export class WalksModule {}
Loading

0 comments on commit 1c8b424

Please sign in to comment.