From 7e83d3fb953a90db6023620f03ea331806eb31a1 Mon Sep 17 00:00:00 2001 From: Darko Luketic <2694548+dalu@users.noreply.github.com> Date: Wed, 28 Mar 2018 18:23:31 +0200 Subject: [PATCH] angular dataService --- angular/src/app/services/data.service.spec.ts | 15 ++ angular/src/app/services/data.service.ts | 204 ++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 angular/src/app/services/data.service.spec.ts create mode 100644 angular/src/app/services/data.service.ts diff --git a/angular/src/app/services/data.service.spec.ts b/angular/src/app/services/data.service.spec.ts new file mode 100644 index 0000000..97922aa --- /dev/null +++ b/angular/src/app/services/data.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { DataService } from './data.service'; + +describe('DataService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [DataService] + }); + }); + + it('should be created', inject([DataService], (service: DataService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/angular/src/app/services/data.service.ts b/angular/src/app/services/data.service.ts new file mode 100644 index 0000000..f6665a3 --- /dev/null +++ b/angular/src/app/services/data.service.ts @@ -0,0 +1,204 @@ +import {Injectable} from '@angular/core'; +import {HttpClient, HttpParams} from '@angular/common/http'; +import {Observable} from 'rxjs/Observable'; + +@Injectable() +export class DataService { + + private rootURL = '/api/v1/data/'; + + constructor(private http: HttpClient) { + } + + public list(p: DataParams): Observable { + let params = new HttpParams(); + if (p.start !== undefined) { + params = params.append('start', p.start.toString()); + } + if (p.limit !== undefined) { + params = params.append('limit', p.limit.toString()); + } + if (p.sort !== undefined) { + params = params.append('sort', p.sort); + } + + // string + if (p.nameQuery !== undefined) { + params = params.append('name', '1'); + params = params.append('name.operation', p.nameQuery.operation); + params = params.append('name.query', p.nameQuery.query); + if (p.nameQuery.options !== undefined) { + params = params.append('name.options', p.nameQuery.options); + } + } + + // string + if (p.addressQuery !== undefined) { + params = params.append('address', '1'); + params = params.append('address.operation', p.addressQuery.operation); + params = params.append('address.query', p.addressQuery.query); + if (p.addressQuery.options !== undefined) { + params = params.append('address.options', p.addressQuery.options); + } + } + + // string + if (p.locationQuery !== undefined) { + params = params.append('location', '1'); + params = params.append('location.operation', p.locationQuery.operation); + params = params.append('location.query', p.locationQuery.query); + if (p.locationQuery.options !== undefined) { + params = params.append('location.options', p.locationQuery.options); + } + } + + // number + if (p.ageQuery !== undefined) { + params = params.append('age', '1'); + params = params.append('age.operation', p.ageQuery.operation); + if (p.ageQuery.from !== undefined) { + params = params.append('age.from', p.ageQuery.from.toString()); + } + if (p.ageQuery.to !== undefined) { + params = params.append('age.to', p.ageQuery.to.toString()); + } + } + + // number + if (p.priceQuery !== undefined) { + params = params.append('price', '1'); + params = params.append('price.operation', p.priceQuery.operation); + if (p.priceQuery.from !== undefined) { + params = params.append('price.from', p.priceQuery.from.toString()); + } + if (p.priceQuery.to !== undefined) { + params = params.append('price.to', p.priceQuery.to.toString()); + } + } + + // bool + if (p.enabledQuery !== undefined) { + params = params.append('enabled', '1'); + if (p.enabledQuery.query) { + params = params.append('enabled.query', 'true'); + } else { + params = params.append('enabled.query', 'false'); + } + } + + return this.http.get(this.rootURL, {params: params}); + } + + public find(p: DataParams): Observable { + let params = new HttpParams(); + + // if + if (p.idQuery !== undefined) { + params = params.append('id', '1'); + params = params.append('id.query', p.idQuery.query); + } + + // string + if (p.nameQuery !== undefined) { + params = params.append('name', '1'); + params = params.append('name.operation', p.nameQuery.operation); + params = params.append('name.query', p.nameQuery.query); + if (p.nameQuery.options !== undefined) { + params = params.append('name.options', p.nameQuery.options); + } + } + + // string + if (p.addressQuery !== undefined) { + params = params.append('address', '1'); + params = params.append('address.operation', p.addressQuery.operation); + params = params.append('address.query', p.addressQuery.query); + if (p.addressQuery.options !== undefined) { + params = params.append('address.options', p.addressQuery.options); + } + } + + // string + if (p.locationQuery !== undefined) { + params = params.append('location', '1'); + params = params.append('location.operation', p.locationQuery.operation); + params = params.append('location.query', p.locationQuery.query); + if (p.locationQuery.options !== undefined) { + params = params.append('location.options', p.locationQuery.options); + } + } + + // number + if (p.ageQuery !== undefined) { + params = params.append('age', '1'); + params = params.append('age.operation', p.ageQuery.operation); + if (p.ageQuery.from !== undefined) { + params = params.append('age.from', p.ageQuery.from.toString()); + } + if (p.ageQuery.to !== undefined) { + params = params.append('age.to', p.ageQuery.to.toString()); + } + } + + // number + if (p.priceQuery !== undefined) { + params = params.append('price', '1'); + params = params.append('price.operation', p.priceQuery.operation); + if (p.priceQuery.from !== undefined) { + params = params.append('price.from', p.priceQuery.from.toString()); + } + if (p.priceQuery.to !== undefined) { + params = params.append('price.to', p.priceQuery.to.toString()); + } + } + + // bool + if (p.enabledQuery !== undefined) { + params = params.append('enabled', '1'); + if (p.enabledQuery.query) { + params = params.append('enabled.query', 'true'); + } else { + params = params.append('enabled.query', 'false'); + } + } + + return this.http.get(this.rootURL + '1', {params: params}); + } + + public create(m: Data) { + return this.http.post(this.rootURL, m); + } + + public updateId(id: string, m: Data) { + return this.http.put(this.rootURL + id, m); + } + + public removeId(id: string) { + return this.http.delete(this.rootURL + id); + } + +} + +export class DataParams { + start?: number; + limit?: number; + sort?: string; + + idQuery?: { query: string }; + nameQuery?: { operation: string, query: string, options?: string }; + addressQuery?: { operation: string, query: string, options?: string }; + locationQuery?: { operation: string, query: string, options?: string }; + ageQuery?: { operation: string, from?: number, to?: number }; + priceQuery?: { operation: string, from?: number, to?: number }; + enabledQuery?: { query: boolean }; +} + +export class Data { + id: string; + name: string; + address: string; + location: string; + age?: number; + price?: number; + enabled: boolean; +}