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

add response body

parent 37bc07bb
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.dto.response; ...@@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.dto.response;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* This class represents a response for a weekly menu. * This class represents a response for a weekly menu.
...@@ -10,12 +11,14 @@ import lombok.Data; ...@@ -10,12 +11,14 @@ import lombok.Data;
@Data @Data
@Builder @Builder
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor
public class WeeklyMenuResponse { public class WeeklyMenuResponse {
private long recipeId; private Integer recipeId;
private String recipeName; private String recipeName;
private long ean; private Long ean;
private String itemName; private String itemName;
private String itemDescription; private String itemDescription;
private boolean inFridge; 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;
...@@ -75,5 +77,6 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { ...@@ -75,5 +77,6 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> {
t.recipe_id, t.recipe_id,
p.ean; p.ean;
""", nativeQuery = true) """, nativeQuery = true)
List<Object[]> findTop5RecipesWithProducts(@Param("fridgeId") long fridgeId); List<Object[]> findTop5RecipesWithProductsRaw(@Param("fridgeId") long fridgeId);
} }
...@@ -42,11 +42,12 @@ public class WeeklyMenuService { ...@@ -42,11 +42,12 @@ public class WeeklyMenuService {
* @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> getTop5RecipesWithProducts(long groupId) {
List<Object[]> weeklyMenu = recipeRepository.findTop5RecipesWithProducts(groupId);
List<Object[]> rawData = recipeRepository.findTop5RecipesWithProductsRaw(groupId);
return weeklyMenu.stream()
List<WeeklyMenuResponse> result = rawData.stream()
.map(row -> new WeeklyMenuResponse( .map(row -> new WeeklyMenuResponse(
(Long) row[0], (Integer) row[0],
(String) row[1], (String) row[1],
(Long) row[2], (Long) row[2],
(String) row[3], (String) row[3],
...@@ -54,6 +55,7 @@ public class WeeklyMenuService { ...@@ -54,6 +55,7 @@ public class WeeklyMenuService {
(Boolean) row[5] (Boolean) row[5]
)) ))
.collect(Collectors.toList()); .collect(Collectors.toList());
}
return result;
}
} }
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