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

View File

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

View File

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