All files / src/app/map/maps-database maps-database.service.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                  1x   16x             1x 12x                     1x 1x                   1x     1x           1x                 1x           1x  
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
 
import { ABAMap, EsriMap } from './utils';
import { mapServerUrl } from 'src/config/urls';
 
@Injectable({
  providedIn: 'root'
})
export class MapsDatabaseService {
 
  constructor(private http: HttpClient) { }
 
  /**
   * Get the list of all existing maps on the maps server.
   *
   * @returns A Promise containing the list of maps saved on the maps server.
   */
  async getMapsList(): Promise<ABAMap[]> {
    return this.http.get<ABAMap[]>(`${mapServerUrl}/maps/`).toPromise();
  }
 
  /**
   * Retrieve a map with a given uid from the maps servers.
   *
   * @param uid The uid of the map to retrieve on the maps server.
   *
   * @returns An ABAMap containing the information stored on the server
   * for the map with the input uid.
   */
  async getMap(uid: number): Promise<ABAMap> {
    return this.http.get<ABAMap>(`${mapServerUrl}/maps/${uid}`).toPromise();
  }
 
  /**
   * Save a new map on the maps server.
   *
   * @param esriMap An EsriMap for which a new map must be saved on the server.
   *
   * @returns An object containing the uid of the newly saved map.
   */
  async saveMap(esriMap: EsriMap): Promise<{id: number}> {
    // Set the http header to avoid sending an OPTIONS request before the POST,
    // which would cause CORS issues.
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'text/plain',
      })
    };
 
    return this.http.post<{id: number}>(
      `${mapServerUrl}`,
      {
        title: esriMap.title,
        city: esriMap.city,
        height: esriMap.height,
        width: esriMap.width,
        extent: esriMap.extent.toJSON(),
        graphics: esriMap.graphics.map(
          (graphic: __esri.Graphic) => graphic.toJSON()
        )
      },
      httpOptions
    ).toPromise();
  }
}