diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java index 5c9f77a8254a47f7c4b49d8ea79ba44578613f87..c534bbf42d964ac430186d55cd02f9bdb1635651 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java @@ -8,7 +8,6 @@ import java.util.List; import org.springframework.http.ResponseEntity; import lombok.AllArgsConstructor; -import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService; @AllArgsConstructor @@ -17,10 +16,10 @@ import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService; public class WeeklyMenuController { private WeeklyMenuService weeklyMenuService; - - @RequestMapping("/getWeeklyMenu/{id}") - public ResponseEntity<List<Product>> getWeeklyMenu(@PathVariable("id") Long id) { - List<Product> weeklyMenu = weeklyMenuService.getWeeklyMenu(id); + + @RequestMapping("/getWeeklyMenu/{fridgeId}") + public ResponseEntity<List<Object[]>> getWeeklyMenu(@PathVariable("fridgeId") Long fridgeId) { + List<Object[]> weeklyMenu = weeklyMenuService.getTop5RecipesWithProducts(fridgeId); if (weeklyMenu.isEmpty()) { return ResponseEntity.notFound().build(); 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 0eb978feac5e8496cee9550dabc8c8dbb54503e2..03ec0776aba1bb9af9185c309cd48a6955bdb306 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -4,6 +4,8 @@ import java.util.List; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; /** * This interface defines the methods for the recipe repository @@ -20,4 +22,52 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { */ List<Recipe> findAllByName(String name); + + @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[]> findTop5RecipesWithProducts(@Param("fridgeId") long 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 3e138e0953eef54874880104bda85a6d90227f66..823b065724909f9b52bbaafd12e434098015550f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java @@ -5,8 +5,7 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import ntnu.idatt2016.v233.SmartMat.entity.product.Product; -import ntnu.idatt2016.v233.SmartMat.repository.FridgeRepository; +import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository; /** * Service class for weekly menu @@ -15,15 +14,10 @@ import ntnu.idatt2016.v233.SmartMat.repository.FridgeRepository; public class WeeklyMenuService { @Autowired - FridgeRepository fridgeRepository; - + RecipeRepository recipeRepository; + + public List<Object[]> getTop5RecipesWithProducts(long groupId) { + return recipeRepository.findTop5RecipesWithProducts(groupId); + } - public List<Product> getWeeklyMenu(long id) { - List<Product> products = fridgeRepository.findById(id).get().getProducts(); - // TODO: Cross reference the products with the recipe_products table - // Now it just returns the products in the fridge - return products; - - } - }