Files
next-transactions/src/main/resources/static/js/controllers.js
T

116 lines
3.5 KiB
JavaScript

var transactionControllers = angular.module('transactionControllers', ['ngResource']);
transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location', '$resource', function($scope, $location, $resource) {
$scope.refreshList = function() {
console.log('refreshList()');
// reset form
if ($scope.form) {
$scope.form.$setPristine();
$scope.form.$setUntouched();
}
$scope.transaction = {
amount: undefined,
date: new Date()
};
if (typeof $scope.categories !== 'undefined') {
// TODO default-Kategorie sollte per Account einstellbar sein
console.log('DID IT!');
$scope.transaction.category = $scope.categories[0];
}
// get recent transactions
var transactions = $resource('/transactions/search/last10', { accountId: $scope.account.id }).get();
transactions.$promise.then(function(data) {
$scope.transactions = transactions._embedded.transactions;
for (var i = 0; i < $scope.transactions.length; i++) {
$scope.transactions[i].category = $resource($scope.transactions[i]._links.category.href).get();
$scope.transactions[i].creditor = $resource($scope.transactions[i]._links.creditor.href).get();
}
});
}
$scope.initList = function() {
console.log('initList()');
var userinfo = $resource('/user').get();
userinfo.$promise.then(function(data) {
$scope.username = userinfo.name;
var accountInfo = $resource('/accounts/search/findByUserName', { username: userinfo.name }).get();
accountInfo.$promise.then(function(data) {
$scope.accounts = accountInfo._embedded.accounts;
$scope.account = $scope.accounts[0];
var categories = $resource('/categories/search/listForAccount', { accountId: $scope.accounts[0].id }).get();
categories.$promise.then(function(data) {
$scope.categories = categories._embedded.categories;
$scope.refreshList();
});
});
});
}
$scope.refreshAccount = function() {
console.log('BINGO!');
$scope.refreshList();
};
$scope.postTransaction = function() {
// via Resource (might be moved into a service)
var Transaction = $resource('/transactions');
var newTransaction = new Transaction({
"account" : $scope.account,
"amount" : $scope.transaction.amount,
"date" : $scope.transaction.date,
"description" : $scope.transaction.description,
"category" : $scope.transaction.category._links.self.href,
"creditor" : $scope.creditor
});
newTransaction.$save(function(transaction) {
$scope.refreshList();
});
};
$scope.showTransaction = function(transaction) {
$location.path('/transaction/' + transaction._links.self.href);
};
// initialize (http://stackoverflow.com/a/25662066/3761783)
$scope.initList();
} ]);
transactionControllers.controller('TransactionDetailCtrl', [ '$scope', '$routeParams', '$resource', '$location', function($scope, $routeParams, $resource, $location) {
var transaction = $resource($routeParams.transactionURI).get();
transaction.$promise.then(function(data) {
transaction.category = $resource(transaction._links.category.href).get();
transaction.creditor = $resource(transaction._links.creditor.href).get();
transaction.account = $resource(transaction._links.account.href).get();
});
$scope.transaction = transaction;
$scope.deleteTransaction = function(transaction) {
/*console.log(angular.toJson(transaction, false));*/
// TODO auf return code reagieren
$resource(transaction._links.self.href).remove().$promise.then(function() {
$location.path('/');
});
}
} ]);