diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java index 68ad57d3dd5df281237ef86ba134574d4013250d..859760b29bd38e53479ad7ac2ef07dfd00f52956 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java @@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.controller; import org.springframework.web.bind.annotation.RestController; import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.dto.response.RecipeFridgeMatch; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import ntnu.idatt2016.v233.SmartMat.service.RecipeService; @@ -53,4 +54,17 @@ public class RecipeController { } return ResponseEntity.ok(recipes); } + + @GetMapping("/match/{fridgeId}/{recipeId}") + public ResponseEntity<List<RecipeFridgeMatch>> getRecipeWithByProductsInFridge( + @PathVariable("fridgeId") Long fridgeId, @PathVariable("recipeId") Long recipeId) { + + List<RecipeFridgeMatch> recipe = recipeService.getRecipeWithFridgeProductMatch(fridgeId, recipeId); + if (recipe.isEmpty()) { + return ResponseEntity.notFound().build(); + } else { + return ResponseEntity.ok(recipe); + } + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/RecipeFridgeMatch.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/RecipeFridgeMatch.java new file mode 100644 index 0000000000000000000000000000000000000000..586ce3c866874252193696f2ac321ea96ba214e6 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/response/RecipeFridgeMatch.java @@ -0,0 +1,26 @@ +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. + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor + +public class RecipeFridgeMatch { + + private Integer recipeId; + private String recipeName; + private String recipeDescription; + private long ean; + private String productName; + private String productDescription; + private boolean inFridge; + +} 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 41208c0bc9a4ef0c0f3e071f3f6638bb3a66de15..54d98c6c3398fa0ae906bb77e43177a1f3077f76 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -88,4 +88,15 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { """, nativeQuery = true) List<Object[]> findWeeklyMenu(@Param("fridgeId") long fridgeId); + @Query( value = """ + SELECT r.recipe_id, r.recipe_name, r.recipe_description, p.ean, p.item_name, p.description as product_description, + CASE WHEN fp.fridge_id IS NOT NULL THEN TRUE ELSE FALSE END as in_fridge + FROM recipe AS r + JOIN recipe_product AS rp ON r.recipe_id = rp.recipe_id + JOIN product AS p ON rp.ean = p.ean + LEFT JOIN fridge_product AS fp ON p.ean = fp.ean AND fp.fridge_id = :fridgeId + WHERE r.recipe_id = :recipeId ; + """ , nativeQuery = true) + List<Object[]> findRecipeWithMatchingProductsInFridge(long fridgeId, long recipeId); + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java index b96e20279fe8b69fb8950b968097900146c222de..fbf4f6eb5b86a79ff98e12ed5100452f13b1ab1b 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java @@ -1,11 +1,13 @@ package ntnu.idatt2016.v233.SmartMat.service; +import ntnu.idatt2016.v233.SmartMat.dto.response.RecipeFridgeMatch; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import ntnu.idatt2016.v233.SmartMat.entity.user.User; import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -102,4 +104,23 @@ public class RecipeService { recipeRepository.save(recipe); } + public List<RecipeFridgeMatch> getRecipeWithFridgeProductMatch(long fridgeId, long recipeId) { + + List<Object[]> rawData = recipeRepository.findRecipeWithMatchingProductsInFridge(fridgeId, recipeId); + + List<RecipeFridgeMatch> result = rawData.stream() + .map(row -> new RecipeFridgeMatch( + (Integer) row[0], + (String) row[1], + (String) row[2], + (Long) row[3], + (String) row[4], + (String) row[5], + (Boolean) row[6] + )) + .collect(Collectors.toList()); + + return result; + } + } \ No newline at end of file