aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiederick de Vries <diederick@diederickdevries.net>2020-02-17 21:58:15 +0100
committerDiederick de Vries <diederick@diederickdevries.net>2020-02-17 22:22:01 +0100
commit9fd143b0d8d47c7883a7798cfb93207ce6cac35c (patch)
tree554f11204d37a5bd979c5db5173c767dd76a56b7
parent31ed4f42f972149896ae379388d57529cb1d9b8b (diff)
downloadboodschapi-feat/co2.tar.gz
boodschapi-feat/co2.tar.bz2
boodschapi-feat/co2.zip
feature: add co2/g amount to a groceryfeat/co2
-rw-r--r--src/main/java/net/diederickdevries/boodschapi/controller/GroceryController.java152
-rw-r--r--src/main/resources/static/js/grocery.js6
-rw-r--r--src/test/java/net/diederickdevries/boodschapi/controller/GroceryControllerIT.java52
3 files changed, 138 insertions, 72 deletions
diff --git a/src/main/java/net/diederickdevries/boodschapi/controller/GroceryController.java b/src/main/java/net/diederickdevries/boodschapi/controller/GroceryController.java
index 40bc5ad..d1085b2 100644
--- a/src/main/java/net/diederickdevries/boodschapi/controller/GroceryController.java
+++ b/src/main/java/net/diederickdevries/boodschapi/controller/GroceryController.java
@@ -55,20 +55,23 @@ public class GroceryController
55 public static final String MSG_404_GROCERY = "De boodschap met id % bestaat niet"; 55 public static final String MSG_404_GROCERY = "De boodschap met id % bestaat niet";
56 public static final String MSG_400_EMPTY = "De ingevoerde naam was leeg"; 56 public static final String MSG_400_EMPTY = "De ingevoerde naam was leeg";
57 public static final String MSG_400_GROCERY_EXISTS = "De boodschap %0 bestaat al in de categorie %1"; 57 public static final String MSG_400_GROCERY_EXISTS = "De boodschap %0 bestaat al in de categorie %1";
58 58
59 @Autowired private GroceryRepository groceryRepository; 59 @Autowired
60 @Autowired private CategoryRepository categoryRepository; 60 private GroceryRepository groceryRepository;
61 @Autowired private IngredientRepository ingredientRepository; 61 @Autowired
62 @Autowired private ItemRepository itemRepository; 62 private CategoryRepository categoryRepository;
63 63 @Autowired
64 private IngredientRepository ingredientRepository;
65 @Autowired
66 private ItemRepository itemRepository;
67
64 /** 68 /**
65 * Adds a new grocery to the category with the id given, unless the category 69 * Adds a new grocery to the category with the id given, unless the category is the default
66 * is the default category or the meal category, or no category exists with 70 * category or the meal category, or no category exists with that id.
67 * that id.
68 * 71 *
69 * @param categoryId The id of the category to add the new grocery to. 72 * @param categoryId The id of the category to add the new grocery to.
70 * @return A response entity containing the new grocery, and a status 73 * @return A response entity containing the new grocery, and a status indicating where the new
71 * indicating where the new resource can be found, if succesful. 74 * resource can be found, if succesful.
72 * @throws java.net.URISyntaxException 75 * @throws java.net.URISyntaxException
73 */ 76 */
74 @RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = "application/json;charset=UTF-8") 77 @RequestMapping(value = "/add", method = RequestMethod.PUT, consumes = "application/json;charset=UTF-8")
@@ -77,21 +80,21 @@ public class GroceryController
77 if (Category.MEAL_CATEGORY == categoryId) 80 if (Category.MEAL_CATEGORY == categoryId)
78 { 81 {
79 return ResponseEntity 82 return ResponseEntity
80 .badRequest() 83 .badRequest()
81 .body(MSG_400_ADD_MEAL_CAT); 84 .body(MSG_400_ADD_MEAL_CAT);
82 } 85 }
83 if (Category.NO_CATEGORY == categoryId) 86 if (Category.NO_CATEGORY == categoryId)
84 { 87 {
85 return ResponseEntity 88 return ResponseEntity
86 .badRequest() 89 .badRequest()
87 .body(MSG_400_ADD_STD_CAT); 90 .body(MSG_400_ADD_STD_CAT);
88 } 91 }
89 Optional<Category> optionalCategory = categoryRepository.findById(categoryId); 92 Optional<Category> optionalCategory = categoryRepository.findById(categoryId);
90 if (!optionalCategory.isPresent()) 93 if (!optionalCategory.isPresent())
91 { 94 {
92 return ResponseEntity 95 return ResponseEntity
93 .status(HttpStatus.NOT_FOUND) 96 .status(HttpStatus.NOT_FOUND)
94 .body(MessageUtil.parse(MSG_404_CATEGORY, categoryId + "")); 97 .body(MessageUtil.parse(MSG_404_CATEGORY, categoryId + ""));
95 } 98 }
96 Category category = optionalCategory.get(); 99 Category category = optionalCategory.get();
97 Grocery grocery = new Grocery(); 100 Grocery grocery = new Grocery();
@@ -100,8 +103,8 @@ public class GroceryController
100 grocery.setShow(true); 103 grocery.setShow(true);
101 groceryRepository.saveAndFlush(grocery); 104 groceryRepository.saveAndFlush(grocery);
102 return ResponseEntity 105 return ResponseEntity
103 .created(RequestUtil.getResourceURI(grocery.getId())) 106 .created(RequestUtil.getResourceURI(grocery.getId()))
104 .body(grocery); 107 .body(grocery);
105 } 108 }
106 109
107 @RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") 110 @RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@@ -115,56 +118,67 @@ public class GroceryController
115 else 118 else
116 { 119 {
117 return ResponseEntity 120 return ResponseEntity
118 .status(HttpStatus.NOT_FOUND) 121 .status(HttpStatus.NOT_FOUND)
119 .body(MessageUtil.parse(MSG_404_GROCERY, id + "")); 122 .body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
120 } 123 }
121 } 124 }
122 125
123 /** 126 /**
124 * Changes the name of the Grocery with the id given into the value 127 * Changes the name and/or co2 amount of the Grocery with the id given into the value given. If
125 * given. If the name was already in use by another hidden grocery, it is 128 * the name was already in use by another hidden grocery, it is deleted and the existing grocery
126 * deleted and the existing grocery is no longer hidden. Any references to 129 * is no longer hidden. Any references to the deleted grocery (meals, shopping lists) are not
127 * the deleted grocery (meals, shopping lists) are not pointing to the 130 * pointing to the existing grocery.
128 * existing grocery.
129 * 131 *
130 * @param id The id of the Grocery to change. 132 * @param id The id of the Grocery to change.
131 * @param value The new name of the Grocery. 133 * @param name The new name of the Grocery.
134 * @param co2 The amount of co2/g for the production and transportation of this grocery.
132 * @return The Grocery with the new name. 135 * @return The Grocery with the new name.
136 * @throws java.net.URISyntaxException
133 */ 137 */
134 @RequestMapping(value = "/edit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 138 @RequestMapping(value = "/edit", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
135 public ResponseEntity<Object> edit(final Integer id, final String value) throws URISyntaxException 139 public ResponseEntity<Object> edit(final Integer id, final String name, final Integer co2) throws URISyntaxException
136 { 140 {
137 final String name = value.trim(); 141 Optional<Grocery> optionalGrocery = groceryRepository.findById(id);
138 if (name.isEmpty()) 142 if (!optionalGrocery.isPresent())
139 { 143 {
140 return ResponseEntity 144 return ResponseEntity
141 .badRequest() 145 .status(HttpStatus.NOT_FOUND)
142 .body(MSG_400_EMPTY); 146 .body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
143 } 147 }
144 148
145 Optional<Grocery> optionalGrocery = groceryRepository.findById(id); 149 if ((null == name || name.isEmpty()) && null == co2)
146 if (!optionalGrocery.isPresent())
147 { 150 {
148 return ResponseEntity 151 return ResponseEntity
149 .status(HttpStatus.NOT_FOUND) 152 .badRequest()
150 .body(MessageUtil.parse(MSG_404_GROCERY, id + "")); 153 .body(MSG_400_EMPTY);
151 } 154 }
152 155
153 Grocery grocery = optionalGrocery.get(); 156 Grocery grocery = optionalGrocery.get();
154 157
155 Grocery existing = groceryRepository.findByName(name); 158 if (null != name)
156 if (null != existing)
157 { 159 {
158 moveReferences(grocery, existing); 160 Grocery existing = groceryRepository.findByName(name);
159 existing.setShow(true); 161 if (null != existing)
160 groceryRepository.save(existing); 162 {
161 groceryRepository.delete(grocery); 163 moveReferences(grocery, existing);
162 groceryRepository.flush(); 164 existing.setShow(true);
163 return ResponseEntity 165 groceryRepository.save(existing);
166 groceryRepository.delete(grocery);
167 groceryRepository.flush();
168 return ResponseEntity
164 .status(HttpStatus.MOVED_PERMANENTLY) 169 .status(HttpStatus.MOVED_PERMANENTLY)
165 .body(RequestUtil.getResourceURI(existing.getId())); 170 .body(RequestUtil.getResourceURI(existing.getId()));
171 }
172 }
173
174 if (null != name)
175 {
176 grocery.setName(name.trim());
177 }
178 if (null != co2)
179 {
180 grocery.setCo2(co2);
166 } 181 }
167 grocery.setName(name);
168 groceryRepository.saveAndFlush(grocery); 182 groceryRepository.saveAndFlush(grocery);
169 return ResponseEntity.ok(grocery); 183 return ResponseEntity.ok(grocery);
170 } 184 }
@@ -182,8 +196,8 @@ public class GroceryController
182 if (!optionalGrocery.isPresent()) 196 if (!optionalGrocery.isPresent())
183 { 197 {
184 return ResponseEntity 198 return ResponseEntity
185 .status(HttpStatus.NOT_FOUND) 199 .status(HttpStatus.NOT_FOUND)
186 .body(MessageUtil.parse(MSG_404_GROCERY, id + "")); 200 .body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
187 } 201 }
188 Grocery grocery = optionalGrocery.get(); 202 Grocery grocery = optionalGrocery.get();
189 groceryRepository.delete(grocery); 203 groceryRepository.delete(grocery);
@@ -206,22 +220,22 @@ public class GroceryController
206 if (!optionalGrocery.isPresent()) 220 if (!optionalGrocery.isPresent())
207 { 221 {
208 return ResponseEntity 222 return ResponseEntity
209 .status(HttpStatus.NOT_FOUND) 223 .status(HttpStatus.NOT_FOUND)
210 .body(MessageUtil.parse(MSG_404_GROCERY, groceryid + "")); 224 .body(MessageUtil.parse(MSG_404_GROCERY, groceryid + ""));
211 } 225 }
212 if (!optionalCategory.isPresent()) 226 if (!optionalCategory.isPresent())
213 { 227 {
214 return ResponseEntity 228 return ResponseEntity
215 .status(HttpStatus.NOT_FOUND) 229 .status(HttpStatus.NOT_FOUND)
216 .body(MessageUtil.parse(MSG_404_CATEGORY, categoryid + "")); 230 .body(MessageUtil.parse(MSG_404_CATEGORY, categoryid + ""));
217 } 231 }
218 final Category category = optionalCategory.get(); 232 final Category category = optionalCategory.get();
219 final Grocery grocery = optionalGrocery.get(); 233 final Grocery grocery = optionalGrocery.get();
220 if (categoryid.equals(grocery.getCategory().getId())) 234 if (categoryid.equals(grocery.getCategory().getId()))
221 { 235 {
222 return ResponseEntity 236 return ResponseEntity
223 .badRequest() 237 .badRequest()
224 .body(MessageUtil.parse(MSG_400_GROCERY_EXISTS, grocery.getName(), category.getName())); 238 .body(MessageUtil.parse(MSG_400_GROCERY_EXISTS, grocery.getName(), category.getName()));
225 } 239 }
226 grocery.setCategory(category); 240 grocery.setCategory(category);
227 groceryRepository.saveAndFlush(grocery); 241 groceryRepository.saveAndFlush(grocery);
@@ -229,9 +243,8 @@ public class GroceryController
229 } 243 }
230 244
231 /** 245 /**
232 * Sets the grocery with the id given as not to be shown. If the grocery is 246 * Sets the grocery with the id given as not to be shown. If the grocery is an ingredient, it is
233 * an ingredient, it is set to not be shown unless ingredients are to be 247 * set to not be shown unless ingredients are to be shown.
234 * shown.
235 * 248 *
236 * @param id The id of the Grocery to hide. 249 * @param id The id of the Grocery to hide.
237 * @return the Grocery 250 * @return the Grocery
@@ -243,8 +256,8 @@ public class GroceryController
243 if (!optionalGrocery.isPresent()) 256 if (!optionalGrocery.isPresent())
244 { 257 {
245 return ResponseEntity 258 return ResponseEntity
246 .status(HttpStatus.NOT_FOUND) 259 .status(HttpStatus.NOT_FOUND)
247 .body(MessageUtil.parse(MSG_404_GROCERY, id + "")); 260 .body(MessageUtil.parse(MSG_404_GROCERY, id + ""));
248 } 261 }
249 Grocery grocery = optionalGrocery.get(); 262 Grocery grocery = optionalGrocery.get();
250 if (!ingredientRepository.findByGrocery(grocery).isEmpty()) 263 if (!ingredientRepository.findByGrocery(grocery).isEmpty())
@@ -259,15 +272,22 @@ public class GroceryController
259 return ResponseEntity.ok().body(grocery); 272 return ResponseEntity.ok().body(grocery);
260 } 273 }
261 274
262 private void moveReferences(Grocery from, Grocery to) { 275 private void moveReferences(Grocery from, Grocery to)
276 {
263 List<Ingredient> ingredients = ingredientRepository.findByGrocery(from); 277 List<Ingredient> ingredients = ingredientRepository.findByGrocery(from);
264 ingredients.forEach(i -> { i.setGrocery(to); }); 278 ingredients.forEach(i ->
279 {
280 i.setGrocery(to);
281 });
265 ingredientRepository.saveAll(ingredients); 282 ingredientRepository.saveAll(ingredients);
266 ingredientRepository.flush(); 283 ingredientRepository.flush();
267 284
268 List<Item> items = itemRepository.findByGrocery(from); 285 List<Item> items = itemRepository.findByGrocery(from);
269 items.forEach(i -> { i.setGrocery(to); }); 286 items.forEach(i ->
287 {
288 i.setGrocery(to);
289 });
270 itemRepository.saveAll(items); 290 itemRepository.saveAll(items);
271 itemRepository.flush(); 291 itemRepository.flush();
272 } 292 }
273} 293}
diff --git a/src/main/resources/static/js/grocery.js b/src/main/resources/static/js/grocery.js
index dc278ef..b5edaa9 100644
--- a/src/main/resources/static/js/grocery.js
+++ b/src/main/resources/static/js/grocery.js
@@ -95,6 +95,9 @@ function editGrocery(id, value)
95 else 95 else
96 { 96 {
97 var url = getContextPath() + "grocery/edit"; 97 var url = getContextPath() + "grocery/edit";
98 var input = value.split(' ');
99 var name = input[0];
100 var co2 = input[1];
98 101
99 var ajax = $.ajax({ 102 var ajax = $.ajax({
100 url: url, 103 url: url,
@@ -102,7 +105,8 @@ function editGrocery(id, value)
102 contentType: "application/x-www-form-urlencoded", 105 contentType: "application/x-www-form-urlencoded",
103 data: { 106 data: {
104 id: id, 107 id: id,
105 value: value 108 name: name,
109 co2: co2
106 }, 110 },
107 dataType: "json" 111 dataType: "json"
108 }); 112 });
diff --git a/src/test/java/net/diederickdevries/boodschapi/controller/GroceryControllerIT.java b/src/test/java/net/diederickdevries/boodschapi/controller/GroceryControllerIT.java
index 57503a0..3f6791a 100644
--- a/src/test/java/net/diederickdevries/boodschapi/controller/GroceryControllerIT.java
+++ b/src/test/java/net/diederickdevries/boodschapi/controller/GroceryControllerIT.java
@@ -39,7 +39,6 @@ import org.junit.Test;
39import org.junit.runner.RunWith; 39import org.junit.runner.RunWith;
40import org.springframework.beans.factory.annotation.Autowired; 40import org.springframework.beans.factory.annotation.Autowired;
41import org.springframework.boot.test.context.SpringBootTest; 41import org.springframework.boot.test.context.SpringBootTest;
42import org.springframework.http.HttpStatus;
43import org.springframework.http.ResponseEntity; 42import org.springframework.http.ResponseEntity;
44import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 43import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45 44
@@ -128,7 +127,7 @@ public class GroceryControllerIT
128 } 127 }
129 128
130 @Test 129 @Test
131 public void testEdit() throws URISyntaxException 130 public void testEditName() throws URISyntaxException
132 { 131 {
133 final String oldName = "testEditGrocery"; 132 final String oldName = "testEditGrocery";
134 final String newName = "newName"; 133 final String newName = "newName";
@@ -139,13 +138,56 @@ public class GroceryControllerIT
139 grocery.setShow(true); 138 grocery.setShow(true);
140 groceryRepository.saveAndFlush(grocery); 139 groceryRepository.saveAndFlush(grocery);
141 140
142 ResponseEntity response = groceryController.edit(grocery.getId(), newName); 141 ResponseEntity response = groceryController.edit(grocery.getId(), newName, null);
143 142
144 assertEquals("The response status", 200, response.getStatusCodeValue()); 143 assertEquals("The response status", 200, response.getStatusCodeValue());
145 assertEquals("The new name", newName, grocery.getName()); 144 assertEquals("The new name", newName, grocery.getName());
146 } 145 }
147 146
148 @Test 147 @Test
148 public void testEditOnlyCo2() throws URISyntaxException
149 {
150 final Integer oldCo2 = null;
151 final Integer newCo2 = 15;
152
153 Grocery grocery = new Grocery();
154 grocery.setCo2(oldCo2);
155 grocery.setCategory(categoryRepository.findById(Category.NO_CATEGORY).orElse(null));
156 grocery.setShow(true);
157 groceryRepository.saveAndFlush(grocery);
158
159 ResponseEntity response = groceryController.edit(grocery.getId(), null, newCo2);
160
161 assertEquals("The response status", 200, response.getStatusCodeValue());
162 assertEquals("The new co2", newCo2, grocery.getCo2());
163 }
164
165 @Test
166 public void testEditNameAndCo2() throws URISyntaxException
167 {
168 final String oldName = "testEditGrocery";
169 final String newName = "newName";
170
171 final Integer oldCo2 = 10;
172 final Integer newCo2 = 15;
173
174 Grocery grocery = new Grocery();
175 grocery.setName(oldName);
176 grocery.setCo2(oldCo2);
177 grocery.setCategory(categoryRepository.findById(Category.NO_CATEGORY).orElse(null));
178 grocery.setShow(true);
179 groceryRepository.saveAndFlush(grocery);
180
181 ResponseEntity response = groceryController.edit(grocery.getId(), newName, newCo2);
182
183 assertEquals("The response status", 200, response.getStatusCodeValue());
184 assertEquals("The new name", newName, grocery.getName());
185 assertEquals("The new co2", newCo2, grocery.getCo2());
186 }
187
188
189
190 @Test
149 public void testEditExistingName() throws URISyntaxException 191 public void testEditExistingName() throws URISyntaxException
150 { 192 {
151 final String existingName = "testEditExistingName"; 193 final String existingName = "testEditExistingName";
@@ -176,7 +218,7 @@ public class GroceryControllerIT
176 item.setShop(shopRepository.getOne(Shop.DEFAULT_SHOP)); 218 item.setShop(shopRepository.getOne(Shop.DEFAULT_SHOP));
177 itemRepository.saveAndFlush(item); 219 itemRepository.saveAndFlush(item);
178 220
179 ResponseEntity response = groceryController.edit(grocery.getId(), existingName); 221 ResponseEntity response = groceryController.edit(grocery.getId(), existingName, null);
180 222
181 Grocery groceryFound = groceryRepository.findById(grocery.getId()).orElse(null); 223 Grocery groceryFound = groceryRepository.findById(grocery.getId()).orElse(null);
182 Item itemFound = itemRepository.findById(item.getId()).orElse(null); 224 Item itemFound = itemRepository.findById(item.getId()).orElse(null);
@@ -201,7 +243,7 @@ public class GroceryControllerIT
201 grocery.setShow(true); 243 grocery.setShow(true);
202 groceryRepository.saveAndFlush(grocery); 244 groceryRepository.saveAndFlush(grocery);
203 245
204 ResponseEntity response = groceryController.edit(grocery.getId(), ""); 246 ResponseEntity response = groceryController.edit(grocery.getId(), "", null);
205 247
206 assertEquals("The response status", 400, response.getStatusCodeValue()); 248 assertEquals("The response status", 400, response.getStatusCodeValue());
207 assertEquals("The new name", oldName, grocery.getName()); 249 assertEquals("The new name", oldName, grocery.getName());