Merge branch 'feature/interfaces' into 'develop'

Feature/interfaces

See merge request tjohn/cc-data!2
This commit is contained in:
Patrick Gebhardt 2020-06-14 20:57:15 +02:00
commit 65219907f8
6 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,7 @@
/** Represents the structure of a search preset. */
export interface Preset {
preset_id: number;
parameter: string;
tag_label: string;
value: number[];
}

View File

@ -0,0 +1,6 @@
/** Represents the structure of a region. */
export interface Region {
region_id: number;
name: string;
country: string;
}

View File

@ -0,0 +1,15 @@
import {SearchParam} from './search-request.interface';
/** Represents the structure of one search result. */
export interface Result {
region_id: string;
region_name: string;
score: number;
scores: Score[];
}
export interface Score {
type: SearchParam;
value: number;
score: number;
}

View File

@ -0,0 +1,25 @@
export enum SearchParam {
FROM = 'from',
TO = 'to',
TEMPERATURE = 'temperature',
PRECIPITATION = 'precipitation',
RAINDAYS = 'raindays',
HUMIDITY = 'humidity',
SUNHOURS = 'sunhours',
ALCOHOL = 'alcohol',
FOOD = 'food'
}
export interface SearchParams {
from: number;
to: number;
price?: number[];
exclude_region_ids?: number[];
temperature?: number[];
precipitation?: number[];
raindays?: number[];
humidity?: number[];
sunhours?: number[];
alcohol?: number[];
food?: number[];
}

View File

@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { DataService } from './data.service';
describe('DataService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: DataService = TestBed.get(DataService);
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,36 @@
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 {SearchParams} from '../interfaces/search-request.interface';
import {Region} from '../interfaces/region.interface';
@Injectable({
providedIn: 'root'
})
export class DataService {
private readonly API_URL = 'https://example.com/api/v1/';
constructor(private http: HttpClient) {
}
public searchRegions(searchParams: SearchParams): Promise<Result[]> {
const params = new HttpParams();
params.append('q', btoa(JSON.stringify(searchParams)));
return this.http.get<Result[]>(this.API_URL + 'search', {params}).toPromise();
}
public getAllPresets(): Promise<Preset[]> {
return this.http.get<Preset[]>(this.API_URL + 'preset').toPromise();
}
public getAllRegions(): Promise<Region[]> {
return this.http.get<Region[]>(this.API_URL + 'region').toPromise();
}
public toMinMaxArray(min: number, max: number): number[] {
return [min, max];
}
}