140 lines
3.2 KiB
TypeScript
140 lines
3.2 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
|
import {Preset} from '../interfaces/preset.interface';
|
|
import {Result} from '../interfaces/result.interface';
|
|
import {Region} from '../interfaces/region.interface';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class DataService {
|
|
|
|
private readonly API_URL = 'https://travopti.de/api/v1';
|
|
|
|
private readonly regionCache = new Map<number, Region>();
|
|
|
|
constructor(private http: HttpClient) {
|
|
}
|
|
|
|
public searchRegions(query: string): Promise<Result[]> {
|
|
const params = new HttpParams().set('q', query);
|
|
|
|
return this.http.get<Result[]>(this.API_URL + '/search', {params}).toPromise();
|
|
// return new Promise<Result[]>(resolve => {
|
|
// setTimeout(() => {
|
|
// resolve(MOCK_RESULT);
|
|
// }, 100);
|
|
// });
|
|
}
|
|
|
|
public getAllPresets(): Promise<Preset[]> {
|
|
return this.http.get<Preset[]>(this.API_URL + '/search/presets').toPromise();
|
|
// return new Promise<Preset[]>(resolve => {
|
|
// setTimeout(() => {
|
|
// resolve(MOCK_PRESETS);
|
|
// }, 100);
|
|
// });
|
|
}
|
|
|
|
public async getAllRegions(max?: number): Promise<Region[]> {
|
|
let params = new HttpParams();
|
|
if (max) {
|
|
params = params.set('randomize', max.toString(10));
|
|
}
|
|
const regions = await this.http.get<Region[]>(this.API_URL + '/regions', {params}).toPromise();
|
|
|
|
regions.forEach(region => this.regionCache.set(region.region_id, region));
|
|
|
|
return regions;
|
|
// return new Promise<Region[]>(resolve => {
|
|
// setTimeout(() => {
|
|
// resolve(MOCK_REGIONS);
|
|
// }, 100);
|
|
// });
|
|
}
|
|
|
|
public async getRegion(id: number): Promise<Region> {
|
|
if (this.regionCache.has(id)) {
|
|
return this.regionCache.get(id);
|
|
}
|
|
|
|
const region = await this.http.get<Region>(`${this.API_URL}/regions/${id}`).toPromise();
|
|
this.regionCache.set(id, region);
|
|
|
|
return region;
|
|
// return new Promise<Region>(resolve => {
|
|
// setTimeout(() => {
|
|
// resolve(MOCK_REGIONS.find(region => region.region_id === id));
|
|
// }, 100);
|
|
// });
|
|
}
|
|
}
|
|
|
|
export const REGION_PARAM_VIS: RegionParamVisLookup = {
|
|
temperature_mean: {
|
|
icon: 'wb_sunny',
|
|
unit: '°C'
|
|
},
|
|
temperature_mean_min: {
|
|
icon: 'wb_sunny',
|
|
unit: '°C'
|
|
},
|
|
temperature_mean_max: {
|
|
icon: 'wb_sunny',
|
|
unit: '°C'
|
|
},
|
|
precipitation: {
|
|
icon: 'opacity',
|
|
unit: 'mm'
|
|
},
|
|
humidity: {
|
|
icon: 'grain',
|
|
unit: '%'
|
|
},
|
|
sun_hours: {
|
|
icon: 'flare',
|
|
unit: 'h'
|
|
},
|
|
rain_days: {
|
|
icon: 'date_range',
|
|
unit: ''
|
|
},
|
|
food_costs: {
|
|
icon: 'local_dining',
|
|
unit: '€/day'
|
|
},
|
|
alcohol_costs: {
|
|
icon: 'local_bar',
|
|
unit: '€/day'
|
|
},
|
|
water_costs: {
|
|
icon: 'local_cafe',
|
|
unit: '€/day'
|
|
},
|
|
local_transportation_costs: {
|
|
icon: 'commute',
|
|
unit: '€/day'
|
|
},
|
|
entertainment_costs: {
|
|
icon: 'local_activity',
|
|
unit: '€/day'
|
|
},
|
|
accommodation_costs: {
|
|
icon: 'hotel',
|
|
unit: '€/day'
|
|
},
|
|
average_per_day_costs: {
|
|
icon: 'euro',
|
|
unit: '€/day'
|
|
}
|
|
};
|
|
|
|
export interface RegionParamVisLookup {
|
|
[key: string]: RegionParamVis;
|
|
}
|
|
|
|
export interface RegionParamVis {
|
|
icon: string;
|
|
unit: string;
|
|
}
|