nestjs rewrite

This commit is contained in:
popovspiridon99
2025-08-01 11:33:40 +09:00
parent 145827ab6d
commit 37bfa912a0
58 changed files with 17130 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import { Entity, PrimaryGeneratedColumn } from "typeorm"
// @Entity()
// export class Bound {
// @PrimaryGeneratedColumn()
// id:
// }

View 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();
});
});

View 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()
}
}

View 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 {}

View 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();
});
});

View 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
}
}