Use account service.

This commit is contained in:
Alexis Lahouze 2017-07-14 10:24:46 +02:00
parent 79ecb1630b
commit c6a406272f
3 changed files with 30 additions and 18 deletions

View File

@ -1,12 +1,16 @@
import { Observable } from 'rxjs/Rx';
var accountFormTmpl = require('./account.form.tmpl.html'), var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html'); accountDeleteTmpl = require('./account.delete.tmpl.html');
import { Account } from './account';
export class AccountController { export class AccountController {
static $inject = ['Account', 'AccountBalances', 'Notification', '$log', '$modal']; static $inject = ['AccountService', 'AccountBalances', 'Notification', '$log', '$modal'];
accounts: Account[]; 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. // Load accounts.
this.load(); this.load();
} }
@ -46,11 +50,11 @@ export class AccountController {
}; };
load() { load() {
vm.accounts = Account.query({}, function(result) { this.AccountService.query().subscribe(accounts => {
return result.map(function(item) { this.accounts = accounts.map((item: Account) => {
item.balances = this.AccountBalances.get({id: item.id}); item.balances = this.AccountBalances.get({id: item.id});
return item; return item;
}); })
}); });
}; };
@ -65,13 +69,19 @@ export class AccountController {
* Save account. * Save account.
*/ */
save(account) { save(account) {
return account.$save().then(function(data) { var observable: Observable<Account>;
this.Notification.success('Account #' + data.id + ' saved.');
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(); this.load();
}, result => {
return data;
}, function(result){
this.$log.error('Error while saving account', account, result); this.$log.error('Error while saving account', account, result);
this.Notification.error( this.Notification.error(
@ -96,7 +106,7 @@ export class AccountController {
locals: { locals: {
title: title, title: title,
account: account, account: account,
$delete: this.delete $delete: (account: Account) => { this.delete(account); }
} }
}); });
}; };
@ -107,7 +117,7 @@ export class AccountController {
delete(account) { delete(account) {
var id = account.id; var id = account.id;
return account.$delete().then(function() { this.AccountService.delete(account).subscribe(account => {
this.Notification.success('account #' + id + ' deleted.'); this.Notification.success('account #' + id + ' deleted.');
this.load(); this.load();
@ -134,7 +144,7 @@ export class AccountController {
this.$modal({ this.$modal({
templateUrl: accountFormTmpl, templateUrl: accountFormTmpl,
controller: function($scope, title, account, $save) { controller: function($log, $scope, title, account, $save) {
$scope.title = title; $scope.title = title;
$scope.account = account; $scope.account = account;
$scope.account.authorized_overdraft *= -1; $scope.account.authorized_overdraft *= -1;
@ -147,7 +157,7 @@ export class AccountController {
locals: { locals: {
title: title, title: title,
account: account, account: account,
$save: this.save $save: (account: Account) => { this.save(account); }
} }
}); });
}; };

View File

@ -27,7 +27,7 @@ var ngResource = require('angular-resource'),
ngUiNotification = require('angular-ui-notification'), ngUiNotification = require('angular-ui-notification'),
ngStrap = require('angular-strap'); ngStrap = require('angular-strap');
import { AccountFactory } from './account.factory'; import { AccountService } from './account.service';
import { AccountBalancesFactory } from './accountBalances.factory'; import { AccountBalancesFactory } from './accountBalances.factory';
import { AccountConfig } from './account.config'; import { AccountConfig } from './account.config';
import { AccountController } from './account.controller'; import { AccountController } from './account.controller';
@ -43,7 +43,7 @@ export default angular.module('accountant.accounts', [
.factory('AccountBalances', AccountBalancesFactory) .factory('AccountBalances', AccountBalancesFactory)
.factory('Account', AccountFactory) .factory('AccountService', downgradeInjectable(AccountService))
.controller('AccountController', AccountController) .controller('AccountController', AccountController)

View File

@ -2,7 +2,7 @@ var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html'); operationDeleteTmpl = require('./operation.delete.tmpl.html');
module.exports = function($routeParams, $modal, Notification, Operation, module.exports = function($routeParams, $modal, Notification, Operation,
Account) { AccountService) {
var vm = this; var vm = this;
@ -145,5 +145,7 @@ module.exports = function($routeParams, $modal, Notification, Operation,
vm.operations = vm.load(minDate, maxDate); vm.operations = vm.load(minDate, maxDate);
}; };
vm.account = Account.get({id: $routeParams.accountId}); AccountService.get($routeParams.accountId).subscribe(account => {
vm.account = account
});
}; };