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'),
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<Account>;
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); }
}
});
};

View File

@ -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)

View File

@ -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
});
};