generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from moevm/Dima
Dima
- Loading branch information
Showing
16 changed files
with
470 additions
and
0 deletions.
There are no files selected for viewing
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,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); | ||
} | ||
} |
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 { DotsService } from './dots.service'; | ||
import { DotsController } from './dots.controller'; | ||
|
||
@Module({ | ||
controllers: [DotsController], | ||
providers: [DotsService], | ||
}) | ||
export class DotsModule {} |
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,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(); | ||
} | ||
} | ||
|
||
} |
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 @@ | ||
import Point from "./point"; | ||
import {IsObject} from "class-validator"; | ||
import {Type} from "class-transformer"; | ||
|
||
export class CreateDotDto { | ||
@IsObject() | ||
@ValidateNested() | ||
@Type(() => Point) | ||
location: Point; | ||
} |
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 @@ | ||
import {IsNumber} from "class-validator"; | ||
|
||
export default class Point { | ||
|
||
@IsNumber() | ||
latitude: number; | ||
|
||
@IsNumber() | ||
longitude: number; | ||
} |
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,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; | ||
} | ||
|
||
} |
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,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; | ||
} |
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,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; | ||
} | ||
|
||
} |
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,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); | ||
} | ||
} |
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 { RoutesService } from './routes.service'; | ||
import { RoutesController } from './routes.controller'; | ||
|
||
@Module({ | ||
controllers: [RoutesController], | ||
providers: [RoutesService], | ||
}) | ||
export class RoutesModule {} |
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,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(); | ||
} | ||
} | ||
|
||
} |
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,6 @@ | ||
import {IsNumber} from "class-validator"; | ||
|
||
export class CreateWalkDto { | ||
@IsNumber() | ||
length: number; | ||
} |
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,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; | ||
} | ||
|
||
} |
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,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); | ||
} | ||
} |
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 { WalksService } from './walks.service'; | ||
import { WalksController } from './walks.controller'; | ||
|
||
@Module({ | ||
controllers: [WalksController], | ||
providers: [WalksService], | ||
}) | ||
export class WalksModule {} |
Oops, something went wrong.