From dddba73bd743ff968acf323e75be285c5792252f Mon Sep 17 00:00:00 2001 From: Stian Lyng <stianlyng@protonmail.com> Date: Thu, 4 May 2023 10:12:47 +0200 Subject: [PATCH] fix weekly menu --- .../dto/response/WeeklyMenuResponse.java | 1 + .../SmartMat/repository/RecipeRepository.java | 57 +------------------ .../SmartMat/service/WeeklyMenuService.java | 3 +- 3 files changed, 4 insertions(+), 57 deletions(-) diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/WeeklyMenuResponse.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/WeeklyMenuResponse.java index 109e337e..3e203560 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/WeeklyMenuResponse.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/WeeklyMenuResponse.java @@ -17,6 +17,7 @@ public class WeeklyMenuResponse { private Integer recipeId; private String recipeName; private String recipeDescription; + private String recipeImage; private long matchingProducts; } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java index 122fda1f..efea184a 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -22,63 +22,8 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { */ List<Recipe> findAllByName(String name); - /** - * Finds the top 5 recipes with products that have a match with items in the given fridge. - * Returns a list of Object arrays, where each array contains the recipe details and product information. - * - * @param fridgeId the ID of the fridge to use for matching products - * @return a list of Object arrays with recipe and product details - */ - @Query(value = """ - WITH fridge_products AS ( - SELECT ean - FROM public.fridge_product - WHERE fridge_id = :fridgeId - ), - matched_products_count AS ( - SELECT - r.recipe_id, - r.recipe_name, - COUNT(*) AS product_count - FROM - public.recipe_product rp - JOIN public.recipe r ON rp.recipe_id = r.recipe_id - JOIN fridge_products fp ON rp.ean = fp.ean - GROUP BY - r.recipe_id, - r.recipe_name - ), - top_5_recipes AS ( - SELECT - recipe_id, - recipe_name, - product_count - FROM - matched_products_count - ORDER BY - product_count DESC, - recipe_id - LIMIT 5 - ) - SELECT - t.recipe_id, - t.recipe_name, - p.ean, - p.item_name, - p.description, - (rp.ean IN (SELECT ean FROM fridge_products)) AS in_fridge - FROM - top_5_recipes t - JOIN public.recipe_product rp ON t.recipe_id = rp.recipe_id - JOIN public.product p ON rp.ean = p.ean - ORDER BY - t.recipe_id, - p.ean; - """, nativeQuery = true) - List<Object[]> findTop5RecipesWithProductsRaw(@Param("fridgeId") long fridgeId); - @Query( value = """ - SELECT r.recipe_id, r.recipe_name,r.recipe_description,r.image_url,r.guide, COUNT(fp.ean) as product_count + SELECT r.recipe_id, r.recipe_name,r.recipe_description,r.image_url, COUNT(fp.ean) as product_count FROM recipe r LEFT JOIN recipe_product rp ON r.recipe_id = rp.recipe_id LEFT JOIN fridge_product fp ON rp.ean = fp.ean AND fp.fridge_id = :fridgeId diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java index 870b3553..8965be1d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java @@ -38,7 +38,8 @@ public class WeeklyMenuService { (Integer) row[0], (String) row[1], (String) row[2], - (long) row[3] + (String) row[3], + (long) row[4] )) .collect(Collectors.toList()); -- GitLab