From 8348f5bb8fa02f21f23f71f622a0a753cba8d591 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sat, 29 Jul 2017 22:26:38 +0200 Subject: [PATCH] Use Operation Service in Operation controller. --- src/operations/index.ts | 4 +- src/operations/operation.controller.ts | 55 ++++++++++++++++---------- src/operations/operation.service.ts | 4 +- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/operations/index.ts b/src/operations/index.ts index ea006b6..f370cfd 100644 --- a/src/operations/index.ts +++ b/src/operations/index.ts @@ -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) diff --git a/src/operations/operation.controller.ts b/src/operations/operation.controller.ts index d8bf2d3..4b4bce1 100644 --- a/src/operations/operation.controller.ts +++ b/src/operations/operation.controller.ts @@ -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; + + 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 + ':
' + 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 => { diff --git a/src/operations/operation.service.ts b/src/operations/operation.service.ts index d83b67b..dd6d414 100644 --- a/src/operations/operation.service.ts +++ b/src/operations/operation.service.ts @@ -36,7 +36,9 @@ export class OperationService { minDate: Date|string = null, maxDate: Date|string = null ): Observable { - var dateRange: any = {}; + var dateRange: any = { + account_id: accountId + }; if(minDate) { dateRange.begin = this.formatDate(minDate);