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 a5b08fcf29e972ad5b835ee20b8d4bce31029483..54d341ba926db90192a38c876d21bb9697157565 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/WeeklyMenuController.java @@ -9,6 +9,7 @@ import java.util.List; import org.springframework.http.ResponseEntity; import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.dto.response.WeeklyMenuResponse; import ntnu.idatt2016.v233.SmartMat.service.WeeklyMenuService; @AllArgsConstructor @@ -19,8 +20,8 @@ public class WeeklyMenuController { private WeeklyMenuService weeklyMenuService; @GetMapping("/getWeeklyMenu/{fridgeId}") - public ResponseEntity<List<Object[]>> getWeeklyMenu(@PathVariable("fridgeId") Long fridgeId) { - List<Object[]> weeklyMenu = weeklyMenuService.getTop5RecipesWithProducts(fridgeId); + public ResponseEntity<List<WeeklyMenuResponse>> getWeeklyMenu(@PathVariable("fridgeId") Long fridgeId) { + List<WeeklyMenuResponse> weeklyMenu = weeklyMenuService.getTop5RecipesWithProducts(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 new file mode 100644 index 0000000000000000000000000000000000000000..1d5fba1f1ffc6494d6cb19f08f12713f8747fdee --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/WeeklyMenuResponse.java @@ -0,0 +1,21 @@ +package ntnu.idatt2016.v233.SmartMat.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +/** + * This class represents a response for a weekly menu. + */ +@Data +@Builder +@AllArgsConstructor +public class WeeklyMenuResponse { + + private long recipeId; + private String recipeName; + private long ean; + private String itemName; + private String itemDescription; + private boolean inFridge; +} 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 daf9dd93586229fb14b3b9c331ef63ef6b5f115e..310116e4b91c06682c27b5100275150921fd2ef4 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java @@ -1,10 +1,12 @@ package ntnu.idatt2016.v233.SmartMat.service; import java.util.List; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import ntnu.idatt2016.v233.SmartMat.dto.response.WeeklyMenuResponse; import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository; /** @@ -26,8 +28,32 @@ public class WeeklyMenuService { * @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. + * Returns a list of RecipeWithProductsDTO objects with recipe details and product information. + * + * @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[]> weeklyMenu = recipeRepository.findTop5RecipesWithProducts(groupId); + + return weeklyMenu.stream() + .map(row -> new WeeklyMenuResponse( + (Long) row[0], + (String) row[1], + (Long) row[2], + (String) row[3], + (String) row[4], + (Boolean) row[5] + )) + .collect(Collectors.toList()); + } }