From 377736585abb7e35812dfb856aa1b98f74c9f6ed Mon Sep 17 00:00:00 2001 From: Tilman Liero Date: Fri, 8 Jul 2016 12:29:03 +0200 Subject: [PATCH] moved Transaction into service --- src/main/resources/static/index.html | 1 + src/main/resources/static/js/app.js | 2 +- src/main/resources/static/js/controllers.js | 79 +++------------------ src/main/resources/static/js/services.js | 32 +++++++++ 4 files changed, 44 insertions(+), 70 deletions(-) create mode 100644 src/main/resources/static/js/services.js diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index 3065d27..c49ae45 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -26,6 +26,7 @@ + diff --git a/src/main/resources/static/js/app.js b/src/main/resources/static/js/app.js index 3dde252..85987db 100644 --- a/src/main/resources/static/js/app.js +++ b/src/main/resources/static/js/app.js @@ -1,4 +1,4 @@ -var transactionsApp = angular.module('transactionsApp', ['ui.bootstrap', 'ngRoute', 'transactionControllers' ]); +var transactionsApp = angular.module('transactionsApp', ['ui.bootstrap', 'ngRoute', 'transactionControllers', 'transactionServices' ]); transactionsApp.config([ '$routeProvider', function($routeProvider) { $routeProvider.when('/list', { diff --git a/src/main/resources/static/js/controllers.js b/src/main/resources/static/js/controllers.js index c7e0cc3..100b028 100644 --- a/src/main/resources/static/js/controllers.js +++ b/src/main/resources/static/js/controllers.js @@ -1,6 +1,6 @@ var transactionControllers = angular.module('transactionControllers', ['ngResource']); -transactionControllers.controller('TransactionListCtrl', [ '$http', '$scope', '$location', '$resource', '$q', '$uibModal', '$log', function($http, $scope, $location, $resource, $q, $uibModal, $log) { +transactionControllers.controller('TransactionListCtrl', [ '$http', '$scope', '$location', '$resource', '$q', '$uibModal', '$log', 'Transaction', function($http, $scope, $location, $resource, $q, $uibModal, $log, Transaction) { $scope.refreshList = function() { @@ -136,13 +136,17 @@ transactionControllers.controller('TransactionListCtrl', [ '$http', '$scope', '$ } - $scope.postTransaction = function() { - - var amount = $scope.transaction.amount.replace(',', '.'); - + function amountToNumber(amount) { + amount = amount.replace(',', '.'); if (amount.charAt(0) !== '+' && amount.charAt(0) !== '-') { amount = '-' + amount; } + return amount; + } + + $scope.postTransaction = function() { + + amount = amountToNumber($scope.transaction.amount); // handle withdrawals if ($scope.transaction.category._links.self.href === 'withdrawal') { @@ -167,12 +171,6 @@ transactionControllers.controller('TransactionListCtrl', [ '$http', '$scope', '$ return; } - // via Resource (might be moved into a service) - var Transaction = $resource('/transactions'); - - console.log('Creditor:'); - console.log($scope.transaction.creditor); - var newTransaction = new Transaction({ "account" : $scope.account._links.self.href, "amount" : amount, @@ -267,69 +265,12 @@ transactionControllers.controller('DetailsModalCtrl', [ '$scope', '$uibModalInst $scope.cancel = function () { $uibModalInstance.dismiss('cancel'); }; -} ]); - - - -// TODO to be removed -transactionControllers.controller('TransactionDetailCtrl', [ '$scope', '$routeParams', '$resource', '$location', function($scope, $routeParams, $resource, $location) { - - $scope.initDetails = function() { - console.log('initDetails()'); - - var transaction = $resource($routeParams.transactionURI).get(); - transaction.$promise.then(function(data) { - transaction.date = new Date(transaction.date); - 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; - - // FIXME - // TODO The rest of this function is *copied code* from TransactionListCtrl and desperately needs to be moved into a service - - var userinfo = $resource('/user').get(); - userinfo.$promise.then(function(data) { - $scope.userid = userinfo.name; - - userinfo = $resource('/users/search/findByUsername', { username: $scope.userid }).get(); - userinfo.$promise.then(function(data) { - $scope.userinfo = userinfo; - }); - - var accountInfo = $resource('/accounts/search/findByUserName', { username: $scope.userid }).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.categories.push({id: -1, name: '────────────────', position: 9999, disabled: true}); - $scope.categories.push({id: -1, name: 'private Entnahme', position: 10000, _links: { self: {href: 'withdrawal'}}}); - - // FIXME Code from TransactionListCtrl.refreshList() - var accountUsers = $resource($scope.account._links.users.href).get(); - accountUsers.$promise.then(function(data) { - $scope.accountUsers = accountUsers._embedded.users; - }); - }); - }); - }); - - } + // TODO still used? $scope.deleteTransaction = function(transaction) { // TODO auf return code reagieren $resource(transaction._links.self.href).remove().$promise.then(function() { $location.path('/'); }); } - - $scope.initDetails(); - } ]); diff --git a/src/main/resources/static/js/services.js b/src/main/resources/static/js/services.js new file mode 100644 index 0000000..dcb98ae --- /dev/null +++ b/src/main/resources/static/js/services.js @@ -0,0 +1,32 @@ +var transactionServices = angular.module('transactionServices', [ 'ngResource' ]); + +transactionServices.factory('Transaction', [ '$resource', function($resource) { + return $resource('/transactions/:id', {}, { + /*query : { + method : 'GET', + params : { + phoneId : 'phones' + }, + isArray : true + }*/ + }); +} ]); + +/* + // via Resource (might be moved into a service) + var Transaction = $resource('/transactions'); + + var newTransaction = new Transaction({ + "account" : $scope.account._links.self.href, + "amount" : amount, + "date" : $scope.transaction.date, + "description" : $scope.transaction.description, + "category" : $scope.transaction.category._links.self.href, + "creditor" : $scope.transaction.creditor + }); + + newTransaction.$save(function(transaction) { + $scope.refreshList(); + $scope.lastSubmission = newTransaction; + }); +*/ \ No newline at end of file