From c9e148320614920658bafe5d7a93d32f7f0ba3ce Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Mon, 31 Jul 2017 12:50:59 +0200 Subject: [PATCH] Migrate to HttpClient in Operation module. --- src/operations/category.service.ts | 12 ++++---- src/operations/operation.module.ts | 6 ++-- src/operations/operation.service.ts | 44 +++++++++++++++-------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/operations/category.service.ts b/src/operations/category.service.ts index 411d4b7..c7559df 100644 --- a/src/operations/category.service.ts +++ b/src/operations/category.service.ts @@ -5,14 +5,14 @@ import * as moment from 'moment'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Rx'; -import { Restangular } from "ngx-restangular"; +import { HttpClient, HttpParams } from "@angular/common/http"; import { Category } from './category'; @Injectable() export class CategoryService { constructor( - private restangular: Restangular + private http: HttpClient ) {} formatDate(date: Date|string) { @@ -24,16 +24,16 @@ export class CategoryService { } query(id: number, minDate: Date = null, maxDate: Date = null): Observable { - var dateRange: any = {}; + let params: HttpParams = new HttpParams(); if(minDate) { - dateRange.begin = this.formatDate(minDate); + params = params.set('begin', this.formatDate(minDate)); } if(maxDate) { - dateRange.end = this.formatDate(maxDate); + params = params.set('end', this.formatDate(maxDate)); } - return this.restangular.one('account', id).getList('category', dateRange); + return this.http.get(`/api/account/${id}/category`, { params: params}); } } diff --git a/src/operations/operation.module.ts b/src/operations/operation.module.ts index 2b252ce..fd87ff7 100644 --- a/src/operations/operation.module.ts +++ b/src/operations/operation.module.ts @@ -3,9 +3,8 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; -import { HttpModule } from '@angular/http'; +import { HttpClientModule } from '@angular/common/http'; -import { RestangularModule } from 'ngx-restangular'; import { NgLoggerModule, Level } from '@nsalaun/ng-logger'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { ToastrModule } from 'ngx-toastr'; @@ -31,11 +30,10 @@ export function accountIdServiceFactory(i: any) { @NgModule({ imports: [ - HttpModule, + HttpClientModule, CommonModule, FormsModule, NgLoggerModule, - RestangularModule, ToastrModule, NgbModule, TextMaskModule diff --git a/src/operations/operation.service.ts b/src/operations/operation.service.ts index 5ff0931..0286040 100644 --- a/src/operations/operation.service.ts +++ b/src/operations/operation.service.ts @@ -3,26 +3,18 @@ import * as moment from 'moment'; import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs/Rx'; +import { HttpClient, HttpParams } from '@angular/common/http'; -import { Restangular } from "ngx-restangular"; +import { Observable } from 'rxjs/Rx'; import { Operation } from './operation'; @Injectable() export class OperationService { constructor( - private restangular: Restangular + private http: HttpClient, ) {} - private all() { - return this.restangular.all('operation'); - } - - private one(id: number) { - return this.restangular.one('operation', id); - } - formatDate(date: Date|string) { if(date instanceof Date) { return moment(date).format('YYYY-MM-DD'); @@ -31,39 +23,49 @@ export class OperationService { return date; } + url(id?: Number): string { + if(id) { + return `/api/operation/${id}`; + } + + return `/api/operation`; + } + query( accountId: number, minDate: Date|string = null, maxDate: Date|string = null ): Observable { - var dateRange: any = { - account_id: accountId - }; + let params = new HttpParams(); + + params = params.set('account_id', `${accountId}`); if(minDate) { - dateRange.begin = this.formatDate(minDate); + params = params.set('begin', this.formatDate(minDate)); } if(maxDate) { - dateRange.end = this.formatDate(maxDate); + params = params.set('end', this.formatDate(maxDate)); } - return this.all().getList(dateRange); + return this.http.get(this.url(), { + params: params + }); } get(id: number): Observable { - return this.one(id).get(); + return this.http.get(this.url(id)); } create(operation: Operation): Observable { - return this.all().post(operation); + return this.http.post(this.url(), operation); } update(operation: Operation): Observable { - return this.one(operation.id).post(null, operation); + return this.http.post(this.url(operation.id), operation); } delete(operation: Operation): Observable { - return this.one(operation.id).remove(); + return this.http.delete(this.url(operation.id)); } }