123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /**
- * Created by robin on 12/11/16.
- */
-
- (function () {
- 'use strict';
- angular.module('luticate2Utils')
- .factory('luWebApiCrudDataAccess', ['luWebApiDataAccess', '$q', 'luUtilsDataAccess', 'luDataInitializer',
- function (luWebApiDataAccess, $q, luUtilsDataAccess, luDataInitializer) {
-
- var luWebApiCrudDataAccess = {};
-
- luWebApiCrudDataAccess.create = function (entry_point) {
- var DataAccess = luWebApiDataAccess.create(entry_point);
-
- DataAccess.OP_EDIT = 'OP_EDIT';
- DataAccess.OP_ADD = 'OP_ADD';
-
- DataAccess.TYPE_DBO = 'TYPE_DBO';
- DataAccess.TYPE_MODEL = 'TYPE_MODEL';
-
- DataAccess.defaultDbo = {
- id: null,
- createdAt: null,
- updatedAt: null,
- toString: function()
- {
- return this.id;
- }
- };
-
-
- DataAccess.modelInitializer = luDataInitializer.create(function(model, op)
- {
- return DataAccess.initModel(model, op);
- });
-
- DataAccess.dboInitializer = luDataInitializer.create(function(dbo, type)
- {
- return DataAccess.initDbo(dbo, type);
- });
-
-
- DataAccess._initModel = function(model, op)
- {
- if (model == null) {
- return null;
- }
- model = angular.copy(model);
- delete model.id;
- delete model.updatedAt;
- delete model.createdAt;
- delete model._itemType;
- delete model.toString;
- return model;
- };
-
- DataAccess.initModel = function(model, op)
- {
- return DataAccess._initModel(model, op);
- };
-
-
- DataAccess._initDbo = function(model, type)
- {
- if (model == null) {
- return null;
- }
- var dbo = DataAccess.extendDeep({}, angular.copy(DataAccess.defaultDbo), model);
- if (type == DataAccess.TYPE_MODEL) {
- dbo.createdAt = luUtilsDataAccess.stringToMomentDateTime(dbo.createdAt);
- dbo.updatedAt = luUtilsDataAccess.stringToMomentDateTime(dbo.updatedAt);
- }
- return dbo;
- };
-
- DataAccess.initDbo = function(model, type)
- {
- return DataAccess._initDbo(model, type);
- };
-
- DataAccess.extendDeep = function extendDeep(dst) {
- angular.forEach(arguments, function(obj) {
- if (obj !== dst) {
- angular.forEach(obj, function(value, key) {
- if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
- extendDeep(dst[key], value);
- } else {
- dst[key] = value;
- }
- });
- }
- });
- return dst;
- };
-
-
-
- DataAccess.getSingleById = function (id, luBusyGroups) {
- return DataAccess.dboInitializer.thenInitData(DataAccess.get(id, null, luBusyGroups), DataAccess.TYPE_MODEL);
- };
-
- DataAccess.getMultiple = function (orderBy, filter, page, perPage, luBusyGroups) {
- return DataAccess.dboInitializer.thenInitPaginatedData(DataAccess.get('', {
- orderBy: orderBy,
- filter: filter,
- page: page,
- perPage: perPage
- }, luBusyGroups), DataAccess.TYPE_MODEL);
- };
-
- DataAccess.addDbo = function(data, luBusyGroups) {
- var model = DataAccess.modelInitializer.initData(data, DataAccess.OP_ADD);
- return DataAccess.dboInitializer.thenInitData(DataAccess.post('', null, model, luBusyGroups), DataAccess.TYPE_MODEL);
- };
-
- DataAccess.editSingleByIdDbo = function(id, data, luBusyGroups) {
- var model = DataAccess.modelInitializer.initData(data, DataAccess.OP_EDIT);
- return DataAccess.dboInitializer.thenInitData(DataAccess.post(id, null, model, luBusyGroups), DataAccess.TYPE_MODEL);
- };
-
- DataAccess.deleteDbo = function(id, luBusyGroups) {
- return DataAccess.dboInitializer.thenInitData(DataAccess.delete(id, null, null, luBusyGroups), DataAccess.TYPE_MODEL);
- };
-
- return DataAccess;
- };
-
- return luWebApiCrudDataAccess;
-
- }]);
- })();
|