Skip to content

Commit

Permalink
add map contract, type map controller
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 20, 2024
1 parent 652eef8 commit cd22ec0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
23 changes: 14 additions & 9 deletions api/src/modules/countries/map/map.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Controller, Get, Query } from '@nestjs/common';
import { Controller, HttpStatus } from '@nestjs/common';
import { MapRepository } from '@api/modules/countries/map/map.repository';
import { Country } from '@shared/entities/countries/country.entity';
import { GeoJSON } from 'geojson';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { mapContract } from '@shared/contracts/map.contract';

@Controller('map')
@Controller()
export class MapController {
constructor(private readonly mapRepository: MapRepository) {}
@Get('/geojson')
getGeoJson(
@Query('countryCode') countryCode: Country['countryCode'],
): Promise<GeoJSON> {
return this.mapRepository.getGeoJson(countryCode);

@TsRestHandler(mapContract.getGeoFeatures)
async getGeoFeatures(): ControllerResponse {
return tsRestHandler(mapContract.getGeoFeatures, async ({ query }) => {
const geoFeatures = await this.mapRepository.getGeoFeatures(
query.countryCode,
);
return { body: geoFeatures, status: HttpStatus.OK };
});
}
}
8 changes: 5 additions & 3 deletions api/src/modules/countries/map/map.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export class MapRepository extends Repository<Country> {
});
}

const result: FeatureCollection | undefined =
await queryBuilder.getRawOne<FeatureCollection>();
const result: { geojson: FeatureCollection } | undefined =
await queryBuilder.getRawOne<{
geojson: FeatureCollection;
}>();
this.logger.log(`Retrieved geo features`);
if (!result) {
throw new NotFoundException(`Could not retrieve geo features`);
}
return result;
return result.geojson;
}

private getPropertiesQuery(): string {
Expand Down
2 changes: 2 additions & 0 deletions shared/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { adminContract } from "@shared/contracts/admin.contract";
import { authContract } from "@shared/contracts/auth.contract";
import { usersContract } from "@shared/contracts/users.contract";
import { JSONAPIError } from "@shared/dtos/json-api.error";
import { mapContract } from "@shared/contracts/map.contract";

const contract = initContract();

Expand All @@ -11,6 +12,7 @@ export const router = contract.router(
auth: authContract,
admin: adminContract,
user: usersContract,
map: mapContract,
},
{
commonResponses: {
Expand Down
15 changes: 15 additions & 0 deletions shared/contracts/map.contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { initContract } from "@ts-rest/core";
import { z } from "zod";
import { FeatureCollection } from "typeorm";

const contract = initContract();
export const mapContract = contract.router({
getGeoFeatures: {
method: "GET",
path: "/map/geo-features",
responses: {
200: contract.type<FeatureCollection>(),
},
query: z.object({ countryCode: z.string().length(3).optional() }),
},
});

0 comments on commit cd22ec0

Please sign in to comment.