From f9ceed63749af21ec4b99d1e51be6fd4801fcaba Mon Sep 17 00:00:00 2001 From: Stian Lyng <stianlyng@protonmail.com> Date: Tue, 25 Apr 2023 11:28:36 +0200 Subject: [PATCH] add response body --- .../SmartMat/dto/response/WeeklyMenuResponse.java | 9 ++++++--- .../v233/SmartMat/repository/RecipeRepository.java | 5 ++++- .../v233/SmartMat/service/WeeklyMenuService.java | 14 ++++++++------ 3 files changed, 18 insertions(+), 10 deletions(-) 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 1d5fba1f..76cd0abf 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 @@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.dto.response; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; /** * This class represents a response for a weekly menu. @@ -10,12 +11,14 @@ import lombok.Data; @Data @Builder @AllArgsConstructor +@NoArgsConstructor public class WeeklyMenuResponse { - private long recipeId; + private Integer recipeId; private String recipeName; - private long ean; + private Long ean; private String itemName; private String itemDescription; private boolean inFridge; -} + +} \ 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 592dabea..cbaf7504 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -1,7 +1,9 @@ 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; @@ -75,5 +77,6 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { t.recipe_id, p.ean; """, nativeQuery = true) - List<Object[]> findTop5RecipesWithProducts(@Param("fridgeId") long fridgeId); + List<Object[]> findTop5RecipesWithProductsRaw(@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 310116e4..eb1101ef 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/WeeklyMenuService.java @@ -42,11 +42,12 @@ public class WeeklyMenuService { * @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() + + List<Object[]> rawData = recipeRepository.findTop5RecipesWithProductsRaw(groupId); + + List<WeeklyMenuResponse> result = rawData.stream() .map(row -> new WeeklyMenuResponse( - (Long) row[0], + (Integer) row[0], (String) row[1], (Long) row[2], (String) row[3], @@ -54,6 +55,7 @@ public class WeeklyMenuService { (Boolean) row[5] )) .collect(Collectors.toList()); - } - + + return result; + } } -- GitLab