nestjs rewrite
This commit is contained in:
7
server/src/gis/dto/bound.ts
Normal file
7
server/src/gis/dto/bound.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { Entity, PrimaryGeneratedColumn } from "typeorm"
|
||||
|
||||
// @Entity()
|
||||
// export class Bound {
|
||||
// @PrimaryGeneratedColumn()
|
||||
// id:
|
||||
// }
|
18
server/src/gis/gis.controller.spec.ts
Normal file
18
server/src/gis/gis.controller.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GisController } from './gis.controller';
|
||||
|
||||
describe('GisController', () => {
|
||||
let controller: GisController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [GisController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<GisController>(GisController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
42
server/src/gis/gis.controller.ts
Normal file
42
server/src/gis/gis.controller.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { Controller, Get, Param, ParseIntPipe, Query } from '@nestjs/common';
|
||||
import { GisService } from './gis.service';
|
||||
|
||||
@Controller('gis')
|
||||
export class GisController {
|
||||
constructor(private readonly gisService: GisService) { }
|
||||
|
||||
@Get('/type-roles')
|
||||
async getTypeRoles() {
|
||||
return await this.gisService.getTypeRoles()
|
||||
}
|
||||
|
||||
@Get('/bounds/:entity_type')
|
||||
async getBoundsByEntityType(@Param('entity_type') entity_type: 'region' | 'district' | 'city') {
|
||||
return await this.gisService.getBoundsByEntityType(entity_type)
|
||||
}
|
||||
|
||||
@Get('/bounds/:entity_type/:entity_id')
|
||||
async getBoundsByEntityTypeAndId(@Param('entity_type') entity_type: 'region' | 'district' | 'city', @Param('entity_id', new ParseIntPipe()) entity_id: number) {
|
||||
return await this.gisService.getBoundsByEntityTypeAndId(entity_type, entity_id)
|
||||
}
|
||||
|
||||
@Get('/images/all')
|
||||
async getImages(@Query('offset') offset: number, @Query('limit') limit: number, @Query('city_id') city_id: number) {
|
||||
return await this.gisService.getImages(offset, limit, city_id)
|
||||
}
|
||||
|
||||
@Get('/figures/all')
|
||||
async getFigures(@Query('offset') offset: number, @Query('limit') limit: number, @Query('year') year: number, @Query('city_id') city_id: number) {
|
||||
return await this.gisService.getFigures(offset, limit, year, city_id)
|
||||
}
|
||||
|
||||
@Get('/lines/all')
|
||||
async getLines(@Query('year') year: number, @Query('city_id') city_id: number) {
|
||||
return await this.gisService.getLines(year, city_id)
|
||||
}
|
||||
|
||||
@Get('/regions/borders')
|
||||
async getRegionBorders() {
|
||||
return await this.gisService.getRegionBorders()
|
||||
}
|
||||
}
|
9
server/src/gis/gis.module.ts
Normal file
9
server/src/gis/gis.module.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GisController } from './gis.controller';
|
||||
import { GisService } from './gis.service';
|
||||
|
||||
@Module({
|
||||
controllers: [GisController],
|
||||
providers: [GisService]
|
||||
})
|
||||
export class GisModule {}
|
18
server/src/gis/gis.service.spec.ts
Normal file
18
server/src/gis/gis.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { GisService } from './gis.service';
|
||||
|
||||
describe('GisService', () => {
|
||||
let service: GisService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [GisService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<GisService>(GisService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
110
server/src/gis/gis.service.ts
Normal file
110
server/src/gis/gis.service.ts
Normal file
@ -0,0 +1,110 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class GisService {
|
||||
constructor(
|
||||
@InjectDataSource('sqliteConnection')
|
||||
private dataSource: DataSource,
|
||||
|
||||
@InjectDataSource('emsConnection')
|
||||
private emsDataSource: DataSource
|
||||
) { }
|
||||
|
||||
async getTypeRoles(): Promise<any[]> {
|
||||
const result = await this.emsDataSource.query(`
|
||||
SELECT * FROM New_Gis..TypeRoles;
|
||||
`)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async getBoundsByEntityType(entity_type: 'region' | 'district' | 'city'): Promise<any[]> {
|
||||
const result = await this.dataSource.query(`
|
||||
SELECT entity_id, entity_type, geometry FROM bounds
|
||||
WHERE entity_type = $1
|
||||
`, [entity_type])
|
||||
if (Array.isArray(result)) {
|
||||
if (result.length > 0) {
|
||||
const geometries = result.map((bound: { id: string, entity_id: number, entity_type: string, geometry: string, published_at: string, deleted_at: string | null }) => {
|
||||
return {
|
||||
...(JSON.parse(bound.geometry)),
|
||||
properties: {
|
||||
id: bound.id,
|
||||
entity_id: bound.entity_id,
|
||||
entity_type: bound.entity_type
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return geometries
|
||||
} else {
|
||||
throw new NotFoundException('not found')
|
||||
}
|
||||
} else {
|
||||
throw new NotFoundException('not found')
|
||||
}
|
||||
}
|
||||
|
||||
async getBoundsByEntityTypeAndId(entity_type: 'region' | 'district' | 'city', entity_id: number): Promise<any[]> {
|
||||
const result = await this.dataSource.query(`
|
||||
SELECT entity_id, entity_type, geometry FROM bounds
|
||||
WHERE entity_type = $1
|
||||
AND entity_id = $2
|
||||
`, [entity_type, entity_id])
|
||||
if (Array.isArray(result)) {
|
||||
if (result.length > 0) {
|
||||
return JSON.parse(result[0].geometry)
|
||||
} else {
|
||||
throw new NotFoundException('not found')
|
||||
}
|
||||
} else {
|
||||
throw new NotFoundException('not found')
|
||||
}
|
||||
}
|
||||
|
||||
async getImages(offset: number, limit: number, city_id: number): Promise<any[]> {
|
||||
const result = await this.emsDataSource.query(`
|
||||
SELECT * FROM images
|
||||
${city_id ? `WHERE city_id = ${city_id}` : ''}
|
||||
ORDER BY city_id
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`)
|
||||
return result
|
||||
}
|
||||
|
||||
async getFigures(offset: number, limit: number, year: number, city_id: number): Promise<any[]> {
|
||||
const result = await this.emsDataSource.query(`
|
||||
SELECT * FROM figures f
|
||||
JOIN nGeneral..vObjects o ON f.object_id = o.object_id WHERE o.id_city = ${city_id} AND f.year = ${year}
|
||||
ORDER BY f.year
|
||||
OFFSET ${Number(offset) || 0} ROWS
|
||||
FETCH NEXT ${Number(limit) || 10} ROWS ONLY;
|
||||
`)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async getLines(year: number, city_id: number): Promise<any[]> {
|
||||
const result = await this.emsDataSource.query(
|
||||
`
|
||||
SELECT * FROM New_Gis..lines l
|
||||
JOIN nGeneral..vObjects o ON l.object_id = o.object_id WHERE o.id_city = ${city_id} AND l.year = ${year};
|
||||
`
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
async getRegionBorders(): Promise<any[]> {
|
||||
const result = await this.emsDataSource.query(
|
||||
`
|
||||
SELECT * FROM New_Gis..visual_regions
|
||||
`
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user