You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lu-webapi-crud-business.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Created by robin on 12/11/16.
  3. */
  4. (function () {
  5. 'use strict';
  6. angular.module('luticate2Utils')
  7. .factory('luWebApiCrudBusiness', ['luBusiness', '$q', function (luBusiness, $q) {
  8. var luWebApiCrudBusiness = {};
  9. luWebApiCrudBusiness.create = function (dataAccess) {
  10. var Business = luBusiness.create();
  11. Business.dataAccess = dataAccess;
  12. Business.defaultDbo = {
  13. id: null,
  14. toString: function()
  15. {
  16. return this.id;
  17. }
  18. };
  19. Business.extendDeep = function extendDeep(dst) {
  20. angular.forEach(arguments, function(obj) {
  21. if (obj !== dst) {
  22. angular.forEach(obj, function(value, key) {
  23. if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
  24. extendDeep(dst[key], value);
  25. } else {
  26. dst[key] = value;
  27. }
  28. });
  29. }
  30. });
  31. return dst;
  32. };
  33. Business._initDbo = function(dbo)
  34. {
  35. if (dbo == null) {
  36. return null;
  37. }
  38. dbo = Business.extendDeep({}, Business.defaultDbo, dbo);
  39. if (dbo.createdAt != null) {
  40. dbo.createdAt = new Date(dbo.createdAt);
  41. }
  42. if (dbo.updatedAt != null) {
  43. dbo.updatedAt = new Date(dbo.updatedAt);
  44. }
  45. return dbo;
  46. };
  47. Business.initDbo = function(dbo)
  48. {
  49. return Business._initDbo(dbo);
  50. };
  51. Business.initListDbo = function(list)
  52. {
  53. var d = [];
  54. for (var i = 0; i < list.length; ++i) {
  55. d.push(Business.initDbo(list[i]));
  56. }
  57. return d;
  58. };
  59. Business.initPaginatedDbo = function(data)
  60. {
  61. var d = {
  62. count: data.count,
  63. data: Business.initListDbo(data.data)
  64. };
  65. return d;
  66. };
  67. Business.thenInitDbo = function(promise)
  68. {
  69. var deferred = $q.defer();
  70. promise.then(function(data)
  71. {
  72. deferred.resolve(Business.initDbo(data));
  73. }, deferred.reject);
  74. return deferred.promise;
  75. };
  76. Business.thenInitPaginatedDbo = function(promise)
  77. {
  78. var deferred = $q.defer();
  79. promise.then(function(data)
  80. {
  81. deferred.resolve(Business.initPaginatedDbo(data));
  82. }, deferred.reject);
  83. return deferred.promise;
  84. };
  85. Business.getSingleById = function (id, luBusyGroups) {
  86. return Business.thenInitDbo(Business.dataAccess.getSingleById(id, luBusyGroups));
  87. };
  88. Business.getMultiple = function (orderBy, filter, page, perPage, luBusyGroups) {
  89. return Business.thenInitPaginatedDbo(Business.dataAccess.getMultiple(orderBy, filter, page, perPage, luBusyGroups));
  90. };
  91. Business.addDbo = function(data, luBusyGroups) {
  92. return Business.thenInitDbo(Business.dataAccess.addDbo(data, luBusyGroups));
  93. };
  94. Business.editSingleByIdDbo = function(id, data, luBusyGroups) {
  95. return Business.thenInitDbo(Business.dataAccess.editSingleByIdDbo(id, data, luBusyGroups));
  96. };
  97. Business.deleteDbo = function(id, luBusyGroups) {
  98. return Business.thenInitDbo(Business.dataAccess.deleteDbo(id, luBusyGroups));
  99. };
  100. return Business;
  101. };
  102. return luWebApiCrudBusiness;
  103. }]);
  104. })();