|
|
@@ -0,0 +1,74 @@
|
|
|
+(function() {
|
|
|
+ // Basic api template all foods follow
|
|
|
+ var abstractFood = function($resource, path) {
|
|
|
+ var methods = {
|
|
|
+ 'get': {
|
|
|
+ url: "food/:id",
|
|
|
+ method: 'GET'
|
|
|
+ },
|
|
|
+ 'query': {
|
|
|
+ url: "food/"+path+"query/:query",
|
|
|
+ method: 'GET',
|
|
|
+ isArray: true
|
|
|
+ },
|
|
|
+ 'delete': {
|
|
|
+ url: "food/:id",
|
|
|
+ method: 'DELETE'
|
|
|
+ }
|
|
|
+ };
|
|
|
+ if (path == "") {
|
|
|
+ return $resource("food/"+path+':id', {id: "@id"}, methods);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // Create/Update requires you to know what type of food you're using
|
|
|
+ methods.create = {method: 'PUT'};
|
|
|
+ methods.update = {method: 'POST'};
|
|
|
+ var res = $resource("food/"+path+':id', {id: "@id"}, methods);
|
|
|
+ // TODO: Pass arguments generically
|
|
|
+ res.prototype.$save = function(success, failure) {
|
|
|
+ if (!this.id) {
|
|
|
+ return this.$create(success, failure);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return this.$update(success, failure);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ angular.module('Food', ['ngResource'])
|
|
|
+ .factory('BasicFood', ['$resource', function($resource) {
|
|
|
+ var res = abstractFood($resource, 'basic/');
|
|
|
+ res.fromNdb = function(ndbItem) {
|
|
|
+ return new this({
|
|
|
+ ndbno: parseInt(ndbItem.ndbno),
|
|
|
+ name: ndbItem.name,
|
|
|
+ food_group: ndbItem.fg,
|
|
|
+ default_unit: ndbItem.ru,
|
|
|
+ calories_p_100: ndbItem.nutrients.find(function (nutrient) {
|
|
|
+ // TODO: Replace 208 with soft-loaded value from database
|
|
|
+ // 208 is the id for kCalories
|
|
|
+ return nutrient.nutrient_id == 208;
|
|
|
+ }).value
|
|
|
+ });
|
|
|
+ };
|
|
|
+ return res;
|
|
|
+ }])
|
|
|
+ .factory('Recipe', ['$resource', function($resource) {
|
|
|
+ return abstractFood($resource, 'recipe/');
|
|
|
+ }])
|
|
|
+ .factory('Food', ['$resource', 'BasicFood', 'Recipe', function($resource, BasicFood, Recipe) {
|
|
|
+ var absFood = abstractFood($resource, "");
|
|
|
+ absFood.prototype.cast = function() {
|
|
|
+ switch (this.type) {
|
|
|
+ case "BasicFood":
|
|
|
+ return new BasicFood(this);
|
|
|
+ case "Recipe":
|
|
|
+ return new Recipe(this);
|
|
|
+ default:
|
|
|
+ throw "Unrecognized food type: "+this.type;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return absFood;
|
|
|
+ }]);
|
|
|
+})();
|