Преглед на файлове

Added details to recipe editor.

Thomas Flucke преди 7 години
родител
ревизия
00666f36ad

+ 20 - 0
src/name/tflucke/ieat2/configs/JacksonConfig.java

@@ -0,0 +1,20 @@
+package name.tflucke.ieat2.configs;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+
+@Configuration
+public class JacksonConfig {
+    @Bean
+    @Primary
+    public ObjectMapper objectMapper() {
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
+        mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
+        return mapper;
+    }
+}

+ 1 - 0
src/name/tflucke/ieat2/models/Food.java

@@ -2,6 +2,7 @@ package name.tflucke.ieat2.models;
 
 import org.mongodb.morphia.annotations.Entity;
 import org.mongodb.morphia.annotations.Transient;
+
 import com.fasterxml.jackson.annotation.JsonProperty;
 
 @Entity("Food")

+ 13 - 1
src/name/tflucke/ieat2/models/Recipe.java

@@ -6,9 +6,13 @@ import org.mongodb.morphia.annotations.Entity;
 import org.mongodb.morphia.annotations.Embedded;
 import org.mongodb.morphia.annotations.Reference;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+
 public class Recipe extends Food {
+    @JsonProperty("result_amount")
+    public int resultAmount;
     public List<String> steps;
-    public List<Ingredient> Ingredients;
+    public List<Ingredient> ingredients;
 
     @Embedded
     public static class Ingredient {
@@ -16,4 +20,12 @@ public class Recipe extends Food {
         @Reference
         public Food item;
     }
+    /*
+    @Embedded
+    public static class Step {
+        public String instruction;
+        public boolean optional = false;
+        public List<Ingredient> ingredients;
+    }
+    */
 }

+ 4 - 4
web/js/ndbDatabase.js

@@ -19,8 +19,8 @@
             },
             transformResponse: function(data, headersGetter, status) {
                 var json = angular.fromJson(data);
-                return status < 400 && json.list?
-                    json.list.item : json.errors.error;
+                return status < 400 && json && json.list? json.list.item :
+                    status > 0? json.errors.error : "Unknown error";
             }
         }
     };
@@ -38,8 +38,8 @@
             },
             transformResponse: function(data, headersGetter, status) {
                 var json = angular.fromJson(data);
-                return status < 400 && json.report.food?
-                    json.report.food : json.errors.error;
+                return status < 400 && json && json.report? json.report.food :
+                    status > 0? json.errors.error : "Unknown error";
             }
         }
     };

+ 50 - 8
web/views/addRecipe.jsp

@@ -11,20 +11,29 @@
     <script type="text/javascript" src="static/searchBar.js"></script>
     <script type="text/javascript">
       var app = angular.module('recipe',
-                               ['ui.bootstrap', 'Food', 'ieat.ui',
+                               ['ui.bootstrap', 'Food', 'ieat.ui', 'ndbDatabase',
                                 'ieat.ui.editors']);
       app.controller('SearchController',
-                     ['$scope', '$uibModal', 'Food', 'Recipe', 'Unit',
-         function($scope, $uibModal, Food, Recipe, Unit) {
+                     ['$scope', '$uibModal', 'Food', 'Recipe', 'Unit', 'NDBList',
+                      function($scope, $uibModal, Food, Recipe, Unit, NDBList) {
              $scope.recipe = new Recipe({
                  steps: [],
                  ingredients: []
              });
+             $scope.categories = NDBList.get({
+                 key: "${ndbKey}",
+                 type: "g"
+             });
              $scope.queryFoods = function(q) {
                  return Food.query({query: q}).$promise;
              };
              var primeUnits = $scope.primeUnits = {};
              var unitList = Unit.query(function(units) {
+                 $scope.unit = units.find(function(u) {
+                     return u.symbol == "";
+                 });
+                 $scope.recipe.unit_type = $scope.unit.type;
+                 $scope.unitList = units;
                  units.forEach(function(u) {
                      if (u.conversion == 1) {
                          $scope.primeUnits[u.type] = u;
@@ -84,18 +93,51 @@
     </script>
   </jsp:attribute>
   <jsp:body>
-    <div class="section container" data-ng-app="recipe" data-ng-controller="SearchController">
+    <div class="section container" data-ng-app="recipe"
+         data-ng-controller="SearchController">
       <h2>Add Recipe</h2>
-      <label for="recipeName">Name:</label>
-      <input id="recipeName" type="text" data-ng-model="recipe.name" />
-      <input type="button" value="Add" class="checkbox-inline" data-ng-click="saveRecipe()" />
+      <div>
+        <label for="recipeName">Name:</label>
+        <input id="recipeName" type="text" data-ng-model="recipe.name" />
+      </div>
+      <div>
+        <label for="amount">Creates:</label>
+        <input id="amount" type="number" data-ng-model="amount"
+               data-ng-change="recipe.result_amount = unit.normalize(recipe, amount);" />
+        <select data-ng-model="unit"
+                data-ng-change="recipe.dry = unit.type != 'volume';
+                                recipe.unit_type = unit.type;
+                                recipe.result_amount = unit.normalize(recipe, amount);"
+                data-ng-options="u as u.symbol for u in unitList">
+        </select>
+      </div>
+      <div>
+        <label for="group">Food Group:</label>
+        <select id="group" data-ng-model="recipe.food_group"
+                data-ng-options="c.name.trim() as c.name.trim()
+                                 for c in categories">
+        </select>
+      </div>
+      <div class="checkbox">
+        <label for="dry">
+          <input id="dry" type="checkbox" data-ng-model="recipe.dry" />
+          Dry
+        </label>
+      </div>
+      <div>
+        <label for="density">Density (g/ml):</label>
+        <input id="density" type="number" data-ng-model="recipe.density" />
+      </div>
+      <input type="button" value="Add" class="checkbox-inline"
+             data-ng-click="saveRecipe()" />
       <table style="width: 100%;">
         <tr>
           <td style="width: 50%;">
             <ol>
               <li data-ng-repeat="step in recipe.steps track by $index">
                 {{step}}
-                <span class="glyphicon glyphicon-remove" data-ng-click="removeStep($index);"></span>
+                <span class="glyphicon glyphicon-remove"
+                      data-ng-click="removeStep($index);"></span>
               </li>
             </ol>
           </td>