2017-07-29 17:51:44 +02:00
|
|
|
// vim: set tw=80 ts=2 sw=2 sts=2 :
|
|
|
|
|
2017-07-29 17:57:50 +02:00
|
|
|
import * as moment from 'moment';
|
|
|
|
|
2017-07-29 17:51:44 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Observable } from 'rxjs/Rx';
|
|
|
|
|
|
|
|
import { Restangular } from "ngx-restangular";
|
|
|
|
|
|
|
|
import { Operation } from './operation';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class OperationService {
|
|
|
|
constructor(
|
|
|
|
private restangular: Restangular
|
|
|
|
) {}
|
|
|
|
|
|
|
|
private all() {
|
|
|
|
return this.restangular.all('operation');
|
|
|
|
}
|
|
|
|
|
|
|
|
private one(id: number) {
|
|
|
|
return this.restangular.one('operation', id);
|
|
|
|
}
|
|
|
|
|
2017-07-29 17:57:50 +02:00
|
|
|
formatDate(date: Date|string) {
|
|
|
|
if(date instanceof Date) {
|
|
|
|
return moment(date).format('YYYY-MM-DD');
|
|
|
|
}
|
|
|
|
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
query(
|
|
|
|
accountId: number,
|
|
|
|
minDate: Date|string = null,
|
|
|
|
maxDate: Date|string = null
|
|
|
|
): Observable<Operation[]> {
|
2017-07-29 22:26:38 +02:00
|
|
|
var dateRange: any = {
|
|
|
|
account_id: accountId
|
|
|
|
};
|
2017-07-29 17:57:50 +02:00
|
|
|
|
|
|
|
if(minDate) {
|
|
|
|
dateRange.begin = this.formatDate(minDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(maxDate) {
|
|
|
|
dateRange.end = this.formatDate(maxDate);
|
|
|
|
}
|
|
|
|
|
2017-07-29 22:26:57 +02:00
|
|
|
return this.all().getList(dateRange);
|
2017-07-29 17:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get(id: number): Observable<Operation> {
|
|
|
|
return this.one(id).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
create(operation: Operation): Observable<Operation> {
|
|
|
|
return this.all().post(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(operation: Operation): Observable<Operation> {
|
|
|
|
return this.one(operation.id).post(null, operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(operation: Operation): Observable<Operation> {
|
|
|
|
return this.one(operation.id).delete();
|
|
|
|
}
|
|
|
|
}
|