Migrate to HttpClient in Operation module.
This commit is contained in:
@ -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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user