diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index b4aeea9bc83c7d3a6198af2d44909e4300b60f89..b25e60d5e1d9427801066aaad25d787e85a4e62f 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -4,6 +4,7 @@ import { Delete, Get, HttpException, + HttpService, HttpStatus, Param, Post, @@ -29,11 +30,28 @@ import { StructuresService } from './services/structures.service'; @Controller('structures') export class StructuresController { constructor( + private readonly httpService: HttpService, private readonly structureService: StructuresService, private readonly userService: UsersService, private readonly tempUserService: TempUserService ) {} + /** + * Return points of given town exist. + * @param zipcode + * @returns Array of points + */ + @Get('coordinates/:zipcode') + @ApiParam({ name: 'zipcode', type: String, required: true }) + public async getCoordinates(@Param('zipcode') city: string): Promise<any> { + return await this.httpService + .get(encodeURI('https://download.data.grandlyon.com/geocoding/photon-bal/api?q=' + city)) + .toPromise() + .then(async (res) => res.data.features) + .then((data) => data.filter((cityPoint) => cityPoint.properties.city.toLowerCase().includes(city.toLowerCase()))) + .then((data) => data.map((filteredCityPoint) => filteredCityPoint.geometry.coordinates)); + } + @Post() public async create(@Body() createStructureDto: CreateStructureDto): Promise<Structure> { return this.structureService.create(createStructureDto.idUser, createStructureDto.structure);