From c6a406272f9e336e080cf3c9c4d5dad7f4c0c3f8 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Fri, 14 Jul 2017 10:24:46 +0200 Subject: [PATCH] Use account service. --- src/accounts/account.controller.ts | 38 ++++++++++++++++---------- src/accounts/index.ts | 4 +-- src/operations/operation.controller.ts | 6 ++-- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/accounts/account.controller.ts b/src/accounts/account.controller.ts index aeb54cf..ad8fc46 100644 --- a/src/accounts/account.controller.ts +++ b/src/accounts/account.controller.ts @@ -1,12 +1,16 @@ +import { Observable } from 'rxjs/Rx'; + var accountFormTmpl = require('./account.form.tmpl.html'), accountDeleteTmpl = require('./account.delete.tmpl.html'); +import { Account } from './account'; + export class AccountController { - static $inject = ['Account', 'AccountBalances', 'Notification', '$log', '$modal']; + static $inject = ['AccountService', 'AccountBalances', 'Notification', '$log', '$modal']; accounts: Account[]; - constructor(private Account, private AccountBalances, private Notification, private $log, private $modal) { + constructor(private AccountService, private AccountBalances, private Notification, private $log, private $modal) { // Load accounts. this.load(); } @@ -46,11 +50,11 @@ export class AccountController { }; load() { - vm.accounts = Account.query({}, function(result) { - return result.map(function(item) { + this.AccountService.query().subscribe(accounts => { + this.accounts = accounts.map((item: Account) => { item.balances = this.AccountBalances.get({id: item.id}); return item; - }); + }) }); }; @@ -65,13 +69,19 @@ export class AccountController { * Save account. */ save(account) { - return account.$save().then(function(data) { - this.Notification.success('Account #' + data.id + ' saved.'); + var observable: Observable; + + if(account.id) { + observable = this.AccountService.update(account); + } else { + observable = this.AccountService.create(account); + } + + observable.subscribe(account => { + this.Notification.success('Account #' + account.id + ' saved.'); this.load(); - - return data; - }, function(result){ + }, result => { this.$log.error('Error while saving account', account, result); this.Notification.error( @@ -96,7 +106,7 @@ export class AccountController { locals: { title: title, account: account, - $delete: this.delete + $delete: (account: Account) => { this.delete(account); } } }); }; @@ -107,7 +117,7 @@ export class AccountController { delete(account) { var id = account.id; - return account.$delete().then(function() { + this.AccountService.delete(account).subscribe(account => { this.Notification.success('account #' + id + ' deleted.'); this.load(); @@ -134,7 +144,7 @@ export class AccountController { this.$modal({ templateUrl: accountFormTmpl, - controller: function($scope, title, account, $save) { + controller: function($log, $scope, title, account, $save) { $scope.title = title; $scope.account = account; $scope.account.authorized_overdraft *= -1; @@ -147,7 +157,7 @@ export class AccountController { locals: { title: title, account: account, - $save: this.save + $save: (account: Account) => { this.save(account); } } }); }; diff --git a/src/accounts/index.ts b/src/accounts/index.ts index 179fcc5..d14b902 100644 --- a/src/accounts/index.ts +++ b/src/accounts/index.ts @@ -27,7 +27,7 @@ var ngResource = require('angular-resource'), ngUiNotification = require('angular-ui-notification'), ngStrap = require('angular-strap'); -import { AccountFactory } from './account.factory'; +import { AccountService } from './account.service'; import { AccountBalancesFactory } from './accountBalances.factory'; import { AccountConfig } from './account.config'; import { AccountController } from './account.controller'; @@ -43,7 +43,7 @@ export default angular.module('accountant.accounts', [ .factory('AccountBalances', AccountBalancesFactory) - .factory('Account', AccountFactory) + .factory('AccountService', downgradeInjectable(AccountService)) .controller('AccountController', AccountController) diff --git a/src/operations/operation.controller.ts b/src/operations/operation.controller.ts index 1830434..b8b7546 100644 --- a/src/operations/operation.controller.ts +++ b/src/operations/operation.controller.ts @@ -2,7 +2,7 @@ var operationFormTmpl = require('./operation.form.tmpl.html'), operationDeleteTmpl = require('./operation.delete.tmpl.html'); module.exports = function($routeParams, $modal, Notification, Operation, - Account) { + AccountService) { var vm = this; @@ -145,5 +145,7 @@ module.exports = function($routeParams, $modal, Notification, Operation, vm.operations = vm.load(minDate, maxDate); }; - vm.account = Account.get({id: $routeParams.accountId}); + AccountService.get($routeParams.accountId).subscribe(account => { + vm.account = account + }); };