Skip to content

Commit

Permalink
manage: filter recipes by ingredient
Browse files Browse the repository at this point in the history
  • Loading branch information
kassner committed Nov 15, 2024
1 parent 1cc6516 commit 08d0183
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main/java/se/kassner/whattocook/RecipeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long>

@Query("SELECT r FROM Recipe r ORDER BY r.name ASC")
public List<Recipe> findAllForListing();

@Query(
value = "SELECT r.* FROM recipe r WHERE r.id IN (SELECT ri.recipe_id FROM recipe_ingredients ri WHERE ri.ingredient_id IN (:ingredient_ids))",
nativeQuery = true
)
public List<Recipe> findAllForListingWithIngredients(@Param("ingredient_ids") List<Long> includedIngredientIds);
}
15 changes: 13 additions & 2 deletions src/main/java/se/kassner/whattocook/manage/RecipeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import se.kassner.whattocook.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/manage/recipe")
Expand All @@ -28,9 +30,18 @@ public RecipeController(IngredientRepository ingredientRepository, RecipeReposit
}

@GetMapping
public String get(Model model)
public String get(Model model, @RequestParam(value = "ingredient", required = false) Long ingredientId)
{
List<Recipe> recipes = recipeRepository.findAllForListing();
List<Recipe> recipes;

if (ingredientId == null || ingredientId == 0) {
recipes = recipeRepository.findAllForListing();
} else {
recipes = recipeRepository.findAllForListingWithIngredients(Collections.singletonList(ingredientId));
Optional<Ingredient> ingredient = ingredientRepository.findById(ingredientId);
ingredient.ifPresent(value -> model.addAttribute("ingredient", value));
}

model.addAttribute("recipes", recipes);

return "recipe/index";
Expand Down
14 changes: 14 additions & 0 deletions src/main/js/manage/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@ h1 {
@apply btn btn-sm;
}

.btn-clear {
@apply btn btn-sm btn-outline;
}

.alert {
@apply mb-8;
}

.filters {
@apply alert;

margin-bottom: 0.5rem;

p {
line-height: 32px;
}
}

.table {
thead {
tr {
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/templates/recipe/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<h1 class="flex-grow">Manage recipes</h1>
<a class="btn-add self-center" th:href="@{/manage/recipe/add}">Add</a>
</div>
<div class="filters" th:if="${ingredient != null}">
<p th:text="'Filtering by ingredient: ' + ${ingredient.getName()}" />
<a class="btn-clear self-center" th:href="@{/manage/recipe}">Clear</a>
</div>
<table class="table table-pin-rows table-pin-cols w-full">
<thead>
<tr>
Expand All @@ -23,7 +27,9 @@ <h1 class="flex-grow">Manage recipes</h1>
<td class="name">
<p th:text="${recipe.getName()}"></p>
<ul>
<li th:each="ingredient: ${recipe.getIngredients()}" th:text="${ingredient.getName()}"></li>
<li th:each="ingredient: ${recipe.getIngredients()}">
<a th:href="@{/manage/recipe(ingredient=${ingredient.getId()})}" th:text="${ingredient.getName()}" />
</li>
</ul>
</td>
<td class="actions">
Expand Down

0 comments on commit 08d0183

Please sign in to comment.