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

add dto

parent 9375c8c7
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......
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;
}
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());
}
}
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