aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiederick de Vries <diederick@diederickdevries.net>2020-07-06 08:10:04 +0200
committerDiederick de Vries <diederick@diederickdevries.net>2020-07-06 08:10:04 +0200
commit4924d42a2e251d3fbf0130d5c12b8984fd7c304f (patch)
tree208ddde29124ee9b75113d9149f4021c9edf6a02
parentc128ca04f920227c5218698347d3e67946282f74 (diff)
downloadboodschapi-4924d42a2e251d3fbf0130d5c12b8984fd7c304f.tar.gz
boodschapi-4924d42a2e251d3fbf0130d5c12b8984fd7c304f.tar.bz2
boodschapi-4924d42a2e251d3fbf0130d5c12b8984fd7c304f.zip
fix:added groceries should no longer be in the list with available groceries
-rw-r--r--src/main/java/net/diederickdevries/boodschapi/controller/template/ComposerController.java9
-rw-r--r--src/main/java/net/diederickdevries/boodschapi/repository/GroceryRepository.java21
-rw-r--r--src/test/java/net/diederickdevries/boodschapi/repository/GroceryRepositoryIT.java11
3 files changed, 24 insertions, 17 deletions
diff --git a/src/main/java/net/diederickdevries/boodschapi/controller/template/ComposerController.java b/src/main/java/net/diederickdevries/boodschapi/controller/template/ComposerController.java
index a194362..68e77cf 100644
--- a/src/main/java/net/diederickdevries/boodschapi/controller/template/ComposerController.java
+++ b/src/main/java/net/diederickdevries/boodschapi/controller/template/ComposerController.java
@@ -18,7 +18,6 @@
18 */ 18 */
19package net.diederickdevries.boodschapi.controller.template; 19package net.diederickdevries.boodschapi.controller.template;
20 20
21import java.text.SimpleDateFormat;
22import java.time.ZoneId; 21import java.time.ZoneId;
23import java.time.format.DateTimeFormatter; 22import java.time.format.DateTimeFormatter;
24import java.util.ArrayList; 23import java.util.ArrayList;
@@ -85,7 +84,7 @@ public class ComposerController
85 model.addAttribute("mealCategoryId", Category.MEAL_CATEGORY); 84 model.addAttribute("mealCategoryId", Category.MEAL_CATEGORY);
86 model.addAttribute("dinnerLists", findDayDinnerLists(week)); 85 model.addAttribute("dinnerLists", findDayDinnerLists(week));
87 model.addAttribute("meals", findUnchosenActiveMeals(week)); 86 model.addAttribute("meals", findUnchosenActiveMeals(week));
88 model.addAttribute("categoryLists", findCategoryLists(Boolean.parseBoolean(showIngredients))); 87 model.addAttribute("categoryLists", findCategoryLists(week, Boolean.parseBoolean(showIngredients)));
89 model.addAttribute("shopLists", findShopLists(week)); 88 model.addAttribute("shopLists", findShopLists(week));
90 model.addAttribute("showIngredients", Boolean.parseBoolean(showIngredients)); 89 model.addAttribute("showIngredients", Boolean.parseBoolean(showIngredients));
91 90
@@ -120,7 +119,7 @@ public class ComposerController
120 return dinnerLists; 119 return dinnerLists;
121 } 120 }
122 121
123 private List<CategoryGroceryList> findCategoryLists(boolean showIngredients) 122 private List<CategoryGroceryList> findCategoryLists(final Week week, boolean showIngredients)
124 { 123 {
125 List<Category> categories = categoryRepository.findAllOrdered(); 124 List<Category> categories = categoryRepository.findAllOrdered();
126 List<CategoryGroceryList> categoryLists = new ArrayList<>(); 125 List<CategoryGroceryList> categoryLists = new ArrayList<>();
@@ -134,11 +133,11 @@ public class ComposerController
134 CategoryGroceryList list; 133 CategoryGroceryList list;
135 if (Category.NO_CATEGORY == category.getId() || showIngredients) 134 if (Category.NO_CATEGORY == category.getId() || showIngredients)
136 { 135 {
137 list = new CategoryGroceryList(category, groceryRepository.findActiveByCategory(category)); 136 list = new CategoryGroceryList(category, groceryRepository.findUnchosenActiveByCategory(category, week.toString()));
138 } 137 }
139 else 138 else
140 { 139 {
141 list = new CategoryGroceryList(category, groceryRepository.findNonIngredientByCategory(category)); 140 list = new CategoryGroceryList(category, groceryRepository.findUnchosenNonIngredientByCategory(category, week.toString()));
142 } 141 }
143 142
144 if (!(Category.NO_CATEGORY == category.getId() && list.getGroceries().isEmpty())) 143 if (!(Category.NO_CATEGORY == category.getId() && list.getGroceries().isEmpty()))
diff --git a/src/main/java/net/diederickdevries/boodschapi/repository/GroceryRepository.java b/src/main/java/net/diederickdevries/boodschapi/repository/GroceryRepository.java
index 942e877..ee877e8 100644
--- a/src/main/java/net/diederickdevries/boodschapi/repository/GroceryRepository.java
+++ b/src/main/java/net/diederickdevries/boodschapi/repository/GroceryRepository.java
@@ -34,15 +34,20 @@ public interface GroceryRepository extends JpaRepository<Grocery, Integer>
34{ 34{
35 public Grocery findByName(final String name); 35 public Grocery findByName(final String name);
36 36
37 @Query("SELECT g FROM Grocery g WHERE g.category = :#{#category} AND g.show != FALSE ORDER BY g.name") 37 @Query("SELECT g FROM Grocery g"
38 public List<Grocery> findActiveByCategory(@Param("category") Category category); 38 + " WHERE g.category = :#{#category}"
39 39 + " AND g.show != FALSE"
40 + " AND g.id NOT IN (SELECT i.grocery FROM Item i WHERE i.week = :#{#weeknr})"
41 + " ORDER BY g.name")
42 public List<Grocery> findUnchosenActiveByCategory(@Param("category") Category category, @Param("weeknr") String weeknr);
43
40 @Query("SELECT g FROM Grocery g " 44 @Query("SELECT g FROM Grocery g "
41 + "WHERE g.category = :#{#category} " 45 + " WHERE g.category = :#{#category} "
42 + "AND g.show != FALSE " 46 + " AND g.show != FALSE "
43 + "AND (g.showalways = TRUE OR g.id NOT IN (SELECT i.grocery FROM Ingredient i)) " 47 + " AND (g.showalways = TRUE OR g.id NOT IN (SELECT i.grocery FROM Ingredient i)) "
44 + "ORDER BY g.name") 48 + " AND g.id NOT IN (SELECT i.grocery FROM Item i WHERE i.week = :#{#weeknr})"
45 public List<Grocery> findNonIngredientByCategory(@Param("category") Category category); 49 + " ORDER BY g.name")
50 public List<Grocery> findUnchosenNonIngredientByCategory(@Param("category") Category category, @Param("weeknr") String weeknr);
46 51
47 public List<Grocery> findByCategory(Category category); 52 public List<Grocery> findByCategory(Category category);
48 53
diff --git a/src/test/java/net/diederickdevries/boodschapi/repository/GroceryRepositoryIT.java b/src/test/java/net/diederickdevries/boodschapi/repository/GroceryRepositoryIT.java
index 5f75283..70b49d1 100644
--- a/src/test/java/net/diederickdevries/boodschapi/repository/GroceryRepositoryIT.java
+++ b/src/test/java/net/diederickdevries/boodschapi/repository/GroceryRepositoryIT.java
@@ -24,6 +24,7 @@ import net.diederickdevries.boodschapi.model.Category;
24import net.diederickdevries.boodschapi.model.Grocery; 24import net.diederickdevries.boodschapi.model.Grocery;
25import net.diederickdevries.boodschapi.model.Ingredient; 25import net.diederickdevries.boodschapi.model.Ingredient;
26import net.diederickdevries.boodschapi.model.Shop; 26import net.diederickdevries.boodschapi.model.Shop;
27import net.diederickdevries.boodschapi.model.Week;
27import static org.junit.Assert.assertEquals; 28import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertFalse; 29import static org.junit.Assert.assertFalse;
29import org.junit.Test; 30import org.junit.Test;
@@ -46,12 +47,14 @@ public class GroceryRepositoryIT
46 @Autowired private CategoryRepository categoryRepository; 47 @Autowired private CategoryRepository categoryRepository;
47 @Autowired private ShopRepository shopRepository; 48 @Autowired private ShopRepository shopRepository;
48 49
50 final private String week = new Week().toString();
51
49 @Test 52 @Test
50 public void testFindNonIngredientByCategory() 53 public void testFindNonIngredientByCategory()
51 { 54 {
52 Category category = categoryRepository.getOne(Category.NO_CATEGORY); 55 final Category category = categoryRepository.getOne(Category.NO_CATEGORY);
53 List<Ingredient> ingredients = ingredientRepository.findAll(); 56 final List<Ingredient> ingredients = ingredientRepository.findAll();
54 List<Grocery> groceries = groceryRepository.findNonIngredientByCategory(category); 57 List<Grocery> groceries = groceryRepository.findUnchosenNonIngredientByCategory(category, week);
55 58
56 ingredients.stream() 59 ingredients.stream()
57 .filter(i -> i.getGrocery().alwaysShown() == false) 60 .filter(i -> i.getGrocery().alwaysShown() == false)
@@ -71,7 +74,7 @@ public class GroceryRepositoryIT
71 74
72 toShow.getGrocery().setShowAlways(true); 75 toShow.getGrocery().setShowAlways(true);
73 ingredientRepository.saveAndFlush(toShow); 76 ingredientRepository.saveAndFlush(toShow);
74 List<Grocery> groceries = groceryRepository.findNonIngredientByCategory(category); 77 List<Grocery> groceries = groceryRepository.findUnchosenNonIngredientByCategory(category, week);
75 ingredients.forEach((ingredient) -> 78 ingredients.forEach((ingredient) ->
76 { 79 {
77 if (!ingredient.equals(toShow)) { 80 if (!ingredient.equals(toShow)) {