Skip to content
Snippets Groups Projects
Commit 4692f53f authored by Stian Lyng Stræte's avatar Stian Lyng Stræte
Browse files

Merge branch 'feature/213-fix-weeklymenu' into 'main'

Feature/213 fix weeklymenu

See merge request idatt2106-v23-03/backend!156
parents 1af3ab55 5d9d235f
No related branches found
No related tags found
No related merge requests found
...@@ -18,10 +18,10 @@ import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService; ...@@ -18,10 +18,10 @@ import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService;
public class WeeklyMenuController { public class WeeklyMenuController {
private WeeklyMenuService weeklyMenuService; private WeeklyMenuService weeklyMenuService;
@GetMapping("/getWeeklyMenu/{fridgeId}") @GetMapping("/{fridgeId}")
public ResponseEntity<List<WeeklyMenuResponse>> getWeeklyMenu(@PathVariable("fridgeId") Long 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()) { if (weeklyMenu.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
......
...@@ -16,9 +16,7 @@ public class WeeklyMenuResponse { ...@@ -16,9 +16,7 @@ public class WeeklyMenuResponse {
private Integer recipeId; private Integer recipeId;
private String recipeName; private String recipeName;
private Long ean; private String recipeDescription;
private String itemName; private long matchingProducts;
private String itemDescription;
private boolean inFridge;
} }
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.repository; package ntnu.idatt2016.v233.SmartMat.repository;
import java.util.List; 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 ntnu.idatt2016.v233.SmartMat.entity.Recipe;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
...@@ -78,5 +76,16 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { ...@@ -78,5 +76,16 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> {
p.ean; p.ean;
""", nativeQuery = true) """, nativeQuery = true)
List<Object[]> findTop5RecipesWithProductsRaw(@Param("fridgeId") long fridgeId); 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);
} }
...@@ -21,18 +21,6 @@ public class WeeklyMenuService { ...@@ -21,18 +21,6 @@ public class WeeklyMenuService {
@Autowired @Autowired
RecipeRepository recipeRepository; 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. * Retrieves the top 5 recipes with products that have a match with items in the given fridge.
...@@ -41,21 +29,20 @@ public class WeeklyMenuService { ...@@ -41,21 +29,20 @@ public class WeeklyMenuService {
* @param groupId the ID of the fridge to use for matching products * @param groupId the ID of the fridge to use for matching products
* @return a list of RecipeWithProductsDTO objects with recipe and product details * @return a list of RecipeWithProductsDTO objects with recipe and product details
*/ */
public List<WeeklyMenuResponse> getTop5RecipesWithProducts(long groupId) { public List<WeeklyMenuResponse> getWeeklyMenu(long fridgeId) {
List<Object[]> rawData = recipeRepository.findTop5RecipesWithProductsRaw(groupId); List<Object[]> rawData = recipeRepository.findWeeklyMenu(fridgeId);
List<WeeklyMenuResponse> result = rawData.stream() List<WeeklyMenuResponse> result = rawData.stream()
.map(row -> new WeeklyMenuResponse( .map(row -> new WeeklyMenuResponse(
(Integer) row[0], (Integer) row[0],
(String) row[1], (String) row[1],
(Long) row[2], (String) row[2],
(String) row[3], (long) row[3]
(String) row[4],
(Boolean) row[5]
)) ))
.collect(Collectors.toList()); .collect(Collectors.toList());
return result; return result;
} }
}
}
\ No newline at end of file
...@@ -32,15 +32,25 @@ public class WeeklyMenuControllerTest { ...@@ -32,15 +32,25 @@ public class WeeklyMenuControllerTest {
public void setUp() { public void setUp() {
weeklyMenu = new ArrayList<>(); weeklyMenu = new ArrayList<>();
// Add WeeklyMenuResponse objects to the weeklyMenu list // Add WeeklyMenuResponse objects to the weeklyMenu list
weeklyMenu.add(new WeeklyMenuResponse(1, "Recipe1", 1L, "Product1", "ProductDescription1", true)); weeklyMenu.add(WeeklyMenuResponse.builder()
weeklyMenu.add(new WeeklyMenuResponse(2, "Recipe2", 2L, "Product2", "ProductDescription2", false)); .recipeId(1)
.recipeName("Recipe1")
.recipeDescription("Description1")
.matchingProducts(10L)
.build());
weeklyMenu.add(WeeklyMenuResponse.builder()
.recipeId(2)
.recipeName("Recipe2")
.recipeDescription("Description2")
.matchingProducts(15L)
.build());
} }
@Test @Test
public void getWeeklyMenu_found() { public void getWeeklyMenu_found() {
Long fridgeId = 1L; Long fridgeId = 1L;
when(weeklyMenuService.getTop5RecipesWithProducts(fridgeId)).thenReturn(weeklyMenu); when(weeklyMenuService.getWeeklyMenu(fridgeId)).thenReturn(weeklyMenu);
ResponseEntity<List<WeeklyMenuResponse>> response = weeklyMenuController.getWeeklyMenu(fridgeId); ResponseEntity<List<WeeklyMenuResponse>> response = weeklyMenuController.getWeeklyMenu(fridgeId);
...@@ -51,10 +61,10 @@ public class WeeklyMenuControllerTest { ...@@ -51,10 +61,10 @@ public class WeeklyMenuControllerTest {
@Test @Test
public void getWeeklyMenu_notFound() { public void getWeeklyMenu_notFound() {
Long fridgeId = 1L; Long fridgeId = 1L;
when(weeklyMenuService.getTop5RecipesWithProducts(fridgeId)).thenReturn(new ArrayList<>()); when(weeklyMenuService.getWeeklyMenu(fridgeId)).thenReturn(new ArrayList<>());
ResponseEntity<List<WeeklyMenuResponse>> response = weeklyMenuController.getWeeklyMenu(fridgeId); ResponseEntity<List<WeeklyMenuResponse>> response = weeklyMenuController.getWeeklyMenu(fridgeId);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
} }
} }
\ No newline at end of file
...@@ -30,23 +30,23 @@ class WeeklyMenuServiceTest { ...@@ -30,23 +30,23 @@ class WeeklyMenuServiceTest {
} }
@Test @Test
void testGetTop5RecipesWithProducts() { void testGetWeeklyMenu() {
long groupId = 1L; long fridgeId = 1L;
Object[] row1 = new Object[] {1, "Recipe 1", 10L, "Product 1", "Description 1", true}; Object[] row1 = new Object[] {1, "Recipe 1", "Description 1", 10L};
Object[] row2 = new Object[] {2, "Recipe 2", 15L, "Product 2", "Description 2", false}; Object[] row2 = new Object[] {2, "Recipe 2", "Description 2", 15L};
List<Object[]> rawData = Arrays.asList(row1, row2); 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(2, result.size());
assertEquals("Recipe 1", result.get(0).getRecipeName()); assertEquals("Recipe 1", result.get(0).getRecipeName());
assertEquals(10L, result.get(0).getEan()); assertEquals("Description 1", result.get(0).getRecipeDescription());
assertEquals("Product 1", result.get(0).getItemName()); assertEquals(10L, result.get(0).getMatchingProducts());
assertEquals("Recipe 2", result.get(1).getRecipeName()); assertEquals("Recipe 2", result.get(1).getRecipeName());
assertEquals(15L, result.get(1).getEan()); assertEquals("Description 2", result.get(1).getRecipeDescription());
assertEquals("Product 2", result.get(1).getItemName()); assertEquals(15L, result.get(1).getMatchingProducts());
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment