filter
This commit is contained in:
parent
32dc4d2e9b
commit
92f854848d
@ -1,11 +1,15 @@
|
|||||||
package com.example.hangry;
|
package com.example.hangry;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import java.util.List;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/recipes")
|
@RequestMapping("/api/recipes")
|
||||||
|
@CrossOrigin(origins = "*") // Erlaubt CORS für alle Domains, kann angepasst werden
|
||||||
public class RecipeController {
|
public class RecipeController {
|
||||||
|
|
||||||
private final RecipeService recipeService;
|
private final RecipeService recipeService;
|
||||||
@ -14,13 +18,51 @@ public class RecipeController {
|
|||||||
this.recipeService = recipeService;
|
this.recipeService = recipeService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alle Rezepte mit Pagination abrufen
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Recipe> getAllRecipes() {
|
public Page<Recipe> getAllRecipes(Pageable pageable) {
|
||||||
return recipeService.getAllRecipes();
|
return recipeService.getAllRecipes(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Einzelnes Rezept nach ID abrufen
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<Recipe> getRecipeById(@PathVariable Long id) {
|
||||||
|
Optional<Recipe> recipe = recipeService.getRecipeById(id);
|
||||||
|
return recipe.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Neues Rezept erstellen
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Recipe createRecipe(@RequestBody Recipe recipe) {
|
public ResponseEntity<Recipe> createRecipe(@RequestBody Recipe recipe) {
|
||||||
return recipeService.createRecipe(recipe);
|
Recipe savedRecipe = recipeService.createRecipe(recipe);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(savedRecipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rezept aktualisieren
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ResponseEntity<Recipe> updateRecipe(@PathVariable Long id, @RequestBody Recipe recipe) {
|
||||||
|
Optional<Recipe> updatedRecipe = recipeService.updateRecipe(id, recipe);
|
||||||
|
return updatedRecipe.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rezept löschen
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> 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<Recipe> 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<Recipe> getRecipesByIngredient(@RequestParam String ingredient, Pageable pageable) {
|
||||||
|
return recipeService.getRecipesByIngredient(ingredient, pageable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
package com.example.hangry;
|
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.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
public interface RecipeRepository extends JpaRepository<Recipe, Long> {
|
public interface RecipeRepository extends JpaRepository<Recipe, Long> {
|
||||||
|
// Filtert Rezepte nach Kategorie (z. B. vegan, vegetarisch, einfach)
|
||||||
|
Page<Recipe> 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<Recipe> findByIngredient(@Param("ingredient") String ingredient, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package com.example.hangry;
|
package com.example.hangry;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class RecipeService {
|
public class RecipeService {
|
||||||
@ -13,11 +15,50 @@ public class RecipeService {
|
|||||||
this.recipeRepository = recipeRepository;
|
this.recipeRepository = recipeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Recipe> getAllRecipes() {
|
// Alle Rezepte mit Pagination abrufen
|
||||||
return recipeRepository.findAll();
|
public Page<Recipe> getAllRecipes(Pageable pageable) {
|
||||||
|
return recipeRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Einzelnes Rezept nach ID abrufen
|
||||||
|
public Optional<Recipe> getRecipeById(Long id) {
|
||||||
|
return recipeRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Neues Rezept erstellen
|
||||||
public Recipe createRecipe(Recipe recipe) {
|
public Recipe createRecipe(Recipe recipe) {
|
||||||
return recipeRepository.save(recipe);
|
return recipeRepository.save(recipe);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rezept aktualisieren
|
||||||
|
public Optional<Recipe> 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<Recipe> getRecipesByCategory(String category, Pageable pageable) {
|
||||||
|
return recipeRepository.findByCategoryIgnoreCase(category, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Filter nach Zutat
|
||||||
|
public Page<Recipe> getRecipesByIngredient(String ingredient, Pageable pageable) {
|
||||||
|
return recipeRepository.findByIngredient(ingredient, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user