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

created recipe product fridge match

parent 5d9d235f
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.controller; ...@@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.controller;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.response.RecipeFridgeMatch;
import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import ntnu.idatt2016.v233.SmartMat.entity.Recipe;
import ntnu.idatt2016.v233.SmartMat.service.RecipeService; import ntnu.idatt2016.v233.SmartMat.service.RecipeService;
...@@ -53,4 +54,17 @@ public class RecipeController { ...@@ -53,4 +54,17 @@ public class RecipeController {
} }
return ResponseEntity.ok(recipes); 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);
}
}
} }
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;
}
...@@ -88,4 +88,15 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> { ...@@ -88,4 +88,15 @@ public interface RecipeRepository extends JpaRepository<Recipe, Long> {
""", nativeQuery = true) """, nativeQuery = true)
List<Object[]> findWeeklyMenu(@Param("fridgeId") long fridgeId); 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);
} }
package ntnu.idatt2016.v233.SmartMat.service; 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.Recipe;
import ntnu.idatt2016.v233.SmartMat.entity.user.User; import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository; import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -102,4 +104,23 @@ public class RecipeService { ...@@ -102,4 +104,23 @@ public class RecipeService {
recipeRepository.save(recipe); 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
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