Migrate to HttpClient in Operation module.

This commit is contained in:
Alexis Lahouze 2017-07-31 12:50:59 +02:00
parent 849a7ae95c
commit c9e1483206
3 changed files with 31 additions and 31 deletions

View File

@ -5,14 +5,14 @@ import * as moment from 'moment';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { Restangular } from "ngx-restangular"; import { HttpClient, HttpParams } from "@angular/common/http";
import { Category } from './category'; import { Category } from './category';
@Injectable() @Injectable()
export class CategoryService { export class CategoryService {
constructor( constructor(
private restangular: Restangular private http: HttpClient
) {} ) {}
formatDate(date: Date|string) { formatDate(date: Date|string) {
@ -24,16 +24,16 @@ export class CategoryService {
} }
query(id: number, minDate: Date = null, maxDate: Date = null): Observable<Category[]> { query(id: number, minDate: Date = null, maxDate: Date = null): Observable<Category[]> {
var dateRange: any = {}; let params: HttpParams = new HttpParams();
if(minDate) { if(minDate) {
dateRange.begin = this.formatDate(minDate); params = params.set('begin', this.formatDate(minDate));
} }
if(maxDate) { 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<Category[]>(`/api/account/${id}/category`, { params: params});
} }
} }

View File

@ -3,9 +3,8 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms'; 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 { NgLoggerModule, Level } from '@nsalaun/ng-logger';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr'; import { ToastrModule } from 'ngx-toastr';
@ -31,11 +30,10 @@ export function accountIdServiceFactory(i: any) {
@NgModule({ @NgModule({
imports: [ imports: [
HttpModule, HttpClientModule,
CommonModule, CommonModule,
FormsModule, FormsModule,
NgLoggerModule, NgLoggerModule,
RestangularModule,
ToastrModule, ToastrModule,
NgbModule, NgbModule,
TextMaskModule TextMaskModule

View File

@ -3,26 +3,18 @@
import * as moment from 'moment'; import * as moment from 'moment';
import { Injectable } from '@angular/core'; 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'; import { Operation } from './operation';
@Injectable() @Injectable()
export class OperationService { export class OperationService {
constructor( 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) { formatDate(date: Date|string) {
if(date instanceof Date) { if(date instanceof Date) {
return moment(date).format('YYYY-MM-DD'); return moment(date).format('YYYY-MM-DD');
@ -31,39 +23,49 @@ export class OperationService {
return date; return date;
} }
url(id?: Number): string {
if(id) {
return `/api/operation/${id}`;
}
return `/api/operation`;
}
query( query(
accountId: number, accountId: number,
minDate: Date|string = null, minDate: Date|string = null,
maxDate: Date|string = null maxDate: Date|string = null
): Observable<Operation[]> { ): Observable<Operation[]> {
var dateRange: any = { let params = new HttpParams();
account_id: accountId
}; params = params.set('account_id', `${accountId}`);
if(minDate) { if(minDate) {
dateRange.begin = this.formatDate(minDate); params = params.set('begin', this.formatDate(minDate));
} }
if(maxDate) { if(maxDate) {
dateRange.end = this.formatDate(maxDate); params = params.set('end', this.formatDate(maxDate));
} }
return this.all().getList(dateRange); return this.http.get<Operation[]>(this.url(), {
params: params
});
} }
get(id: number): Observable<Operation> { get(id: number): Observable<Operation> {
return this.one(id).get(); return this.http.get<Operation>(this.url(id));
} }
create(operation: Operation): Observable<Operation> { create(operation: Operation): Observable<Operation> {
return this.all().post(operation); return this.http.post<Operation>(this.url(), operation);
} }
update(operation: Operation): Observable<Operation> { update(operation: Operation): Observable<Operation> {
return this.one(operation.id).post(null, operation); return this.http.post<Operation>(this.url(operation.id), operation);
} }
delete(operation: Operation): Observable<Operation> { delete(operation: Operation): Observable<Operation> {
return this.one(operation.id).remove(); return this.http.delete<Operation>(this.url(operation.id));
} }
} }