Hangry/src/main/java/com/example/hangry/IngredientController.java
2025-01-28 14:55:01 +01:00

46 lines
1.6 KiB
Java

package com.example.hangry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/api/ingredients")
public class IngredientController {
@Autowired
private IngredientService ingredientService;
// Globale Zutaten abrufen
@GetMapping("/global")
public List<Ingredient> getGlobalIngredients() {
return ingredientService.getGlobalIngredients();
}
// Benutzerspezifische Zutaten abrufen
@GetMapping("/user/{userId}")
public List<Ingredient> getUserIngredients(@PathVariable Long userId) {
return ingredientService.getUserIngredients(userId);
}
// Neue globale Zutat hinzufügen
@PostMapping("/global")
public Ingredient addGlobalIngredient(@RequestBody Ingredient ingredient) {
return ingredientService.addGlobalIngredient(ingredient);
}
// Neue benutzerspezifische Zutat hinzufügen
@PostMapping("/user/{userId}")
public ResponseEntity<Ingredient> addUserIngredient(@PathVariable Long userId, @RequestBody Ingredient ingredient) {
try {
Ingredient newIngredient = ingredientService.addUserIngredient(userId, ingredient);
return ResponseEntity.status(HttpStatus.CREATED).body(newIngredient);
} catch (IllegalArgumentException e) {
// Falls die Zutat bereits global existiert
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}
}