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,22 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger"
import { IsNumberString, IsOptional } from "class-validator"
export class GetFiguresDTO {
@ApiProperty()
@IsNumberString()
city_id: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
offset?: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
limit?: number
@ApiProperty()
@IsNumberString()
year: number
}

View File

@ -0,0 +1,19 @@
import { ApiPropertyOptional } from "@nestjs/swagger"
import { IsNumberString, IsOptional } from "class-validator"
export class GetImagesDTO {
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
city_id?: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
offset?: number
@ApiPropertyOptional()
@IsNumberString()
@IsOptional()
limit?: number
}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { EmsController } from './ems.controller';
describe('EmsController', () => {
let controller: EmsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [EmsController],
}).compile();
controller = module.get<EmsController>(EmsController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,26 @@
import { Controller, Get, Query } from '@nestjs/common';
import { EmsService } from './ems.service';
import { GetImagesDTO } from './dto/get-images';
import { GetFiguresDTO } from './dto/get-figures';
@Controller('ems')
export class EmsController {
constructor(private readonly emsService: EmsService) { }
@Get('/regions')//✅
async getRegions() {
return this.emsService.getTypeRoles()
}
@Get('/images')//✅
async getImages(@Query() getImagesDTO: GetImagesDTO) {
const { city_id, limit, offset } = getImagesDTO
return this.emsService.getImages(city_id, offset, limit)
}
@Get('/figures')
async getFigures(@Query() getFiguresDTO: GetFiguresDTO) {
const { offset, limit, year, city_id } = getFiguresDTO
return this.emsService.getFigures(year, city_id, offset, limit)
}
}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { EmsController } from './ems.controller';
import { EmsService } from './ems.service';
@Module({
controllers: [EmsController],
providers: [EmsService]
})
export class EmsModule {}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { EmsService } from './ems.service';
describe('EmsService', () => {
let service: EmsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [EmsService],
}).compile();
service = module.get<EmsService>(EmsService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
@Injectable()
export class EmsService {
constructor(
@InjectDataSource('emsConnection')
private dataSource: DataSource
) { }
async getTypeRoles(): Promise<any[]> {
const result = await this.dataSource.query(`
SELECT * FROM "TypeRoles";
`)
return result
}
async getImages(city_id?: number, offset?: number, limit?: number): Promise<any[]> {
const result = await this.dataSource.query(`
SELECT * FROM "images"
${city_id ? `WHERE city_id = ${city_id}` : ''}
ORDER BY city_id
OFFSET ${offset || 0} ROWS
FETCH NEXT ${limit || 10} ROWS ONLY;
`)
return result
}
async getFigures(year: number, city_id: number, offset?: number, limit?: number): Promise<any[]> {
const result = await this.dataSource.query(`
SELECT * FROM figures f
JOIN 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
}
}