Use Operation Service in Operation controller.

This commit is contained in:
Alexis Lahouze 2017-07-29 22:26:38 +02:00
parent 4c921bfaaa
commit 8348f5bb8f
3 changed files with 40 additions and 23 deletions

View File

@ -34,8 +34,8 @@ import accountModule from '@accountant/accounts';
import { BalanceChartComponent } from './balanceChart.component';
import { CategoryChartComponent } from './categoryChart.component';
import { OperationService } from './operation.service';
var OperationFactory = require('./operation.factory');
var OperationController = require('./operation.controller');
export default angular.module('accountant.operations', [
@ -46,7 +46,7 @@ export default angular.module('accountant.operations', [
.factory('toastrService', downgradeInjectable(ToastrService))
.factory('Operation', OperationFactory)
.factory('operationService', downgradeInjectable(OperationService))
.controller('OperationController', OperationController)

View File

@ -1,21 +1,27 @@
import * as moment from 'moment';
var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html');
module.exports = function($stateParams, $modal, toastrService, Operation,
AccountService) {
import { Observable } from 'rxjs/Rx';
import { ToastrService } from 'ngx-toastr';
import { Operation } from './operation';
import { OperationService } from './operation.service';
module.exports = function(
$stateParams, $modal,
toastrService: ToastrService,
operationService: OperationService,
AccountService
){
var vm = this;
/*
* Add an empty operation.
*/
vm.add = function() {
var operation = new Operation({
// eslint-disable-next-line camelcase
account_id: $stateParams.accountId
});
var operation = new Operation();
operation.account_id = $stateParams.accountId;
return vm.modify(operation);
};
@ -27,11 +33,12 @@ module.exports = function($stateParams, $modal, toastrService, Operation,
vm.minDate = minDate;
vm.maxDate = maxDate;
return Operation.query({
// eslint-disable-next-line camelcase
account_id: $stateParams.accountId,
begin: minDate ? moment(minDate).format('YYYY-MM-DD') : null,
end: maxDate ? moment(maxDate).format('YYYY-MM-DD') : null
return operationService.query(
$stateParams.accountId,
minDate,
maxDate
).subscribe((operations: Operation[]) => {
vm.operations = operations;
});
};
@ -59,13 +66,21 @@ module.exports = function($stateParams, $modal, toastrService, Operation,
vm.save = function(operation) {
operation.confirmed = true;
return operation.$save().then(function(operation) {
var observable: Observable<Operation>;
if(operation.id){
observable = operationService.update(operation);
} else {
observable = operationService.create(operation);
}
return observable.subscribe((operation) => {
toastrService.success('Operation #' + operation.id + ' saved.');
vm.operations = vm.load();
vm.load();
return operation;
}, function(result){
}, (result) => {
toastrService.error(
'Error while saving operation: ' + result.message
);
@ -99,13 +114,13 @@ module.exports = function($stateParams, $modal, toastrService, Operation,
vm.delete = function(operation) {
var id = operation.id;
return operation.$delete().then(function() {
return operationService.delete(operation).subscribe(() => {
toastrService.success('Operation #' + id + ' deleted.');
vm.operations = vm.load();
vm.load();
return operation;
}, function(result) {
}, (result) => {
toastrService.error(
'An error occurred while trying to delete operation #' +
id + ':<br />' + result
@ -144,7 +159,7 @@ module.exports = function($stateParams, $modal, toastrService, Operation,
};
vm.onUpdate = function(dateRange) {
vm.operations = vm.load(dateRange.minDate, dateRange.maxDate);
vm.load(dateRange.minDate, dateRange.maxDate);
};
AccountService.get($stateParams.accountId).subscribe(account => {

View File

@ -36,7 +36,9 @@ export class OperationService {
minDate: Date|string = null,
maxDate: Date|string = null
): Observable<Operation[]> {
var dateRange: any = {};
var dateRange: any = {
account_id: accountId
};
if(minDate) {
dateRange.begin = this.formatDate(minDate);