From 15a191a34206ba726000bba9fd2a20b42365fb99 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Thu, 19 Nov 2020 18:34:44 +0100
Subject: [PATCH] fix: address query

---
 src/app/home/home.component.spec.ts           |  6 ++---
 src/app/home/home.component.ts                |  6 ++---
 src/app/map/components/map.component.ts       |  6 ++---
 src/app/models/structure.model.ts             |  3 ++-
 src/app/services/geojson.service.spec.ts      | 23 ++++---------------
 src/app/services/geojson.service.ts           | 14 ++---------
 .../components/card/card.component.ts         | 12 ----------
 7 files changed, 17 insertions(+), 53 deletions(-)

diff --git a/src/app/home/home.component.spec.ts b/src/app/home/home.component.spec.ts
index 6aeea5f08..d389cc50d 100644
--- a/src/app/home/home.component.spec.ts
+++ b/src/app/home/home.component.spec.ts
@@ -34,10 +34,10 @@ describe('HomeComponent', () => {
 
   it('getCoord(): should get coord', async () => {
     await new Promise((resolve) => {
-      component.getCoord(21356).subscribe(
+      component.getCoord('Rue de la Mairie ', 'Feyzin').subscribe(
         (val) => {
-          expect(val.geometry.getLat()).toEqual(69800);
-          expect(val.geometry.getLon).toEqual(69800);
+          expect(val.geometry.getLat()).toEqual(4.8591584);
+          expect(val.geometry.getLon()).toEqual(45.6727968);
           resolve();
         },
         (err) => {
diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts
index c96c6f15b..78dfe60ef 100644
--- a/src/app/home/home.component.ts
+++ b/src/app/home/home.component.ts
@@ -94,7 +94,7 @@ export class HomeComponent implements OnInit {
    */
   private getStructurePosition(structure: Structure): Promise<Structure> {
     return new Promise((resolve, reject) => {
-      this.getCoord(structure.voie).subscribe((coord: GeoJson) => {
+      this.getCoord(structure.voie, structure.commune).subscribe((coord: GeoJson) => {
         structure.address = coord.properties.name + ' - ' + coord.properties.postcode + ' ' + coord.properties.city;
         structure.distance = parseInt(
           this.geoJsonService.getDistance(
@@ -115,8 +115,8 @@ export class HomeComponent implements OnInit {
    * Get coord with a street reference
    * @param idVoie Street reference
    */
-  public getCoord(idVoie: number): Observable<GeoJson> {
-    return this.geoJsonService.getAddressByIdVoie(idVoie).pipe(mergeMap((res) => this.geoJsonService.getCoord(res)));
+  public getCoord(voie: string, zipcode: string): Observable<GeoJson> {
+    return this.geoJsonService.getCoord(voie, zipcode);
   }
 
   public getLocation(): void {
diff --git a/src/app/map/components/map.component.ts b/src/app/map/components/map.component.ts
index 29937bddd..f41fb5da9 100644
--- a/src/app/map/components/map.component.ts
+++ b/src/app/map/components/map.component.ts
@@ -107,7 +107,7 @@ export class MapComponent implements OnChanges {
 
   private getStructuresPositions(structureListe: Structure[]): void {
     structureListe.forEach((element: Structure) => {
-      this.getCoord(element.voie).subscribe((coord: GeoJson) => {
+      this.getCoord(element.voie, element.commune).subscribe((coord: GeoJson) => {
         this.mapService
           .createMarker(coord.geometry.getLon(), coord.geometry.getLat(), element.id, this.buildToolTip(element))
           .addTo(this.map)
@@ -151,8 +151,8 @@ export class MapComponent implements OnChanges {
    * Get coord with a street reference
    * @param idVoie Street reference
    */
-  public getCoord(idVoie: number): Observable<GeoJson> {
-    return this.geoJsonService.getAddressByIdVoie(idVoie).pipe(mergeMap((res) => this.geoJsonService.getCoord(res)));
+  public getCoord(voie: string, zipcode: string): Observable<GeoJson> {
+    return this.geoJsonService.getCoord(voie, zipcode);
   }
 
   /**
diff --git a/src/app/models/structure.model.ts b/src/app/models/structure.model.ts
index 35d8222e5..221dd15cf 100644
--- a/src/app/models/structure.model.ts
+++ b/src/app/models/structure.model.ts
@@ -13,7 +13,8 @@ export class Structure {
   public typeDeStructure: string;
   public description: string;
   public n: string;
-  public voie: number;
+  public voie: string;
+  public commune: string;
   public telephone: string;
   public courriel: string;
   public siteWeb: string;
diff --git a/src/app/services/geojson.service.spec.ts b/src/app/services/geojson.service.spec.ts
index f6b6c9bda..a947152a8 100644
--- a/src/app/services/geojson.service.spec.ts
+++ b/src/app/services/geojson.service.spec.ts
@@ -22,27 +22,12 @@ describe('GeojsonService', () => {
     expect(service).toBeTruthy();
   });
 
-  it('should get address for id 26061 ', async () => {
+  it('should get coord with query string Rue de la Mairie Feyzin ', async () => {
     await new Promise((resolve) => {
-      service.getAddressByIdVoie(26061).subscribe(
+      service.getCoord('Rue de la Mairie', 'Feyzin').subscribe(
         (val) => {
-          expect(val.zipcode).toEqual('69800');
-          expect(val.text).toEqual('13ème Rue Cité Berliet');
-          resolve();
-        },
-        (err) => {
-          resolve();
-        }
-      );
-    });
-  });
-
-  it('should get coord with query string avenue foch 69006 ', async () => {
-    await new Promise((resolve) => {
-      service.getCoord(new Address({ text: 'avenue foch', citycode: '69006' })).subscribe(
-        (val) => {
-          expect(val.geometry.getLat()).toEqual(4.8429024);
-          expect(val.geometry.getLon()).toEqual(45.7733884);
+          expect(val.geometry.getLat()).toEqual(4.8591584);
+          expect(val.geometry.getLon()).toEqual(45.6727968);
           resolve();
         },
         (err) => {
diff --git a/src/app/services/geojson.service.ts b/src/app/services/geojson.service.ts
index 36c0f3f56..ef92bb87a 100644
--- a/src/app/services/geojson.service.ts
+++ b/src/app/services/geojson.service.ts
@@ -12,16 +12,6 @@ import * as _ from 'lodash';
 export class GeojsonService {
   constructor(private http: HttpClient) {}
 
-  /**
-   * Retrive an address with a street national reference
-   * @param idVoie Number
-   */
-  public getAddressByIdVoie(idVoie: number): Observable<Address> {
-    return this.http
-      .get('/base-adresse/base-adresse-nationale/streets' + '?id=' + idVoie)
-      .pipe(map((data: { data: any[]; err: number }) => new Address(data.data[0])));
-  }
-
   /**
    * Retrive an address by geolocation
    * @param idVoie Number
@@ -57,9 +47,9 @@ export class GeojsonService {
    * Get GeoLocation with an address
    * @param address Address
    */
-  public getCoord(address: Address): Observable<GeoJson> {
+  public getCoord(address: string, zipcode: string): Observable<GeoJson> {
     return this.http
-      .get('/geocoding/photon/api' + '?q=' + address.queryString())
+      .get('/geocoding/photon/api' + '?q=' + address + ' ' + zipcode)
       .pipe(map((data: { features: any[]; type: string }) => new GeoJson(data.features[0])));
   }
 
diff --git a/src/app/structure-list/components/card/card.component.ts b/src/app/structure-list/components/card/card.component.ts
index 4050fe759..6af5fbd47 100644
--- a/src/app/structure-list/components/card/card.component.ts
+++ b/src/app/structure-list/components/card/card.component.ts
@@ -1,10 +1,6 @@
 import { Component, Input, Output, OnInit, EventEmitter } from '@angular/core';
 import { Structure } from '../../../models/structure.model';
 import { GeojsonService } from '../../../services/geojson.service';
-import { GeoJson } from '../../../map/models/geojson.model';
-import { Observable } from 'rxjs';
-import { mergeMap } from 'rxjs/operators';
-
 @Component({
   selector: 'app-card',
   templateUrl: './card.component.html',
@@ -29,14 +25,6 @@ export class CardComponent implements OnInit {
     }
   }
 
-  /**
-   * Get coord with a street reference
-   * @param idVoie Street reference
-   */
-  public getCoord(idVoie: number): Observable<GeoJson> {
-    return this.geoJsonService.getAddressByIdVoie(idVoie).pipe(mergeMap((res) => this.geoJsonService.getCoord(res)));
-  }
-
   public cardClicked(): void {
     this.showDetails.emit(this.structure);
   }
-- 
GitLab