This commit is contained in:
PaulK 2025-01-28 14:55:01 +01:00
parent 99aaa482cd
commit 32dc4d2e9b
3 changed files with 34 additions and 100 deletions

View File

@ -4,9 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.List;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/api/ingredients") @RequestMapping("/api/ingredients")
@ -15,71 +13,33 @@ public class IngredientController {
@Autowired @Autowired
private IngredientService ingredientService; private IngredientService ingredientService;
// 1. Globale Zutaten abrufen // Globale Zutaten abrufen
@GetMapping("/global") @GetMapping("/global")
public ResponseEntity<List<Ingredient>> getGlobalIngredients() { public List<Ingredient> getGlobalIngredients() {
List<Ingredient> ingredients = ingredientService.getGlobalIngredients(); return ingredientService.getGlobalIngredients();
return ResponseEntity.ok(ingredients);
} }
// 2. Benutzerdefinierte Zutaten abrufen // Benutzerspezifische Zutaten abrufen
@GetMapping("/user/{userId}") @GetMapping("/user/{userId}")
public ResponseEntity<List<Ingredient>> getUserIngredients(@PathVariable Long userId) { public List<Ingredient> getUserIngredients(@PathVariable Long userId) {
List<Ingredient> ingredients = ingredientService.getUserIngredients(userId); return ingredientService.getUserIngredients(userId);
return ResponseEntity.ok(ingredients);
} }
// 3. Neue globale Zutat hinzufügen // Neue globale Zutat hinzufügen
@PostMapping("/global") @PostMapping("/global")
public ResponseEntity<Ingredient> addGlobalIngredient(@RequestBody Ingredient ingredient) { public Ingredient addGlobalIngredient(@RequestBody Ingredient ingredient) {
Ingredient newIngredient = ingredientService.addGlobalIngredient(ingredient); return ingredientService.addGlobalIngredient(ingredient);
return ResponseEntity.status(HttpStatus.CREATED).body(newIngredient);
} }
// 4. Benutzerdefinierte Zutat hinzufügen // Neue benutzerspezifische Zutat hinzufügen
@PostMapping("/user/{userId}") @PostMapping("/user/{userId}")
public ResponseEntity<Ingredient> addUserIngredient( public ResponseEntity<Ingredient> addUserIngredient(@PathVariable Long userId, @RequestBody Ingredient ingredient) {
@PathVariable Long userId, try {
@RequestBody Ingredient ingredient
) {
Ingredient newIngredient = ingredientService.addUserIngredient(userId, ingredient); Ingredient newIngredient = ingredientService.addUserIngredient(userId, ingredient);
return ResponseEntity.status(HttpStatus.CREATED).body(newIngredient); return ResponseEntity.status(HttpStatus.CREATED).body(newIngredient);
} } catch (IllegalArgumentException e) {
// Falls die Zutat bereits global existiert
// 5. Einzelne Zutat abrufen return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
@GetMapping("/{id}")
public ResponseEntity<Ingredient> getIngredientById(@PathVariable Long id) {
Optional<Ingredient> ingredient = ingredientService.getIngredientById(id);
if (ingredient.isPresent()) {
return ResponseEntity.ok(ingredient.get());
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
// 6. Zutat aktualisieren
@PutMapping("/{id}")
public ResponseEntity<Ingredient> updateIngredient(
@PathVariable Long id,
@RequestBody Ingredient updatedIngredient
) {
Optional<Ingredient> ingredient = ingredientService.updateIngredient(id, updatedIngredient);
if (ingredient.isPresent()) {
return ResponseEntity.ok(ingredient.get());
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
// 7. Zutat löschen
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteIngredient(@PathVariable Long id) {
boolean deleted = ingredientService.deleteIngredient(id);
if (deleted) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} }
} }
} }

View File

@ -21,6 +21,6 @@ public interface IngredientRepository extends JpaRepository<Ingredient, Long> {
List<Ingredient> findByNameAndUserId(String name, Long userId); List<Ingredient> findByNameAndUserId(String name, Long userId);
// 5. Überprüfen, ob eine Zutat global existiert // 5. Überprüfen, ob eine Zutat global existiert
boolean existsByNameAndIsGlobal(String name); boolean existsByNameAndIsGlobal(String name, boolean isGlobal);
} }

View File

@ -2,9 +2,7 @@ package com.example.hangry;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*;
import java.util.List;
import java.util.Optional;
@Service @Service
public class IngredientService { public class IngredientService {
@ -15,12 +13,12 @@ public class IngredientService {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
// 1. Globale Zutaten abrufen // Globale Zutaten abrufen
public List<Ingredient> getGlobalIngredients() { public List<Ingredient> getGlobalIngredients() {
return ingredientRepository.findByIsGlobal(true); return ingredientRepository.findByIsGlobal(true);
} }
// 2. Benutzerdefinierte Zutaten abrufen // Benutzerspezifische Zutaten abrufen
public List<Ingredient> getUserIngredients(Long userId) { public List<Ingredient> getUserIngredients(Long userId) {
Optional<User> user = userRepository.findById(userId); Optional<User> user = userRepository.findById(userId);
if (user.isPresent()) { if (user.isPresent()) {
@ -30,52 +28,28 @@ public class IngredientService {
} }
} }
// 3. Neue globale Zutat hinzufügen // Globale Zutat hinzufügen
public Ingredient addGlobalIngredient(Ingredient ingredient) { public Ingredient addGlobalIngredient(Ingredient ingredient) {
ingredient.setGlobal(true); // Markiere die Zutat als global ingredient.setGlobal(true); // Zutat als global markieren
return ingredientRepository.save(ingredient); return ingredientRepository.save(ingredient);
} }
// 4. Benutzerdefinierte Zutat hinzufügen // Benutzerdefinierte Zutat hinzufügen, nur wenn sie nicht global existiert
public Ingredient addUserIngredient(Long userId, Ingredient ingredient) { public Ingredient addUserIngredient(Long userId, Ingredient ingredient) {
// Zuerst prüfen, ob diese Zutat bereits global existiert
boolean isGlobalIngredientExists = ingredientRepository.existsByNameAndIsGlobal(ingredient.getName(), true);
if (isGlobalIngredientExists) {
throw new IllegalArgumentException("Zutat existiert bereits als globale Zutat.");
}
Optional<User> user = userRepository.findById(userId); Optional<User> user = userRepository.findById(userId);
if (user.isPresent()) { if (user.isPresent()) {
ingredient.setGlobal(false); // Markiere die Zutat als benutzerspezifisch ingredient.setGlobal(false); // Zutat als benutzerspezifisch markieren
ingredient.setUser(user.get()); ingredient.setUser(user.get()); // Zutat dem Benutzer zuweisen
return ingredientRepository.save(ingredient); return ingredientRepository.save(ingredient);
} else { } else {
throw new IllegalArgumentException("User not found with ID: " + userId); throw new IllegalArgumentException("User not found with ID: " + userId);
} }
} }
// 5. Einzelne Zutat abrufen
public Optional<Ingredient> getIngredientById(Long id) {
return ingredientRepository.findById(id);
} }
// 6. Zutat aktualisieren
public Optional<Ingredient> updateIngredient(Long id, Ingredient updatedIngredient) {
Optional<Ingredient> ingredient = ingredientRepository.findById(id);
if (ingredient.isPresent()) {
Ingredient existingIngredient = ingredient.get();
existingIngredient.setName(updatedIngredient.getName());
existingIngredient.setUnit(updatedIngredient.getUnit());
return Optional.of(ingredientRepository.save(existingIngredient));
} else {
return Optional.empty();
}
}
// 7. Zutat löschen
public boolean deleteIngredient(Long id) {
Optional<Ingredient> ingredient = ingredientRepository.findById(id);
if (ingredient.isPresent()) {
ingredientRepository.deleteById(id);
return true;
} else {
return false;
}
}
}