UNPKG

zettapi_client

Version:

Admin panel and client-side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project.

179 lines (142 loc) 6.32 kB
app.service('$entity', function ($http, $q, mySocket, $rootScope, $routeParams, $interval, $util, $interpolate, $location, blockUI, zapi) { var listeners = {}; this.get = get; this.getByKey = function (entity, keyValuePair, callback, transform) { var entityObj = zapi.entityMap[entity]; var db = typeof entityObj.db === "function" ? entityObj.db($routeParams) : (entity.db || "common"); var url = zapi.serverUrl + '/api/' + entity + '/fetch/' + db; var httpOptions = { params: keyValuePair }; if (typeof transform === 'function') httpOptions.transformResponse = transform; if (typeof callback !== 'function') return $http.get(url, httpOptions); $http.get(url, httpOptions).then(function (response) { callback(null, response); }).catch(function (response) { callback(response.data); }); }; this.add = function (entity, item, scope) { return $q(function (resolve, reject) { var entityObj = zapi.entityMap[entity]; var beforeSave = typeof entityObj.beforeSave === 'function' ? entityObj.beforeSave : $util.skip; beforeSave(item, function (err) { if (err) { swal("Antes de gravar...", err, "info"); return reject(err); } var db = typeof entityObj.db === "function" ? entityObj.db($routeParams) : (entity.db || "common"); var id = entityObj.id || "_id"; var url = zapi.serverUrl + '/api/' + entity + '/add/' + db + (item[id] ? '/' + item[id] : ''); blockUI.start($interpolate("{{'api.entity.sendData' | translate}}")(scope)); var fd = new FormData(); $util.setFormFiles(fd, item); fd.append('item', angular.toJson(item)); $http.post(url, fd, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }).then(function (response) { if (item[id]) swal({ title: "Alterado", text: $interpolate("{{'" + entityObj.title + "'|translate}}")(scope) + " - " + $interpolate("{{'api.entity.newRecordSuccess'|translate}}")(scope), type: "success" }); else swal({ title: "Criado", text: $interpolate("{{'" + entityObj.title + "'|translate}}")(scope) + " - " + $interpolate("{{'api.entity.editRecordSuccess' | translate}}")(scope), type: "success" }); var afterSave = typeof entityObj.afterSave === 'function' ? entityObj.afterSave : $util.skip; afterSave($location, $rootScope); resolve(response); }).catch(function (response) { swal("Erro ao Guardar", response.data, "error"); reject(response); }).finally(function () { blockUI.stop(); }); }); }); }; this.update = function (entity, id, fieldsToSet) { var entityObj = zapi.entityMap[entity]; var db = typeof entityObj.db === "function" ? entityObj.db($routeParams) : (entity.db || "common"); var url = zapi.serverUrl + "/api/" + entity + "/update/" + db + '/' + id; return $http.post(url, fieldsToSet); }; this.remove = function (entity, item, scope) { var deferred = $q.defer(); var entityObj = zapi.entityMap[entity]; blockUI.start($interpolate("{{'api.entity.checkRemove' | translate}}")(scope)); var db = typeof entityObj.db === "function" ? entityObj.db($routeParams) : (entity.db || "common"); var id = entityObj.id || "_id"; var url = zapi.serverUrl + '/api/' + entity + '/remove/' + db + '/' + item[id]; $http.get(url).then(function (response) { swal({ title: "Removido", text: $interpolate("{{'" + entityObj.title + "'|translate}}")(scope) + " - " + $interpolate("{{'api.entity.deleteRecordSuccess' | translate}}")(scope), type: "success" }); deferred.resolve(response); }).catch(function (response) { swal("Erro ao Remover", response.error ? response.error : response.data, "warning"); deferred.reject(response); }).finally(function () { blockUI.stop(); }); return deferred.promise; }; this.getMany = function (entityKeys, callback) { var exec = function (entityKeys, callback) { //defaults entityKeys = entityKeys || []; //validate if (!(entityKeys instanceof Array)) return callback(undefined, {}); if (entityKeys.length === 0) return callback(); var invalid = entityKeys.some(function (entityKey) { return typeof entityKey !== 'string'; }); if (invalid) return callback(undefined, {}); //assemble promises var promises = entityKeys.map(function (entityKey) { /* //todo var inCache = $lookup.get(entityKey); //http.get if empty if (inCache instanceof Array && inCache.length === 0) return get(entityKey); //http.get if not in cache if (inCache) return $q.resolve({ data: inCache }); else */ if (!listeners[entityKey]) { var entityObj = zapi.entityMap[entityKey]; listeners[entityKey] = true; var websocket; if (typeof entityObj.websocket !== 'function') websocket = entityKey; else websocket = entityObj.websocket($rootScope, $routeParams) + "." + entityKey; mySocket.forward(websocket + '.remove'); mySocket.forward(websocket + '.new'); mySocket.forward(websocket + '.edit'); } return get(entityKey); }); //resolve promises $q.all(promises).then(function (results) { //todo return from cache var lookups = {}; results.forEach(function (result, i) { lookups[entityKeys[i]] = result.data; }); callback(null, lookups); }, function (response) { callback(response.data); }); }; var intervalId = $interval(function () { if (typeof $rootScope.login === 'undefined') return; $interval.cancel(intervalId); exec(entityKeys, callback); }); }; function get(entity, id) { var entityObj = zapi.entityMap[entity]; var db = typeof entityObj.db === "function" ? entityObj.db($routeParams) : (entity.db || "common"); var url = zapi.serverUrl + '/api/' + entity + '/list/' + db + (id ? '/' + id : ''); return $http.get(url); } });