UNPKG

zettapi_client

Version:

Client side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project

130 lines (100 loc) 4.72 kB
app.service('$entity', function ($http, $q, $translate, $routeParams, $util, $lookup, blockUI, zapi) { 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) { var deferred = $q.defer(); var entityObj = zapi.entityMap[entity]; var next = typeof entityObj.beforeSave === 'function' ? entityObj.beforeSave : $util.skip; next(item, function (err) { if (err) { swal("Antes de gravar...", err, "info"); return deferred.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($translate.instant('api.entity.sendData')); 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("Alterado", $translate.instant(entityObj.title) + $translate.instant('api.entity.newRecordSuccess'), "success"); else swal("Criado", $translate.instant(entityObj.title) + $translate.instant('api.entity.editRecordSuccess'), "success"); deferred.resolve(response); }).catch(function (response) { swal("Erro ao Guardar", response.data, "error"); deferred.reject(response); }).finally(function () { blockUI.stop(); }); }); return deferred.promise; }; 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) { var deferred = $q.defer(); var entityObj = zapi.entityMap[entity]; blockUI.start($translate.instant('api.entity.checkRemove')); 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("Removido", $translate.instant(entityObj.title) + $translate.instant('api.entity.deleteRecordSuccess'), "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) { //defaults entityKeys = entityKeys || []; //validate if (!(entityKeys instanceof Array)) return callback("$entity.getMany: unexpected entityKeys type - must be Array"); if (entityKeys.length === 0) return callback(); var invalid = entityKeys.some(function (entityKey) { return typeof entityKey !== 'string'; }); if (invalid) return callback("$entity.getMany: unexpected entityKey type - must be string"); //assemble promises var promises = entityKeys.map(function (entityKey) { if ($lookup.get(entityKey)) return $q.resolve(); else return get(entityKey); }); //resolve promises $q.all(promises).then(function (results) { results.forEach(function (result, i) { $lookup.set(entityKeys[i], result ? result.data : []); }); callback(null, $lookup.get()); }, function (response) { callback(response.data); }); }; 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); } });