aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiederick de Vries <diederick@diederickdevries.net>2020-06-29 10:30:32 +0200
committerDiederick de Vries <diederick@diederickdevries.net>2020-06-29 10:30:32 +0200
commit787029324d65ec9791d73d1431ceb28917fbba35 (patch)
tree425d0de5f4af66ec2a80314fb3d8328b09c23e47
parent1926b6b9b17c2768be8b60a45d3dbf90338818be (diff)
downloadboodschapi-787029324d65ec9791d73d1431ceb28917fbba35.tar.gz
boodschapi-787029324d65ec9791d73d1431ceb28917fbba35.tar.bz2
boodschapi-787029324d65ec9791d73d1431ceb28917fbba35.zip
feature:add controller to copy the items of each of the lists of the last three weeks to the current list.
-rw-r--r--src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java31
-rw-r--r--src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java33
2 files changed, 62 insertions, 2 deletions
diff --git a/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java b/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java
index a7f6201..e26ce5d 100644
--- a/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java
+++ b/src/main/java/net/diederickdevries/boodschapi/controller/ItemController.java
@@ -19,6 +19,8 @@
19package net.diederickdevries.boodschapi.controller; 19package net.diederickdevries.boodschapi.controller;
20 20
21import java.net.URISyntaxException; 21import java.net.URISyntaxException;
22import java.util.ArrayList;
23import java.util.List;
22import java.util.Optional; 24import java.util.Optional;
23import net.diederickdevries.boodschapi.model.Grocery; 25import net.diederickdevries.boodschapi.model.Grocery;
24import net.diederickdevries.boodschapi.model.Item; 26import net.diederickdevries.boodschapi.model.Item;
@@ -286,4 +288,33 @@ public class ItemController
286 item.getGrocery().setBought(null); 288 item.getGrocery().setBought(null);
287 groceryRepository.saveAndFlush(item.getGrocery()); 289 groceryRepository.saveAndFlush(item.getGrocery());
288 } 290 }
291
292 /**
293 * copyWeeklyBoughtItems will make a copy in the current list of any item
294 * that was on the lists of the three last weeks. The copy will have the
295 * amount of last week.
296 *
297 * @return The copied items
298 */
299 @RequestMapping(value = "/copyWeeklyBoughtItems", method = RequestMethod.GET,
300 produces = "application/json;charset=UTF-8")
301 public ResponseEntity<Object> copyWeeklyBoughtItems()
302 {
303 final Week week = new Week();
304 final Week lastWeek = week.getLastWeek();
305 final Week twoWeeksAgo = lastWeek.getLastWeek();
306 final Week threeWeeksAgo = twoWeeksAgo.getLastWeek();
307
308 final List<Item> weeklyBoughts = itemRepository.findByWeeks(lastWeek.toString(),
309 twoWeeksAgo.toString(), threeWeeksAgo.toString());
310
311 List<Item> copies = new ArrayList<>();
312
313 weeklyBoughts.forEach(item -> {
314 copies.add(new Item(item.getGrocery(), item.getShop(), item.getAmount(), week));
315 });
316 itemRepository.saveAll(copies);
317
318 return ResponseEntity.ok(copies);
319 }
289} 320}
diff --git a/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java b/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java
index f3d57fe..154b627 100644
--- a/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java
+++ b/src/test/java/net/diederickdevries/boodschapi/controller/ItemControllerIT.java
@@ -51,7 +51,7 @@ public class ItemControllerIT
51 @Autowired private ItemRepository itemRepository; 51 @Autowired private ItemRepository itemRepository;
52 @Autowired private ShopRepository shopRepository; 52 @Autowired private ShopRepository shopRepository;
53 @Autowired private GroceryRepository groceryRepository; 53 @Autowired private GroceryRepository groceryRepository;
54 @Autowired private CategoryRepository categroryRepository; 54 @Autowired private CategoryRepository categoryRepository;
55 55
56 private Item testItem; 56 private Item testItem;
57 57
@@ -62,7 +62,7 @@ public class ItemControllerIT
62 public void setUp() 62 public void setUp()
63 { 63 {
64 jumbo = shopRepository.findById(0).orElse(null); 64 jumbo = shopRepository.findById(0).orElse(null);
65 Category none = categroryRepository.findById(Category.NO_CATEGORY).orElse(null); 65 Category none = categoryRepository.findById(Category.NO_CATEGORY).orElse(null);
66 66
67 assertNotNull("The default category must exist", none); 67 assertNotNull("The default category must exist", none);
68 68
@@ -117,4 +117,33 @@ public class ItemControllerIT
117 assertNull("Item should no longer be bought", testItem.getBought()); 117 assertNull("Item should no longer be bought", testItem.getBought());
118 } 118 }
119 119
120 /**
121 * Test that any item on each of the lists of the last three weeks is copied
122 * to the current list.
123 */
124 @Test
125 public void testCopyWeeklyBoughtItems()
126 {
127 final Shop shop = new Shop("shop");
128 final Grocery grocery = new Grocery("grocery",
129 categoryRepository.getOne(Category.NO_CATEGORY));
130 shopRepository.saveAndFlush(shop);
131 groceryRepository.saveAndFlush(grocery);
132
133 final Week week = new Week();
134 final Week lastWeek = week.getLastWeek();
135 final Week twoWeeksAgo = lastWeek.getLastWeek();
136 final Week threeWeeksAgo = twoWeeksAgo.getLastWeek();
137
138 itemRepository.saveAndFlush(new Item(grocery, shop, 1, lastWeek));
139 itemRepository.saveAndFlush(new Item(grocery, shop, 2, twoWeeksAgo));
140 itemRepository.saveAndFlush(new Item(grocery, shop, 2, threeWeeksAgo));
141
142 controller.copyWeeklyBoughtItems();
143
144 Item found = itemRepository.findByWeekAndGrocery(week.toString(), grocery);
145 assertEquals("The grocery should be on the lists of the three last weeks",
146 grocery, found.getGrocery());
147 assertTrue("The amount should be of the last week's list", 1 == found.getAmount());
148 }
120} \ No newline at end of file 149} \ No newline at end of file