浏览代码

Created generic implementation for food objects.

Logic was moved into the food objects for compartmentalization
purposes such as converting an ndb object to basic food or converting
an abstract food type into either a basicfood or recipe.

I had to add angular to the dependency list since the one added by
default did not support components.
Thomas Flucke 7 年之前
父节点
当前提交
7fe163fa8c
共有 4 个文件被更改,包括 78 次插入50 次删除
  1. 2 0
      conf/dev/ant/ivy.xml
  2. 1 0
      web/WEB-INF/tags/template.tag
  3. 74 0
      web/js/Food.js
  4. 1 50
      web/js/basicFoodEditor.js

+ 2 - 0
conf/dev/ant/ivy.xml

@@ -19,6 +19,8 @@
     <dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.9.6"/>
     <!-- UI Resources -->
     <dependency org="org.webjars" name="jquery" rev="2.2.4" conf="war->default"/>
+    <!-- https://mvnrepository.com/artifact/org.webjars/angularjs -->
+    <dependency org="org.webjars" name="angularjs" rev="1.7.5" conf="war->default"/>
     <dependency org="org.webjars" name="angular-ui-bootstrap" rev="2.5.0" conf="war->default"/>
     <dependency org="org.webjars" name="webjars-locator" rev="0.34" conf="war->default"/>
     <dependency org="javax.servlet" name="jstl" rev="1.2" conf="war->default"/>

+ 1 - 0
web/WEB-INF/tags/template.tag

@@ -17,6 +17,7 @@
     <script type="text/javascript" src="static/angularjs/angular-resource.min.js"></script>
     <script type="text/javascript" src="static/angular-ui-bootstrap/ui-bootstrap.min.js"></script>
     <script type="text/javascript" src="static/angular-ui-bootstrap/ui-bootstrap-tpls.min.js"></script>
+    <script type="text/javascript" src="static/Food.js"></script>
     <jsp:invoke fragment="head"/>
   </head>
   <body>

+ 74 - 0
web/js/Food.js

@@ -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;
+        }]);
+})();

+ 1 - 50
web/js/basicFoodEditor.js

@@ -1,53 +1,4 @@
-angular.module('basicFoodEditor', ['ndbDatabase', 'ngResource'])
-    .factory('BasicFood', ['$resource', '$q', function($resource, $q) {
-        return $resource('food/basic/:id', {id: "@id"}, {
-            'get':    {
-                url: "food/:id",
-                method: 'GET'
-            },
-            'query':  {
-                url: "food/basic/query/:query",
-                method: 'GET',
-                isArray: true
-            },
-            'save': {method: 'PUT'},
-            'update': {method: 'POST'},
-            'delete': {
-                url: "food/:id",
-                method: 'DELETE'
-            }
-        });
-    }])
-    .factory('Recipe', ['$resource', '$q', function($resource, $q) {
-        return $resource('food/recipe/:id', {id: "@id"}, {
-            'get':    {
-                url: "food/:id",
-                method: 'GET'
-            },
-            'query':  {
-                url: "food/recipe/query/:query",
-                method: 'GET',
-                isArray: true
-            },
-            'save':   {method: 'PUT'},
-            'update': {method: 'POST'},
-            'delete': {
-                url: "food/:id",
-                method: 'DELETE'
-            }
-        });
-    }])
-    .factory('Food', ['$resource', '$q', function($resource, $q) {
-        return $resource('food/:id', {id: "@id"}, {
-            'get':    {method: 'GET'},
-            'query':  {
-                url: "food/query/:query/",
-                method: 'GET',
-                isArray: true
-            },
-            'delete': {method: 'DELETE'}
-        });
-    }])
+angular.module('basicFoodEditor', ['ndbDatabase', 'ngResource', 'Food'])
     // TODO: Only ever used with editBasicFood template.  See if can auto link.
     .controller('BasicFoodEditorController',
                 ['$scope', '$uibModalInstance', 'NDBList', 'BasicFood', 'foodData', 'ndbKey',