Skip to content

Commit

Permalink
first approach correct query for feature collection
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 30, 2024
1 parent be6e426 commit 2b0a701
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 55 deletions.
59 changes: 59 additions & 0 deletions api/src/modules/projects/projects-map.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { Project } from '@shared/entities/projects.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { ProjectMap } from '@shared/dtos/projects/projects-map.dto';

@Injectable()
export class ProjectsMapRepository extends Repository<Project> {
constructor(
@InjectRepository(Project)
private readonly projectRepo: Repository<Project>,
) {
super(projectRepo.target, projectRepo.manager, projectRepo.queryRunner);
}

async getProjectsMap(): Promise<ProjectMap> {
const geoQueryBuilder = this.manager.createQueryBuilder();
geoQueryBuilder
.select(
`
json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(country.geometry)::jsonb,
'properties', json_build_object(
'country', country.name,
'abatementPotential', filtered_projects.total_abatement_potential,
'cost', filtered_projects.total_cost
)
)
)
) as geojson
`,
)
.from('countries', 'country')
.innerJoin(
(subQuery) => {
return subQuery
.select('p.country_code')
.from(Project, 'p')
.addSelect(
'SUM(p.abatement_potential)',
'total_abatement_potential',
)
.addSelect('SUM(p.total_cost)', 'total_cost')
.groupBy('p.country_code');
},
'filtered_projects',
'filtered_projects.country_code = country.code',
);

const { geojson } = await geoQueryBuilder.getRawOne<{
geojson: ProjectMap;
}>();
return geojson;
}
}
46 changes: 0 additions & 46 deletions api/src/modules/projects/projects-map/projects-map.service.ts

This file was deleted.

17 changes: 9 additions & 8 deletions api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { projectsContract } from '@shared/contracts/projects.contract';
import { ProjectsService } from '@api/modules/projects/projects.service';
import { CountriesService } from '@api/modules/countries/countries.service';
import { CountryWithNoGeometry } from '@shared/entities/country.entity';
import { ProjectMap } from '@shared/dtos/projects/projects-map.dto';
import { ProjectsMapRepository } from '@api/modules/projects/projects-map.repository';

@Controller()
export class ProjectsController {
constructor(
private readonly projectsService: ProjectsService,
private readonly countryService: CountriesService,
private readonly projectMapRepository: ProjectsMapRepository,
) {}

@TsRestHandler(projectsContract.getProjects)
Expand Down Expand Up @@ -41,13 +42,13 @@ export class ProjectsController {
});
}

// @TsRestHandler(projectsContract.getProjectsMap)
// async getProjectsMap(): ControllerResponse {
// return tsRestHandler(projectsContract.getProjectsMap, async () => {
// const data = await this.projectsService.projectMaps.getMap();
// return { body: null, status: HttpStatus.OK } as any;
// });
// }
@TsRestHandler(projectsContract.getProjectsMap)
async getProjectsMap(): ControllerResponse {
return tsRestHandler(projectsContract.getProjectsMap, async () => {
const data = await this.projectMapRepository.getProjectsMap();
return { body: data, status: HttpStatus.OK } as any;
});
}

@TsRestHandler(projectsContract.getProject)
async getProject(): ControllerResponse {
Expand Down
3 changes: 2 additions & 1 deletion api/src/modules/projects/projects.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Project } from '@shared/entities/projects.entity';
import { ProjectsController } from './projects.controller';
import { ProjectsService } from './projects.service';
import { CountriesModule } from '@api/modules/countries/countries.module';
import { ProjectsMapRepository } from '@api/modules/projects/projects-map.repository';

@Module({
imports: [TypeOrmModule.forFeature([Project]), CountriesModule],
controllers: [ProjectsController],
providers: [ProjectsService],
providers: [ProjectsService, ProjectsMapRepository],
})
export class ProjectsModule {}

0 comments on commit 2b0a701

Please sign in to comment.