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 { 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<Category[]> {
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<Category[]>(`/api/account/${id}/category`, { params: params});
}
}

View File

@ -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

View File

@ -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<Operation[]> {
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<Operation[]>(this.url(), {
params: params
});
}
get(id: number): Observable<Operation> {
return this.one(id).get();
return this.http.get<Operation>(this.url(id));
}
create(operation: Operation): Observable<Operation> {
return this.all().post(operation);
return this.http.post<Operation>(this.url(), 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> {
return this.one(operation.id).remove();
return this.http.delete<Operation>(this.url(operation.id));
}
}