Compare commits
No commits in common. "32dc4d2e9bf3164224a378d2e6077e62f0c94f97" and "cc3d351bf2455253cad1c0d00cd153f3f73a84ac" have entirely different histories.
32dc4d2e9b
...
cc3d351bf2
@ -1,6 +1,9 @@
|
|||||||
package com.example.hangry;
|
package com.example.hangry;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Ingredient {
|
public class Ingredient {
|
||||||
@ -11,18 +14,11 @@ public class Ingredient {
|
|||||||
|
|
||||||
private String name; // Name der Zutat
|
private String name; // Name der Zutat
|
||||||
|
|
||||||
private boolean isGlobal; // Flag, ob die Zutat global ist (true) oder benutzerspezifisch (false)
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name = "user_id") // Verknüpft mit der 'User'-Tabelle
|
|
||||||
private User user; // Hinzufügen einer 'user'-Beziehung
|
|
||||||
|
|
||||||
public Ingredient() {
|
public Ingredient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ingredient(String name, boolean isGlobal) {
|
public Ingredient(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.isGlobal = isGlobal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getter und Setter
|
// Getter und Setter
|
||||||
@ -41,20 +37,4 @@ public class Ingredient {
|
|||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGlobal() {
|
|
||||||
return isGlobal;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setGlobal(boolean global) {
|
|
||||||
isGlobal = global;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User getUser() {
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUser(User user) {
|
|
||||||
this.user = user;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
package com.example.hangry;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface IngredientRepository extends JpaRepository<Ingredient, Long> {
|
|
||||||
|
|
||||||
// 1. Alle globalen Zutaten abrufen
|
|
||||||
List<Ingredient> findByIsGlobal(boolean isGlobal);
|
|
||||||
|
|
||||||
// 2. Zutaten, die einem bestimmten Benutzer gehören, abrufen
|
|
||||||
List<Ingredient> findByUserId(Long userId);
|
|
||||||
|
|
||||||
// 3. Zutaten suchen, die global und/oder benutzerdefiniert nach Name passen
|
|
||||||
List<Ingredient> findByNameContainingIgnoreCase(String name);
|
|
||||||
|
|
||||||
// 4. Benutzerdefinierte Zutat für einen bestimmten Benutzer nach Namen abrufen
|
|
||||||
List<Ingredient> findByNameAndUserId(String name, Long userId);
|
|
||||||
|
|
||||||
// 5. Überprüfen, ob eine Zutat global existiert
|
|
||||||
boolean existsByNameAndIsGlobal(String name, boolean isGlobal);
|
|
||||||
}
|
|
||||||
|
|
@ -1,55 +0,0 @@
|
|||||||
package com.example.hangry;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class IngredientService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private IngredientRepository ingredientRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
// Globale Zutaten abrufen
|
|
||||||
public List<Ingredient> getGlobalIngredients() {
|
|
||||||
return ingredientRepository.findByIsGlobal(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Benutzerspezifische Zutaten abrufen
|
|
||||||
public List<Ingredient> getUserIngredients(Long userId) {
|
|
||||||
Optional<User> user = userRepository.findById(userId);
|
|
||||||
if (user.isPresent()) {
|
|
||||||
return ingredientRepository.findByUserId(userId);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("User not found with ID: " + userId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Globale Zutat hinzufügen
|
|
||||||
public Ingredient addGlobalIngredient(Ingredient ingredient) {
|
|
||||||
ingredient.setGlobal(true); // Zutat als global markieren
|
|
||||||
return ingredientRepository.save(ingredient);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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); // 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,105 +0,0 @@
|
|||||||
package com.example.hangry;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(unique = true, nullable = false)
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private String password; // Hier solltest du sicherstellen, dass das Passwort verschlüsselt wird
|
|
||||||
|
|
||||||
@Column(unique = true, nullable = false)
|
|
||||||
private String email;
|
|
||||||
|
|
||||||
private String role; // Für Rollen wie "ADMIN", "USER", etc.
|
|
||||||
|
|
||||||
// Many-to-Many Beziehung zu Ingredients
|
|
||||||
@ManyToMany
|
|
||||||
@JoinTable(
|
|
||||||
name = "user_ingredients",
|
|
||||||
joinColumns = @JoinColumn(name = "user_id"),
|
|
||||||
inverseJoinColumns = @JoinColumn(name = "ingredient_id")
|
|
||||||
)
|
|
||||||
private List<Ingredient> ingredients = new ArrayList<>();
|
|
||||||
|
|
||||||
// No-Args-Konstruktor (wichtig für JPA)
|
|
||||||
public User() {
|
|
||||||
}
|
|
||||||
|
|
||||||
// All-Args-Konstruktor
|
|
||||||
public User(Long id, String username, String password, String email, String role) {
|
|
||||||
this.id = id;
|
|
||||||
this.username = username;
|
|
||||||
this.password = password;
|
|
||||||
this.email = email;
|
|
||||||
this.role = role;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getter und Setter
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
|
||||||
return username;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUsername(String username) {
|
|
||||||
this.username = username;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPassword() {
|
|
||||||
return password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPassword(String password) {
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEmail() {
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmail(String email) {
|
|
||||||
this.email = email;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getRole() {
|
|
||||||
return role;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRole(String role) {
|
|
||||||
this.role = role;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Ingredient> getIngredients() {
|
|
||||||
return ingredients;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIngredients(List<Ingredient> ingredients) {
|
|
||||||
this.ingredients = ingredients;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methode, um eine Zutat zu einem User hinzuzufügen
|
|
||||||
public void addIngredient(Ingredient ingredient) {
|
|
||||||
this.ingredients.add(ingredient);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Methode, um eine Zutat aus der Liste zu entfernen
|
|
||||||
public void removeIngredient(Ingredient ingredient) {
|
|
||||||
this.ingredients.remove(ingredient);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package com.example.hangry;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/users")
|
|
||||||
public class UserController {
|
|
||||||
|
|
||||||
private final UserService userService;
|
|
||||||
|
|
||||||
public UserController(UserService userService) {
|
|
||||||
this.userService = userService;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Endpunkt: Benutzer erstellen
|
|
||||||
@PostMapping
|
|
||||||
public User createUser(@RequestBody User user) {
|
|
||||||
return userService.createUser(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Endpunkt: Benutzer nach Benutzername abrufen
|
|
||||||
@GetMapping("/username/{username}")
|
|
||||||
public User getUserByUsername(@PathVariable String username) {
|
|
||||||
return userService.getUserByUsername(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Endpunkt: Benutzer nach E-Mail abrufen
|
|
||||||
@GetMapping("/email/{email}")
|
|
||||||
public User getUserByEmail(@PathVariable String email) {
|
|
||||||
return userService.getUserByEmail(email);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package com.example.hangry;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
|
||||||
|
|
||||||
// Benutzerdefinierte Abfrage: Benutzer nach Benutzernamen finden
|
|
||||||
User findByUsername(String username);
|
|
||||||
|
|
||||||
// Benutzerdefinierte Abfrage: Benutzer nach E-Mail finden
|
|
||||||
User findByEmail(String email);
|
|
||||||
}
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
|||||||
package com.example.hangry;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class UserService {
|
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
|
|
||||||
// Dependency Injection (Spring kümmert sich darum)
|
|
||||||
public UserService(UserRepository userRepository) {
|
|
||||||
this.userRepository = userRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Einen neuen Benutzer erstellen
|
|
||||||
public User createUser(User user) {
|
|
||||||
return userRepository.save(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Benutzer anhand des Benutzernamens suchen
|
|
||||||
public User getUserByUsername(String username) {
|
|
||||||
return userRepository.findByUsername(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Benutzer anhand der E-Mail suchen
|
|
||||||
public User getUserByEmail(String email) {
|
|
||||||
return userRepository.findByEmail(email);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user