mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
User Recipe one to many relationship established
This commit is contained in:
@@ -10,7 +10,7 @@ public class RecipeDto {
|
|||||||
private Integer prepTimeMinutes;
|
private Integer prepTimeMinutes;
|
||||||
private Integer cookTimeMinutes;
|
private Integer cookTimeMinutes;
|
||||||
private Integer servings;
|
private Integer servings;
|
||||||
private Integer userId;
|
private UserDto userDto;
|
||||||
private String status;
|
private String status;
|
||||||
private List<RecipeIngredientDto> ingredients;
|
private List<RecipeIngredientDto> ingredients;
|
||||||
|
|
||||||
@@ -22,14 +22,14 @@ public class RecipeDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public RecipeDto(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
|
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();
|
super();
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.prepTimeMinutes = prepTimeMinutes;
|
this.prepTimeMinutes = prepTimeMinutes;
|
||||||
this.cookTimeMinutes = cookTimeMinutes;
|
this.cookTimeMinutes = cookTimeMinutes;
|
||||||
this.servings = servings;
|
this.servings = servings;
|
||||||
this.userId = userId;
|
this.userDto = userDto;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.ingredients = ingredients;
|
this.ingredients = ingredients;
|
||||||
}
|
}
|
||||||
@@ -66,12 +66,6 @@ public class RecipeDto {
|
|||||||
public void setServings(Integer servings) {
|
public void setServings(Integer servings) {
|
||||||
this.servings = servings;
|
this.servings = servings;
|
||||||
}
|
}
|
||||||
public Integer getUserId() {
|
|
||||||
return userId;
|
|
||||||
}
|
|
||||||
public void setUserId(Integer userId) {
|
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
public String getStatus() {
|
public String getStatus() {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
@@ -84,5 +78,14 @@ public class RecipeDto {
|
|||||||
public void setIngredients(List<RecipeIngredientDto> ingredients) {
|
public void setIngredients(List<RecipeIngredientDto> ingredients) {
|
||||||
this.ingredients = ingredients;
|
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;
|
package com.example.demo.entity;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -23,14 +25,17 @@ public class Recipe {
|
|||||||
private Integer cookTimeMinutes;
|
private Integer cookTimeMinutes;
|
||||||
private Integer servings;
|
private Integer servings;
|
||||||
|
|
||||||
@Column(name = "user_id", nullable = false)
|
|
||||||
private Integer userId;
|
|
||||||
|
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "user_id", nullable = false)
|
||||||
|
@EqualsAndHashCode.Include
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
|
||||||
// Recipe ingredients relationship
|
// Recipe ingredients relationship
|
||||||
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||||
private Set<RecipeIngredient> recipeIngredients = new HashSet<>();
|
private Set<RecipeIngredient> recipeIngredients = new HashSet<>();
|
||||||
@@ -41,13 +46,13 @@ public class Recipe {
|
|||||||
|
|
||||||
// Convenience constructor
|
// Convenience constructor
|
||||||
public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
|
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.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.prepTimeMinutes = prepTimeMinutes;
|
this.prepTimeMinutes = prepTimeMinutes;
|
||||||
this.cookTimeMinutes = cookTimeMinutes;
|
this.cookTimeMinutes = cookTimeMinutes;
|
||||||
this.servings = servings;
|
this.servings = servings;
|
||||||
this.userId = userId;
|
this.user = user;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.createdAt = LocalDateTime.now();
|
this.createdAt = LocalDateTime.now();
|
||||||
this.updatedAt = LocalDateTime.now();
|
this.updatedAt = LocalDateTime.now();
|
||||||
@@ -82,8 +87,17 @@ public class Recipe {
|
|||||||
public Integer getId() { return id; }
|
public Integer getId() { return id; }
|
||||||
public void setId(Integer id) { this.id = id; }
|
public void setId(Integer id) { this.id = id; }
|
||||||
|
|
||||||
public String getTitle() { return title; }
|
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 void setTitle(String title) { this.title = title; }
|
||||||
|
|
||||||
|
|
||||||
public String getDescription() { return description; }
|
public String getDescription() { return description; }
|
||||||
public void setDescription(String description) { this.description = description; }
|
public void setDescription(String description) { this.description = description; }
|
||||||
@@ -97,9 +111,6 @@ public class Recipe {
|
|||||||
public Integer getServings() { return servings; }
|
public Integer getServings() { return servings; }
|
||||||
public void setServings(Integer servings) { this.servings = 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 String getStatus() { return status; }
|
||||||
public void setStatus(String status) { this.status = status; }
|
public void setStatus(String status) { this.status = status; }
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
package com.example.demo.entity;
|
package com.example.demo.entity;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.FetchType;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.OneToMany;
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.GenerationType;
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.CascadeType;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@@ -28,9 +33,13 @@ public class User {
|
|||||||
|
|
||||||
@Column(name = "created_at")
|
@Column(name = "created_at")
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
// User Recipe relationship
|
||||||
|
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||||
|
private Set<Recipe> recipes = new HashSet<>();
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
public User() {} // default constructor required by JPA
|
public User() {}
|
||||||
|
|
||||||
public User(String username, String role, String email, String hashedpassword, LocalDateTime createdAt) {
|
public User(String username, String role, String email, String hashedpassword, LocalDateTime createdAt) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
@@ -40,7 +49,7 @@ public class User {
|
|||||||
this.createdAt = createdAt;
|
this.createdAt = createdAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters and Setters
|
|
||||||
public Integer getId() { return id; }
|
public Integer getId() { return id; }
|
||||||
public void setId(Integer id) { this.id = id; }
|
public void setId(Integer id) { this.id = id; }
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.List;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.example.demo.dto.RecipeDto;
|
import com.example.demo.dto.RecipeDto;
|
||||||
|
import com.example.demo.dto.UserDto;
|
||||||
import com.example.demo.dto.RecipeIngredientDto;
|
import com.example.demo.dto.RecipeIngredientDto;
|
||||||
import com.example.demo.entity.Ingredient;
|
import com.example.demo.entity.Ingredient;
|
||||||
import com.example.demo.entity.Recipe;
|
import com.example.demo.entity.Recipe;
|
||||||
@@ -15,22 +16,26 @@ import com.example.demo.exception.NotFoundException;
|
|||||||
import com.example.demo.repository.IngredientRepo;
|
import com.example.demo.repository.IngredientRepo;
|
||||||
import com.example.demo.repository.RecipeIngredientRepo;
|
import com.example.demo.repository.RecipeIngredientRepo;
|
||||||
import com.example.demo.repository.RecipeRepo;
|
import com.example.demo.repository.RecipeRepo;
|
||||||
|
import com.example.demo.repository.UserRepo;
|
||||||
import com.example.demo.service.RecipeService;
|
import com.example.demo.service.RecipeService;
|
||||||
|
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class RecipeServiceImpl implements RecipeService{
|
public class RecipeServiceImpl implements RecipeService{
|
||||||
|
|
||||||
|
|
||||||
private RecipeRepo recipeRepository;
|
private RecipeRepo recipeRepository;
|
||||||
private IngredientRepo ingredientRepository;
|
private IngredientRepo ingredientRepository;
|
||||||
private RecipeIngredientRepo recipeIngredientRepository;
|
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();
|
super();
|
||||||
this.recipeRepository = recipeRepository;
|
this.recipeRepository = recipeRepository;
|
||||||
this.ingredientRepository = ingredientRepository;
|
this.ingredientRepository = ingredientRepository;
|
||||||
this.recipeIngredientRepository = recipeIngredientRepository;
|
this.recipeIngredientRepository = recipeIngredientRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecipeDto convertToDto(Recipe recipe) {
|
public RecipeDto convertToDto(Recipe recipe) {
|
||||||
@@ -42,14 +47,15 @@ private RecipeIngredientRepo recipeIngredientRepository;
|
|||||||
ri.getNotes()
|
ri.getNotes()
|
||||||
))
|
))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
UserDto userDto = new UserDto(recipe.getUser().getId());
|
||||||
return new RecipeDto(
|
return new RecipeDto(
|
||||||
recipe.getTitle(),
|
recipe.getTitle(),
|
||||||
recipe.getDescription(),
|
recipe.getDescription(),
|
||||||
recipe.getPrepTimeMinutes(),
|
recipe.getPrepTimeMinutes(),
|
||||||
recipe.getCookTimeMinutes(),
|
recipe.getCookTimeMinutes(),
|
||||||
recipe.getServings(),
|
recipe.getServings(),
|
||||||
recipe.getUserId(),
|
userDto,
|
||||||
recipe.getStatus(),
|
recipe.getStatus(),
|
||||||
ingredientDtos
|
ingredientDtos
|
||||||
);
|
);
|
||||||
@@ -57,14 +63,17 @@ private RecipeIngredientRepo recipeIngredientRepository;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RecipeDto saveRecipe(RecipeDto dto) {
|
public RecipeDto saveRecipe(RecipeDto dto) {
|
||||||
|
|
||||||
|
User user = userRepository.findById(dto.getUserDto().getId())
|
||||||
|
.orElseThrow(() -> new NotFoundException("User", "id", dto.getUserDto().getId()));
|
||||||
|
|
||||||
Recipe recipe = new Recipe(
|
Recipe recipe = new Recipe(
|
||||||
dto.getTitle(),
|
dto.getTitle(),
|
||||||
dto.getDescription(),
|
dto.getDescription(),
|
||||||
dto.getPrepTimeMinutes(),
|
dto.getPrepTimeMinutes(),+
|
||||||
dto.getCookTimeMinutes(),
|
dto.getCookTimeMinutes(),
|
||||||
dto.getServings(),
|
dto.getServings(),
|
||||||
dto.getUserId(),
|
user,
|
||||||
dto.getStatus()
|
dto.getStatus()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user