showing transactions, next step: CRUD

This commit is contained in:
2016-03-07 17:06:19 +01:00
parent fe18e91101
commit 56799eaefe
19 changed files with 3158 additions and 15 deletions
@@ -0,0 +1,81 @@
var transactionControllers = angular.module('transactionControllers', ['ngResource']);
transactionControllers.controller('TransactionListCtrl', [ '$scope', '$location', '$resource', function($scope, $location, $resource) {
// TODO pass accountId
$scope.accountId = 1;
$scope.initList = function() {
var transactions = $resource('/transactions/search/last10', { accountId: $scope.accountId }).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();
}
});
var categories = $resource('/categories/search/listForAccount', { accountId: $scope.accountId }).get();
categories.$promise.then(function(data) {
$scope.categories = categories._embedded.categories;
// select the first entry as default
$scope.categorySelection = categories._embedded.categories[0];
});
}
$scope.postTransaction = function() {
// // simple HTTP POST
// $http.post('/transactions', { "amount" : $scope.amount /* date, */ }).success(function() {
// console.log('REFRESH');
// $scope.initList();
// });
// via Resource (might be moved into a service)
var Transaction = $resource('/transactions');
var newTransaction = new Transaction({
"account" : $scope.account,
"amount" : $scope.amount,
"date" : $scope.date,
"description" : $scope.description,
"category" : $scope.category,
"creditor" : $scope.creditor
});
newTransaction.$save();
};
$scope.showTransaction = function(transaction) {
$location.path('/transaction/' + transaction._links.self.href);
};
} ]);
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('/');
});
}
} ]);