account.service.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function() {
  2. 'use strict';
  3. angular
  4. .module('cloudbudget')
  5. .factory('AccountService', AccountService);
  6. AccountService.$inject =['$http', 'apiRoutes'];
  7. function AccountService($http, apiRoute) {
  8. var service = {};
  9. service.details = details;
  10. service.list = list;
  11. service.create = create;
  12. service.drop = drop;
  13. service.edit = edit;
  14. return service;
  15. function details(account_id) {
  16. return $http.get( apiRoute.accounts + account_id)
  17. .then(function handleSuccess(response) {
  18. return {success: true, account: response.data};
  19. }, handleError('Error during accounts listing'));
  20. }
  21. function list(account_id) {
  22. return $http.get( apiRoute.accounts + account_id + '/entries')
  23. .then(handleSuccess, handleError('Error listing account entries'));
  24. }
  25. function create(account, entry) {
  26. return $http.post( apiRoute.accounts + account._id + '/entries', entry)
  27. .then(handleSuccess, handleError('Error creating entry'));
  28. }
  29. function drop(account, entry) {
  30. return $http.delete(apiRoute.accounts + account._id + '/entries/' + entry._id)
  31. .then(handleSuccess, handleError('Error deleting entry'));
  32. }
  33. function edit(account, id, entry) {
  34. return $http.put(apiRoute.accounts + account._id + '/entries/' + id, entry)
  35. .then(handleSuccess, handleError('Error updating entry'));
  36. }
  37. function handleSuccess(response) {
  38. return {success: true, data: response.data};
  39. }
  40. function handleError(error) {
  41. return function() {
  42. return {success: false, message: error};
  43. };
  44. }
  45. }
  46. })();