diff --git a/src/main/java/com/example/hangry/RecipeController.java b/src/main/java/com/example/hangry/RecipeController.java index 474aba7..2c34d30 100644 --- a/src/main/java/com/example/hangry/RecipeController.java +++ b/src/main/java/com/example/hangry/RecipeController.java @@ -1,11 +1,15 @@ package com.example.hangry; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - -import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import java.util.Optional; @RestController @RequestMapping("/api/recipes") +@CrossOrigin(origins = "*") // Erlaubt CORS für alle Domains, kann angepasst werden public class RecipeController { private final RecipeService recipeService; @@ -14,13 +18,51 @@ public class RecipeController { this.recipeService = recipeService; } + // Alle Rezepte mit Pagination abrufen @GetMapping - public List getAllRecipes() { - return recipeService.getAllRecipes(); + public Page getAllRecipes(Pageable pageable) { + return recipeService.getAllRecipes(pageable); } + // Einzelnes Rezept nach ID abrufen + @GetMapping("/{id}") + public ResponseEntity getRecipeById(@PathVariable Long id) { + Optional recipe = recipeService.getRecipeById(id); + return recipe.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + } + + // Neues Rezept erstellen @PostMapping - public Recipe createRecipe(@RequestBody Recipe recipe) { - return recipeService.createRecipe(recipe); + public ResponseEntity createRecipe(@RequestBody Recipe recipe) { + Recipe savedRecipe = recipeService.createRecipe(recipe); + return ResponseEntity.status(HttpStatus.CREATED).body(savedRecipe); + } + + // Rezept aktualisieren + @PutMapping("/{id}") + public ResponseEntity updateRecipe(@PathVariable Long id, @RequestBody Recipe recipe) { + Optional updatedRecipe = recipeService.updateRecipe(id, recipe); + return updatedRecipe.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + } + + // Rezept löschen + @DeleteMapping("/{id}") + public ResponseEntity deleteRecipe(@PathVariable Long id) { + if (recipeService.deleteRecipe(id)) { + return ResponseEntity.noContent().build(); + } else { + return ResponseEntity.notFound().build(); + } + } + //Endpunkt: Filter nach Kategorie (z. B. "/api/recipes/filter/category?category=vegan") + @GetMapping("/filter/category") + public Page getRecipesByCategory(@RequestParam String category, Pageable pageable) { + return recipeService.getRecipesByCategory(category, pageable); + } + + // Endpunkt: Filter nach Zutat (z. B. "/api/recipes/filter/ingredient?ingredient=tomate") + @GetMapping("/filter/ingredient") + public Page getRecipesByIngredient(@RequestParam String ingredient, Pageable pageable) { + return recipeService.getRecipesByIngredient(ingredient, pageable); } } diff --git a/src/main/java/com/example/hangry/RecipeRepository.java b/src/main/java/com/example/hangry/RecipeRepository.java index e4104da..35967b3 100644 --- a/src/main/java/com/example/hangry/RecipeRepository.java +++ b/src/main/java/com/example/hangry/RecipeRepository.java @@ -1,6 +1,16 @@ package com.example.hangry; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface RecipeRepository extends JpaRepository { + // Filtert Rezepte nach Kategorie (z. B. vegan, vegetarisch, einfach) + Page findByCategoryIgnoreCase(String category, Pageable pageable); + + // Sucht nach Rezepten, die eine bestimmte Zutat enthalten + @Query("SELECT r FROM Recipe r JOIN r.recipeIngredients i WHERE LOWER(i.name) LIKE LOWER(CONCAT('%', :ingredient, '%'))") + Page findByIngredient(@Param("ingredient") String ingredient, Pageable pageable); } diff --git a/src/main/java/com/example/hangry/RecipeService.java b/src/main/java/com/example/hangry/RecipeService.java index 58bc129..e8b2250 100644 --- a/src/main/java/com/example/hangry/RecipeService.java +++ b/src/main/java/com/example/hangry/RecipeService.java @@ -1,8 +1,10 @@ package com.example.hangry; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; -import java.util.List; +import java.util.Optional; @Service public class RecipeService { @@ -13,11 +15,50 @@ public class RecipeService { this.recipeRepository = recipeRepository; } - public List getAllRecipes() { - return recipeRepository.findAll(); + // Alle Rezepte mit Pagination abrufen + public Page getAllRecipes(Pageable pageable) { + return recipeRepository.findAll(pageable); } + // Einzelnes Rezept nach ID abrufen + public Optional getRecipeById(Long id) { + return recipeRepository.findById(id); + } + + // Neues Rezept erstellen public Recipe createRecipe(Recipe recipe) { return recipeRepository.save(recipe); } + + // Rezept aktualisieren + public Optional updateRecipe(Long id, Recipe recipeDetails) { + return recipeRepository.findById(id).map(recipe -> { + if (recipeDetails.getName() != null) recipe.setName(recipeDetails.getName()); + if (recipeDetails.getDescription() != null) recipe.setDescription(recipeDetails.getDescription()); + if (recipeDetails.getCategory() != null) recipe.setCategory(recipeDetails.getCategory()); + if (recipeDetails.getImageUrl() != null) recipe.setImageUrl(recipeDetails.getImageUrl()); + return recipeRepository.save(recipe); + }); + } + + + // Rezept löschen + public boolean deleteRecipe(Long id) { + if (recipeRepository.existsById(id)) { + recipeRepository.deleteById(id); + return true; + } + return false; + } + + //Filter nach Kategorie + public Page getRecipesByCategory(String category, Pageable pageable) { + return recipeRepository.findByCategoryIgnoreCase(category, pageable); + } + + //Filter nach Zutat + public Page getRecipesByIngredient(String ingredient, Pageable pageable) { + return recipeRepository.findByIngredient(ingredient, pageable); + } + }