Skip to content
Snippets Groups Projects
Commit ec0f1dab authored by Stian Lyng's avatar Stian Lyng
Browse files

create weekly Menu suggestions

parent 45cf7018
No related branches found
No related tags found
No related merge requests found
......@@ -19,9 +19,9 @@ 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();
......
......@@ -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
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;
......@@ -79,4 +77,15 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> {
""", 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);
}
......@@ -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) {
public List<WeeklyMenuResponse> getWeeklyMenu(long fridgeId) {
List<Object[]> rawData = recipeRepository.findTop5RecipesWithProductsRaw(groupId);
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment