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 54d341ba926db90192a38c876d21bb9697157565..f7e4e753ffd86cd5465fa96232b849f4100aff76 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java @@ -18,10 +18,10 @@ import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService; public class WeeklyMenuController { private WeeklyMenuService weeklyMenuService; - - @GetMapping("/getWeeklyMenu/{fridgeId}") + + @GetMapping("/{fridgeId}") public ResponseEntity<List<WeeklyMenuResponse>> getWeeklyMenu(@PathVariable("fridgeId") Long fridgeId) { - List<WeeklyMenuResponse> weeklyMenu = weeklyMenuService.getTop5RecipesWithProducts(fridgeId); + List<WeeklyMenuResponse> weeklyMenu = weeklyMenuService.getWeeklyMenu(fridgeId); if (weeklyMenu.isEmpty()) { return ResponseEntity.notFound().build(); 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 76cd0abf85ae6b4116c99e51ae73d447e0a8114d..109e337e00229e68a6678390276de9a4559a02af 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 @@ -16,9 +16,7 @@ public class WeeklyMenuResponse { private Integer recipeId; private String recipeName; - private Long ean; - private String itemName; - private String itemDescription; - private boolean inFridge; + private String recipeDescription; + 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 cbaf75044630bd95366a4554a64a18a59e0315b4..41208c0bc9a4ef0c0f3e071f3f6638bb3a66de15 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -1,9 +1,7 @@ package ntnu.idatt2016.v233.SmartMat.repository; import java.util.List; -import java.util.stream.Collectors; -import ntnu.idatt2016.v233.SmartMat.dto.response.WeeklyMenuResponse; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -78,5 +76,16 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { p.ean; """, nativeQuery = true) List<Object[]> findTop5RecipesWithProductsRaw(@Param("fridgeId") long fridgeId); + + @Query( value = """ + SELECT r.recipe_id, r.recipe_name,r.recipe_description, 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 + GROUP BY r.recipe_id, r.recipe_name + ORDER BY product_count DESC + LIMIT 5; + """, nativeQuery = true) + List<Object[]> findWeeklyMenu(@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 eb1101ef864b282cffe818c3972677a940aef1b4..870b355345307ddf7c590960c34793f9b6f4a9e9 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java @@ -21,18 +21,6 @@ public class WeeklyMenuService { @Autowired RecipeRepository recipeRepository; - /** - * Retrieves 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 groupId the ID of the fridge to use for matching products - * @return a list of Object arrays with recipe and product details - */ - /* - public List<Object[]> getTop5RecipesWithProducts(long groupId) { - return recipeRepository.findTop5RecipesWithProducts(groupId); - } - */ /** * Retrieves the top 5 recipes with products that have a match with items in the given fridge. @@ -41,21 +29,20 @@ public class WeeklyMenuService { * @param groupId the ID of the fridge to use for matching products * @return a list of RecipeWithProductsDTO objects with recipe and product details */ - public List<WeeklyMenuResponse> getTop5RecipesWithProducts(long groupId) { - - List<Object[]> rawData = recipeRepository.findTop5RecipesWithProductsRaw(groupId); + public List<WeeklyMenuResponse> getWeeklyMenu(long fridgeId) { + + List<Object[]> rawData = recipeRepository.findWeeklyMenu(fridgeId); List<WeeklyMenuResponse> result = rawData.stream() .map(row -> new WeeklyMenuResponse( (Integer) row[0], (String) row[1], - (Long) row[2], - (String) row[3], - (String) row[4], - (Boolean) row[5] + (String) row[2], + (long) row[3] )) .collect(Collectors.toList()); return result; } -} + +} \ No newline at end of file diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuControllerTest.java index a8f0d81b97a5c863f13d4902b33bf752ec21c6d3..d6840e065c56c52ff4a6f7ddb0a003ea11bdec48 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuControllerTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuControllerTest.java @@ -32,15 +32,25 @@ public class WeeklyMenuControllerTest { public void setUp() { weeklyMenu = new ArrayList<>(); // Add WeeklyMenuResponse objects to the weeklyMenu list - weeklyMenu.add(new WeeklyMenuResponse(1, "Recipe1", 1L, "Product1", "ProductDescription1", true)); - weeklyMenu.add(new WeeklyMenuResponse(2, "Recipe2", 2L, "Product2", "ProductDescription2", false)); + weeklyMenu.add(WeeklyMenuResponse.builder() + .recipeId(1) + .recipeName("Recipe1") + .recipeDescription("Description1") + .matchingProducts(10L) + .build()); + weeklyMenu.add(WeeklyMenuResponse.builder() + .recipeId(2) + .recipeName("Recipe2") + .recipeDescription("Description2") + .matchingProducts(15L) + .build()); } @Test public void getWeeklyMenu_found() { Long fridgeId = 1L; - when(weeklyMenuService.getTop5RecipesWithProducts(fridgeId)).thenReturn(weeklyMenu); + when(weeklyMenuService.getWeeklyMenu(fridgeId)).thenReturn(weeklyMenu); ResponseEntity<List<WeeklyMenuResponse>> response = weeklyMenuController.getWeeklyMenu(fridgeId); @@ -51,10 +61,10 @@ public class WeeklyMenuControllerTest { @Test public void getWeeklyMenu_notFound() { Long fridgeId = 1L; - when(weeklyMenuService.getTop5RecipesWithProducts(fridgeId)).thenReturn(new ArrayList<>()); + when(weeklyMenuService.getWeeklyMenu(fridgeId)).thenReturn(new ArrayList<>()); ResponseEntity<List<WeeklyMenuResponse>> response = weeklyMenuController.getWeeklyMenu(fridgeId); assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); } -} +} \ No newline at end of file diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/WeeklyMenuServiceTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/WeeklyMenuServiceTest.java index 2481f75ddd9ba3bcd6f599a52f3a637f2feb1538..fa55d70e4c254b67cf5c8d54c53d19ede1de401a 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/WeeklyMenuServiceTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/service/user/WeeklyMenuServiceTest.java @@ -30,23 +30,23 @@ class WeeklyMenuServiceTest { } @Test - void testGetTop5RecipesWithProducts() { - long groupId = 1L; + void testGetWeeklyMenu() { + long fridgeId = 1L; - Object[] row1 = new Object[] {1, "Recipe 1", 10L, "Product 1", "Description 1", true}; - Object[] row2 = new Object[] {2, "Recipe 2", 15L, "Product 2", "Description 2", false}; + Object[] row1 = new Object[] {1, "Recipe 1", "Description 1", 10L}; + Object[] row2 = new Object[] {2, "Recipe 2", "Description 2", 15L}; List<Object[]> rawData = Arrays.asList(row1, row2); - when(recipeRepository.findTop5RecipesWithProductsRaw(groupId)).thenReturn(rawData); + when(recipeRepository.findWeeklyMenu(fridgeId)).thenReturn(rawData); - List<WeeklyMenuResponse> result = weeklyMenuService.getTop5RecipesWithProducts(groupId); + List<WeeklyMenuResponse> result = weeklyMenuService.getWeeklyMenu(fridgeId); assertEquals(2, result.size()); assertEquals("Recipe 1", result.get(0).getRecipeName()); - assertEquals(10L, result.get(0).getEan()); - assertEquals("Product 1", result.get(0).getItemName()); + assertEquals("Description 1", result.get(0).getRecipeDescription()); + assertEquals(10L, result.get(0).getMatchingProducts()); assertEquals("Recipe 2", result.get(1).getRecipeName()); - assertEquals(15L, result.get(1).getEan()); - assertEquals("Product 2", result.get(1).getItemName()); + assertEquals("Description 2", result.get(1).getRecipeDescription()); + assertEquals(15L, result.get(1).getMatchingProducts()); } -} +} \ No newline at end of file