import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { IItemType } from '../models/item.type'; import { IItem } from '../models/item'; import { IItemTask } from '../models/itemTask'; import { HttpClient, HttpParams } from "@angular/common/http"; import { AppConfig } from "../shared/app.config"; @Injectable({ providedIn: 'root', }) export class ItemService { constructor(public httpClient: HttpClient, public appConfig: AppConfig) { } ApiEndpoint() { return this.appConfig.getConfig("apiEndPoint"); } parseDates(item: any): IItem { item.created = new Date(Date.parse(item.created)); item.updated = new Date(Date.parse(item.updated)); item.dataDate = new Date(Date.parse(item.dataDate)); return item; } getItemTypes(): Observable<{ [id: string]: IItemType }> { return this.httpClient.get<{ [id: string]: IItemType }>(`${this.ApiEndpoint()}/api/v1/itemtypes/`); } getFeatures(extent: number[], crs: string, searchText?: string, searchTags?:string,startDate?:Date,endDate?:Date,itemType?:string,parentCode?:string,dataFilter?:string,level?:number): Observable { var params = new HttpParams(); params = params.append("bbox", extent.join(",")); params = params.append("crs", crs); if (searchText) params = params.append("q", searchText); if (searchTags) params = params.append("t", searchTags); if (startDate) params = params.append("sd", startDate.toISOString()); if (endDate) params = params.append("ed", endDate.toISOString()); if (itemType) params = params.append("it", itemType); if (parentCode) params = params.append("pc", parentCode); if (dataFilter) params = params.append("df", dataFilter); if (level) params = params.append("lvl", dataFilter); return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/features/`, {params:params}); } getFeature(code:string, crs: string): Observable { var params = new HttpParams(); params = params.append("crs", crs); return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/feature/`, { params: params }); } getItem(code: string): Observable { return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}`).pipe(map(i => this.parseDates(i))); } getItemByCodeAndType(code: string, itemType: string): Observable { return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/${itemType}`); } getItemList(itemType: string, dataFilter?: any, level: number = 1): Observable { var params = new HttpParams(); params = params.append("it", itemType); if(dataFilter != null){ params = params.append("df", JSON.stringify(dataFilter)); } params = params.append("lvl", itemType); return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/`, { params: params }).pipe(map(ia => ia.map(i => this.parseDates(i)))); } getChildItemList(parentcode: string, itemType: string, dataFilter?: any, level: number = 1): Observable { var params = new HttpParams(); params = params.append("it", itemType); if (dataFilter != null) { params = params.append("df", JSON.stringify(dataFilter)); } params = params.append("lvl", level.toString()); return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${parentcode}/children`, { params: params }).pipe(map(ia => ia.map(i => this.parseDates(i)))); } getChildItemListByExtent(parentcode: string, itemType: string, extent: number[], crs: string, dataFilter?: any, level: number = 1): Observable { var params = new HttpParams(); params = params.append("it", itemType); params = params.append("bbox", extent.join(",")); params = params.append("crs", crs); if (dataFilter != null) { params = params.append("df", JSON.stringify(dataFilter)); } params = params.append("lvl", level.toString()); return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${parentcode}/children`, { params: params }).pipe(map(ia => ia.map(i => this.parseDates(i)))); } getItemFeatures(code: string, extent: number[], crs: string, layerIndex?:number): Observable { var params = new HttpParams(); params = params.append("bbox", extent.join(",")); params = params.append("crs", crs); if(layerIndex!=null) return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/features/layer/${layerIndex}`, { params: params }); else return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/features`, { params: params }); } putItem(item:IItem): Observable { return this.httpClient.put(`${this.ApiEndpoint()}/api/v1/items/${item.code}`,item); } deleteItems(itemCodes:string[]): Observable { return this.httpClient.post(`${this.ApiEndpoint()}/api/v1/items/delete`, itemCodes); } getTemporalLast(code: string): Observable { return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/temporal/last`); } getTemporal(code: string, startDate?: Date, endDate?: Date): Observable { var params = new HttpParams(); if (startDate) params = params.append("sd", startDate.toISOString()); if (endDate) params = params.append("ed", endDate.toISOString()); return this.httpClient.get(`${this.ApiEndpoint()}/api/v1/items/${code}/temporal/`, { params: params }); } postItemTask(item: IItem, task: IItemTask): Observable { return this.httpClient.post(`${this.ApiEndpoint()}/api/v1/items/${item.code}/tasks`, task); } }