forked from TerryMooreII/angular-azure-mobile-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-azure-mobile-service.min.js
1 lines (1 loc) · 3.2 KB
/
angular-azure-mobile-service.min.js
1
"use strict";angular.module("azure-mobile-service.module",[]).service("Azureservice",["$q","AzureMobileServiceClient",function(a,b){var c=b.API_URL,d=b.API_KEY,e=["google","twitter","facebook","microsoftaccount","aad"],f=WindowsAzure.MobileServiceClient,g=new f(c,d),h=function(){sessionStorage.loggedInUser&&(g.currentUser=JSON.parse(sessionStorage.loggedInUser))};h();var i=function(a,b){return"function"==typeof b?g.withFilter(b).getTable(a):g.getTable(a)},j=function(a){return"undefined"==typeof a||"object"!=typeof a&&"function"!=typeof a},k=function(a){return l(a)||"object"!=typeof a},l=function(a){return null===a||"undefined"==typeof a},m=function(a){return!l(a)},n=function(b){var c=a.defer();return b.done(function(a){c.resolve(a)},function(a){c.reject(a)}),c.promise};return{query:function(a,b,c){var d=null;if(l(a))return console.error("Azureservice.query: You must specify a table name"),null;if(angular.isDefined(b)&&angular.isObject(b)){if(j(b.criteria)&&(b.criteria={}),d=i(a,c).where(b.criteria,b.params),m(b.take)&&angular.isNumber(b.take)&&(d=d.take(b.take)),m(b.skip)&&angular.isNumber(b.take)&&(d=d.skip(b.skip)),angular.isDefined(b.orderBy)&&angular.isArray(b.orderBy))for(var e=b.orderBy,f=0;f<e.length;f++){var g=e[f].column,h=e[f].direction;angular.isDefined(g)&&(angular.isDefined(h)&&"desc"===h.toLowerCase()?d=d.orderByDescending(g):angular.isDefined(g)&&(d=d.orderBy(g)))}angular.isDefined(b.columns)&&angular.isArray(b.columns)&&(d=d.select(b.columns.join()))}else d=i(a,c).where({});return n(d.includeTotalCount().read())},getById:function(a,b,c){return l(a)?(console.error("Azureservice.getById: You must specify a table name"),null):l(b)?(console.error("Azureservice.getById: You must specify the id"),null):n(i(a,c).lookup(b))},getAll:function(a,b){return this.query(a,null,b)},insert:function(a,b,c){return l(a)?(console.error("Azureservice.insert: You must specify a table name"),null):k(b)?(console.error("Azureservice.insert: You must specify the insert object"),null):n(i(a,c).insert(b))},update:function(a,b,c){return l(a)?(console.error("Azureservice.update: You must specify a table name"),null):k(b)?(console.error("Azureservice.update: You must specify the insert object"),null):n(i(a,c).update(b))},del:function(a,b,c){return l(a)?(console.error("Azureservice.del: You must specify a table name"),null):k(b)?(console.error("Azureservice.del: You must specify the insert object"),null):n(i(a,c).del(b))},login:function(a){if(!angular.isDefined(a)||-1===e.indexOf(a))throw new Error("Azureservice.login Invalid or no oauth provider listed.");var b=g.login(a).then(function(){sessionStorage.loggedInUser=JSON.stringify(g.currentUser)});return n(b)},logout:function(){sessionStorage.loggedInUser=null,g.logout()},isLoggedIn:function(){return m(g.currentUser)&&m(sessionStorage.loggedInUser)},invokeApi:function(b,c){var d=a.defer(),e=["get","post","put","delete"];if(l(b))return console.error("Azureservice.invokeApi No custom api name specified"),null;if(k(c))c={method:"get"};else if(l(c.method))c.method="get";else if(-1===e.indexOf(c.method.toLowerCase()))return console.error("Azureservice.invokeApi Invalid method type"),null;return g.invokeApi(b,c).done(function(a){d.resolve(a.result)},function(a){d.reject(a)}),d.promise}}}]);