start
This commit is contained in:
parent
99aaa482cd
commit
32dc4d2e9b
@ -4,9 +4,7 @@ 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.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ingredients")
|
||||
@ -15,71 +13,33 @@ public class IngredientController {
|
||||
@Autowired
|
||||
private IngredientService ingredientService;
|
||||
|
||||
// 1. Globale Zutaten abrufen
|
||||
// Globale Zutaten abrufen
|
||||
@GetMapping("/global")
|
||||
public ResponseEntity<List<Ingredient>> getGlobalIngredients() {
|
||||
List<Ingredient> ingredients = ingredientService.getGlobalIngredients();
|
||||
return ResponseEntity.ok(ingredients);
|
||||
public List<Ingredient> getGlobalIngredients() {
|
||||
return ingredientService.getGlobalIngredients();
|
||||
}
|
||||
|
||||
// 2. Benutzerdefinierte Zutaten abrufen
|
||||
// Benutzerspezifische Zutaten abrufen
|
||||
@GetMapping("/user/{userId}")
|
||||
public ResponseEntity<List<Ingredient>> getUserIngredients(@PathVariable Long userId) {
|
||||
List<Ingredient> ingredients = ingredientService.getUserIngredients(userId);
|
||||
return ResponseEntity.ok(ingredients);
|
||||
public List<Ingredient> getUserIngredients(@PathVariable Long userId) {
|
||||
return ingredientService.getUserIngredients(userId);
|
||||
}
|
||||
|
||||
// 3. Neue globale Zutat hinzufügen
|
||||
// Neue globale Zutat hinzufügen
|
||||
@PostMapping("/global")
|
||||
public ResponseEntity<Ingredient> addGlobalIngredient(@RequestBody Ingredient ingredient) {
|
||||
Ingredient newIngredient = ingredientService.addGlobalIngredient(ingredient);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(newIngredient);
|
||||
public Ingredient addGlobalIngredient(@RequestBody Ingredient ingredient) {
|
||||
return ingredientService.addGlobalIngredient(ingredient);
|
||||
}
|
||||
|
||||
// 4. Benutzerdefinierte Zutat hinzufügen
|
||||
// Neue benutzerspezifische Zutat hinzufügen
|
||||
@PostMapping("/user/{userId}")
|
||||
public ResponseEntity<Ingredient> addUserIngredient(
|
||||
@PathVariable Long userId,
|
||||
@RequestBody Ingredient ingredient
|
||||
) {
|
||||
public ResponseEntity<Ingredient> addUserIngredient(@PathVariable Long userId, @RequestBody Ingredient ingredient) {
|
||||
try {
|
||||
Ingredient newIngredient = ingredientService.addUserIngredient(userId, ingredient);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(newIngredient);
|
||||
}
|
||||
|
||||
// 5. Einzelne Zutat abrufen
|
||||
@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();
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Falls die Zutat bereits global existiert
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,6 @@ public interface IngredientRepository extends JpaRepository<Ingredient, Long> {
|
||||
List<Ingredient> findByNameAndUserId(String name, Long userId);
|
||||
|
||||
// 5. Überprüfen, ob eine Zutat global existiert
|
||||
boolean existsByNameAndIsGlobal(String name);
|
||||
boolean existsByNameAndIsGlobal(String name, boolean isGlobal);
|
||||
}
|
||||
|
||||
|
@ -2,9 +2,7 @@ package com.example.hangry;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class IngredientService {
|
||||
@ -15,12 +13,12 @@ public class IngredientService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
// 1. Globale Zutaten abrufen
|
||||
// Globale Zutaten abrufen
|
||||
public List<Ingredient> getGlobalIngredients() {
|
||||
return ingredientRepository.findByIsGlobal(true);
|
||||
}
|
||||
|
||||
// 2. Benutzerdefinierte Zutaten abrufen
|
||||
// Benutzerspezifische Zutaten abrufen
|
||||
public List<Ingredient> getUserIngredients(Long userId) {
|
||||
Optional<User> user = userRepository.findById(userId);
|
||||
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) {
|
||||
ingredient.setGlobal(true); // Markiere die Zutat als global
|
||||
ingredient.setGlobal(true); // Zutat als global markieren
|
||||
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) {
|
||||
// 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);
|
||||
if (user.isPresent()) {
|
||||
ingredient.setGlobal(false); // Markiere die Zutat als benutzerspezifisch
|
||||
ingredient.setUser(user.get());
|
||||
ingredient.setGlobal(false); // Zutat als benutzerspezifisch markieren
|
||||
ingredient.setUser(user.get()); // Zutat dem Benutzer zuweisen
|
||||
return ingredientRepository.save(ingredient);
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user