User Recipe one to many relationship established

This commit is contained in:
durn
2026-03-03 03:44:57 -07:00
parent 5c78a983dc
commit e2fb9c1034
5 changed files with 77 additions and 26 deletions
@@ -10,7 +10,7 @@ public class RecipeDto {
private Integer prepTimeMinutes;
private Integer cookTimeMinutes;
private Integer servings;
private Integer userId;
private UserDto userDto;
private String status;
private List<RecipeIngredientDto> ingredients;
@@ -22,14 +22,14 @@ public class RecipeDto {
}
public RecipeDto(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
Integer servings, Integer userId, String status, List<RecipeIngredientDto> ingredients) {
Integer servings, UserDto userDto, String status, List<RecipeIngredientDto> ingredients) {
super();
this.title = title;
this.description = description;
this.prepTimeMinutes = prepTimeMinutes;
this.cookTimeMinutes = cookTimeMinutes;
this.servings = servings;
this.userId = userId;
this.userDto = userDto;
this.status = status;
this.ingredients = ingredients;
}
@@ -66,12 +66,6 @@ public class RecipeDto {
public void setServings(Integer servings) {
this.servings = servings;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getStatus() {
return status;
}
@@ -85,4 +79,13 @@ public class RecipeDto {
this.ingredients = ingredients;
}
public UserDto getUserDto() {
return userDto;
}
public void setUserDto(UserDto userDto) {
this.userDto = userDto;
}
}
@@ -0,0 +1,19 @@
package com.example.demo.dto;
public class UserDto {
private Integer id;
public UserDto() {}
public UserDto(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@@ -1,6 +1,8 @@
package com.example.demo.entity;
import jakarta.persistence.*;
import lombok.EqualsAndHashCode;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
@@ -23,14 +25,17 @@ public class Recipe {
private Integer cookTimeMinutes;
private Integer servings;
@Column(name = "user_id", nullable = false)
private Integer userId;
private String status;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@EqualsAndHashCode.Include
private User user;
// Recipe ingredients relationship
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<RecipeIngredient> recipeIngredients = new HashSet<>();
@@ -41,13 +46,13 @@ public class Recipe {
// Convenience constructor
public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
Integer servings, Integer userId, String status) {
Integer servings, User user, String status) {
this.title = title;
this.description = description;
this.prepTimeMinutes = prepTimeMinutes;
this.cookTimeMinutes = cookTimeMinutes;
this.servings = servings;
this.userId = userId;
this.user = user;
this.status = status;
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
@@ -82,9 +87,18 @@ public class Recipe {
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
@@ -97,9 +111,6 @@ public class Recipe {
public Integer getServings() { return servings; }
public void setServings(Integer servings) { this.servings = servings; }
public Integer getUserId() { return userId; }
public void setUserId(Integer userId) { this.userId = userId; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
@@ -1,12 +1,17 @@
package com.example.demo.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Table;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "users")
@@ -29,8 +34,12 @@ public class User {
@Column(name = "created_at")
private LocalDateTime createdAt;
// User Recipe relationship
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<Recipe> recipes = new HashSet<>();
// Constructors
public User() {} // default constructor required by JPA
public User() {}
public User(String username, String role, String email, String hashedpassword, LocalDateTime createdAt) {
this.username = username;
@@ -40,7 +49,7 @@ public class User {
this.createdAt = createdAt;
}
// Getters and Setters
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@@ -6,6 +6,7 @@ import java.util.List;
import org.springframework.stereotype.Service;
import com.example.demo.dto.RecipeDto;
import com.example.demo.dto.UserDto;
import com.example.demo.dto.RecipeIngredientDto;
import com.example.demo.entity.Ingredient;
import com.example.demo.entity.Recipe;
@@ -15,6 +16,7 @@ import com.example.demo.exception.NotFoundException;
import com.example.demo.repository.IngredientRepo;
import com.example.demo.repository.RecipeIngredientRepo;
import com.example.demo.repository.RecipeRepo;
import com.example.demo.repository.UserRepo;
import com.example.demo.service.RecipeService;
import jakarta.transaction.Transactional;
@@ -22,15 +24,18 @@ import jakarta.transaction.Transactional;
@Service
public class RecipeServiceImpl implements RecipeService{
private RecipeRepo recipeRepository;
private IngredientRepo ingredientRepository;
private RecipeIngredientRepo recipeIngredientRepository;
private UserRepo userRepository;
public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository, RecipeIngredientRepo recipeIngredientRepository) {
public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository, RecipeIngredientRepo recipeIngredientRepository, UserRepo userRepository) {
super();
this.recipeRepository = recipeRepository;
this.ingredientRepository = ingredientRepository;
this.recipeIngredientRepository = recipeIngredientRepository;
this.userRepository = userRepository;
}
public RecipeDto convertToDto(Recipe recipe) {
@@ -43,13 +48,14 @@ private RecipeIngredientRepo recipeIngredientRepository;
))
.toList();
UserDto userDto = new UserDto(recipe.getUser().getId());
return new RecipeDto(
recipe.getTitle(),
recipe.getDescription(),
recipe.getPrepTimeMinutes(),
recipe.getCookTimeMinutes(),
recipe.getServings(),
recipe.getUserId(),
userDto,
recipe.getStatus(),
ingredientDtos
);
@@ -58,13 +64,16 @@ private RecipeIngredientRepo recipeIngredientRepository;
@Override
public RecipeDto saveRecipe(RecipeDto dto) {
User user = userRepository.findById(dto.getUserDto().getId())
.orElseThrow(() -> new NotFoundException("User", "id", dto.getUserDto().getId()));
Recipe recipe = new Recipe(
dto.getTitle(),
dto.getDescription(),
dto.getPrepTimeMinutes(),
dto.getPrepTimeMinutes(),+
dto.getCookTimeMinutes(),
dto.getServings(),
dto.getUserId(),
user,
dto.getStatus()
);