cleaned up code format

This commit is contained in:
durn
2026-03-04 01:27:15 -07:00
parent 0a38aa3ded
commit 4bdb449f47
29 changed files with 865 additions and 822 deletions
@@ -65,6 +65,4 @@ public class RecipeController {
return new ResponseEntity<String>("Recipe deleted succesfully!", HttpStatus.OK); return new ResponseEntity<String>("Recipe deleted succesfully!", HttpStatus.OK);
} }
} }
@@ -3,8 +3,6 @@ package com.example.demo.dto;
public class ImageDto { public class ImageDto {
String imageUrl; String imageUrl;
public ImageDto() { public ImageDto() {
super(); super();
} }
@@ -22,5 +20,4 @@ public class ImageDto {
this.imageUrl = imageUrl; this.imageUrl = imageUrl;
} }
} }
@@ -17,15 +17,13 @@ public class RecipeDto {
private List<ImageDto> images; private List<ImageDto> images;
private List<TagDto> tags; private List<TagDto> tags;
public RecipeDto() { public RecipeDto() {
super(); super();
} }
public RecipeDto(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes, public RecipeDto(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
Integer servings, UserDto userDto, String status, List<RecipeIngredientDto> ingredients, List<StepDto> steps, List<ImageDto> images, List<TagDto> tags) { Integer servings, UserDto userDto, String status, List<RecipeIngredientDto> ingredients,
List<StepDto> steps, List<ImageDto> images, List<TagDto> tags) {
super(); super();
this.title = title; this.title = title;
this.description = description; this.description = description;
@@ -45,42 +43,55 @@ public class RecipeDto {
public String getTitle() { public String getTitle() {
return title; return title;
} }
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
public Integer getPrepTimeMinutes() { public Integer getPrepTimeMinutes() {
return prepTimeMinutes; return prepTimeMinutes;
} }
public void setPrepTimeMinutes(Integer prepTimeMinutes) { public void setPrepTimeMinutes(Integer prepTimeMinutes) {
this.prepTimeMinutes = prepTimeMinutes; this.prepTimeMinutes = prepTimeMinutes;
} }
public Integer getCookTimeMinutes() { public Integer getCookTimeMinutes() {
return cookTimeMinutes; return cookTimeMinutes;
} }
public void setCookTimeMinutes(Integer cookTimeMinutes) { public void setCookTimeMinutes(Integer cookTimeMinutes) {
this.cookTimeMinutes = cookTimeMinutes; this.cookTimeMinutes = cookTimeMinutes;
} }
public Integer getServings() { public Integer getServings() {
return servings; return servings;
} }
public void setServings(Integer servings) { public void setServings(Integer servings) {
this.servings = servings; this.servings = servings;
} }
public String getStatus() { public String getStatus() {
return status; return status;
} }
public void setStatus(String status) { public void setStatus(String status) {
this.status = status; this.status = status;
} }
public List<RecipeIngredientDto> getIngredients() { public List<RecipeIngredientDto> getIngredients() {
return ingredients; return ingredients;
} }
public void setIngredients(List<RecipeIngredientDto> ingredients) { public void setIngredients(List<RecipeIngredientDto> ingredients) {
this.ingredients = ingredients; this.ingredients = ingredients;
} }
@@ -93,7 +104,6 @@ public class RecipeDto {
this.steps = steps; this.steps = steps;
} }
public List<ImageDto> getImages() { public List<ImageDto> getImages() {
return images; return images;
} }
@@ -102,7 +112,6 @@ public class RecipeDto {
this.images = images; this.images = images;
} }
public List<TagDto> getTags() { public List<TagDto> getTags() {
return tags; return tags;
} }
@@ -119,5 +128,4 @@ public class RecipeDto {
this.userDto = userDto; this.userDto = userDto;
} }
} }
@@ -8,10 +8,6 @@ public class RecipeIngredientDto {
private String unit; private String unit;
private String notes; private String notes;
public RecipeIngredientDto() { public RecipeIngredientDto() {
super(); super();
} }
@@ -28,24 +24,31 @@ public class RecipeIngredientDto {
public String getIngredientName() { public String getIngredientName() {
return ingredientName; return ingredientName;
} }
public void setIngredientName(String ingredientName) { public void setIngredientName(String ingredientName) {
this.ingredientName = ingredientName; this.ingredientName = ingredientName;
} }
public BigDecimal getQuantity() { public BigDecimal getQuantity() {
return quantity; return quantity;
} }
public void setQuantity(BigDecimal quantity) { public void setQuantity(BigDecimal quantity) {
this.quantity = quantity; this.quantity = quantity;
} }
public String getUnit() { public String getUnit() {
return unit; return unit;
} }
public void setUnit(String unit) { public void setUnit(String unit) {
this.unit = unit; this.unit = unit;
} }
public String getNotes() { public String getNotes() {
return notes; return notes;
} }
public void setNotes(String notes) { public void setNotes(String notes) {
this.notes = notes; this.notes = notes;
} }
@@ -4,7 +4,8 @@ public class StepDto {
private Integer stepNumber; private Integer stepNumber;
private String instruction; private String instruction;
public StepDto() {} public StepDto() {
}
public StepDto(Integer stepNumber, String instruction) { public StepDto(Integer stepNumber, String instruction) {
this.stepNumber = stepNumber; this.stepNumber = stepNumber;
@@ -7,21 +7,15 @@ public class TagDto {
super(); super();
} }
public TagDto(String name) { public TagDto(String name) {
super(); super();
this.name = name; this.name = name;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@@ -5,7 +5,8 @@ public class UserDto {
private String username; private String username;
private String email; private String email;
public UserDto() {} public UserDto() {
}
public UserDto(Integer id, String username, String email) { public UserDto(Integer id, String username, String email) {
this.id = id; this.id = id;
@@ -37,5 +38,4 @@ public class UserDto {
this.email = email; this.email = email;
} }
} }
@@ -6,7 +6,7 @@ import lombok.EqualsAndHashCode;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@Entity @Entity
@Table(name = "recipe_images") // keep table name the same @Table(name = "recipe_images")
public class Image { public class Image {
@Id @Id
@@ -24,7 +24,8 @@ public class Image {
@Column(name = "created_at") @Column(name = "created_at")
private LocalDateTime createdAt; private LocalDateTime createdAt;
public Image() {} public Image() {
}
public Image(Recipe recipe, String imageUrl) { public Image(Recipe recipe, String imageUrl) {
this.recipe = recipe; this.recipe = recipe;
@@ -33,8 +34,13 @@ public class Image {
} }
// Getters and setters // Getters and setters
public Integer getId() { return id; } public Integer getId() {
public void setId(Integer id) { this.id = id; } return id;
}
public void setId(Integer id) {
this.id = id;
}
public Recipe getRecipe() { public Recipe getRecipe() {
return recipe; return recipe;
@@ -44,9 +50,19 @@ public class Image {
this.recipe = recipe; this.recipe = recipe;
} }
public String getImageUrl() { return imageUrl; } public String getImageUrl() {
public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } return imageUrl;
}
public LocalDateTime getCreatedAt() { return createdAt; }
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
} }
@@ -16,36 +16,42 @@ import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
@Entity @Entity
@Table(name = "ingredients", uniqueConstraints = { @Table(name = "ingredients", uniqueConstraints = { @UniqueConstraint(columnNames = "name") })
@UniqueConstraint(columnNames = "name")
})
public class Ingredient { public class Ingredient {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; private Integer id;
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String name; private String name;
@OneToMany(mappedBy = "ingredient") @OneToMany(mappedBy = "ingredient")
private Set<RecipeIngredient> recipeIngredients = new HashSet<>(); private Set<RecipeIngredient> recipeIngredients = new HashSet<>();
// Default constructor required by JPA public Ingredient() {
public Ingredient() {} }
// Convenience constructor
public Ingredient(String name) { public Ingredient(String name) {
this.name = name; this.name = name;
} }
// Getters and setters // Getters and setters
public Integer getId() { return id; } public Integer getId() {
public void setId(Integer id) { this.id = id; } return id;
}
public String getName() { return name; } public void setId(Integer id) {
public void setName(String name) { this.name = name; } this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<RecipeIngredient> getRecipeIngredients() { public Set<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients; return recipeIngredients;
@@ -1,6 +1,7 @@
package com.example.demo.entity; package com.example.demo.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.math.BigDecimal; import java.math.BigDecimal;
@@ -16,6 +17,7 @@ public class Recipe {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; private Integer id;
@NotBlank(message = "Please provide a recipe title")
private String title; private String title;
@Column(columnDefinition = "TEXT") @Column(columnDefinition = "TEXT")
@@ -35,7 +37,6 @@ public class Recipe {
@EqualsAndHashCode.Include @EqualsAndHashCode.Include
private User user; 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<>();
@@ -50,22 +51,19 @@ public class Recipe {
// Recipe Tag relationship and also junction table // Recipe Tag relationship and also junction table
@ManyToMany(fetch = FetchType.LAZY) @ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "recipe_tags", @JoinTable(name = "recipe_tags", joinColumns = { @JoinColumn(name = "recipe_id") }, inverseJoinColumns = {
joinColumns = {@JoinColumn(name = "recipe_id")}, @JoinColumn(name = "tag_id") })
inverseJoinColumns = {@JoinColumn(name = "tag_id")})
private Set<Tag> tags = new HashSet<>(); private Set<Tag> tags = new HashSet<>();
// User is the manager for this relationship // User is the manager for this relationship
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "FavRecipes") @ManyToMany(fetch = FetchType.LAZY, mappedBy = "FavRecipes")
private Set<User> users = new HashSet<>(); private Set<User> users = new HashSet<>();
public Recipe() {
}
// Default constructor public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes, Integer servings,
public Recipe() {} User user, String status) {
// Convenience constructor
public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
Integer servings, User user, String status) {
this.title = title; this.title = title;
this.description = description; this.description = description;
this.prepTimeMinutes = prepTimeMinutes; this.prepTimeMinutes = prepTimeMinutes;
@@ -77,34 +75,14 @@ public class Recipe {
this.updatedAt = LocalDateTime.now(); this.updatedAt = LocalDateTime.now();
} }
// Helper method to add an ingredient
public void addIngredient(Ingredient ingredient, BigDecimal quantity, String unit, String notes) {
RecipeIngredient recipeIngredient = new RecipeIngredient(this, ingredient, quantity, unit, notes);
recipeIngredients.add(recipeIngredient);
ingredient.getRecipeIngredients().add(recipeIngredient);
}
public void removeIngredient(Ingredient ingredient) {
RecipeIngredient remove = null;
for (RecipeIngredient recipeIngredient : recipeIngredients) {
if (recipeIngredient.getIngredient().equals(ingredient)) {
remove = recipeIngredient;
break;
}
}
if (remove != null) {
recipeIngredients.remove(remove);
ingredient.getRecipeIngredients().remove(remove);
remove.setRecipe(null);
remove.setIngredient(null);
}
}
// Getters and setters // Getters and setters
public Integer getId() { return id; } public Integer getId() {
public void setId(Integer id) { this.id = id; } return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() { public User getUser() {
return user; return user;
@@ -114,33 +92,77 @@ public class Recipe {
this.user = user; this.user = user;
} }
public String getTitle() { return title; } public String getTitle() {
public void setTitle(String title) { this.title = title; } return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() { return description; } public String getDescription() {
public void setDescription(String description) { this.description = description; } return description;
}
public Integer getPrepTimeMinutes() { return prepTimeMinutes; } public void setDescription(String description) {
public void setPrepTimeMinutes(Integer prepTimeMinutes) { this.prepTimeMinutes = prepTimeMinutes; } this.description = description;
}
public Integer getCookTimeMinutes() { return cookTimeMinutes; } public Integer getPrepTimeMinutes() {
public void setCookTimeMinutes(Integer cookTimeMinutes) { this.cookTimeMinutes = cookTimeMinutes; } return prepTimeMinutes;
}
public Integer getServings() { return servings; } public void setPrepTimeMinutes(Integer prepTimeMinutes) {
public void setServings(Integer servings) { this.servings = servings; } this.prepTimeMinutes = prepTimeMinutes;
}
public String getStatus() { return status; } public Integer getCookTimeMinutes() {
public void setStatus(String status) { this.status = status; } return cookTimeMinutes;
}
public LocalDateTime getCreatedAt() { return createdAt; } public void setCookTimeMinutes(Integer cookTimeMinutes) {
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } this.cookTimeMinutes = cookTimeMinutes;
}
public LocalDateTime getUpdatedAt() { return updatedAt; } public Integer getServings() {
public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; } return servings;
}
public Set<RecipeIngredient> getRecipeIngredients() { return recipeIngredients; } public void setServings(Integer servings) {
public void setRecipeIngredients(Set<RecipeIngredient> recipeIngredients) { this.recipeIngredients = recipeIngredients; } this.servings = servings;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public LocalDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
public Set<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(Set<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public Set<Step> getSteps() { public Set<Step> getSteps() {
return steps; return steps;
@@ -173,7 +195,4 @@ public class Recipe {
public void setUsers(Set<User> users) { public void setUsers(Set<User> users) {
this.users = users; this.users = users;
} }
} }
@@ -5,11 +5,9 @@ import java.math.BigDecimal;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@Entity @Entity
@Table(name = "recipe_ingredient_junction", @Table(name = "recipe_ingredient_junction", uniqueConstraints = {
uniqueConstraints = {@UniqueConstraint(columnNames = {"recipe_id", "ingredient_id"})}) @UniqueConstraint(columnNames = { "recipe_id", "ingredient_id" }) })
@EqualsAndHashCode( @EqualsAndHashCode(onlyExplicitlyIncluded = true)
onlyExplicitlyIncluded = true
)
public class RecipeIngredient { public class RecipeIngredient {
@Id @Id
@@ -32,7 +30,8 @@ public class RecipeIngredient {
private String notes; private String notes;
public RecipeIngredient() {} public RecipeIngredient() {
}
public RecipeIngredient(Recipe recipe, Ingredient ingredient, BigDecimal quantity, String unit, String notes) { public RecipeIngredient(Recipe recipe, Ingredient ingredient, BigDecimal quantity, String unit, String notes) {
this.recipe = recipe; this.recipe = recipe;
@@ -43,21 +42,51 @@ public class RecipeIngredient {
} }
// Getters and setters // Getters and setters
public Integer getId() { return id; } public Integer getId() {
public void setId(Integer id) { this.id = id; } return id;
}
public Recipe getRecipe() { return recipe; }
public void setRecipe(Recipe recipe) { this.recipe = recipe; } public void setId(Integer id) {
this.id = id;
public Ingredient getIngredient() { return ingredient; } }
public void setIngredient(Ingredient ingredient) { this.ingredient = ingredient; }
public Recipe getRecipe() {
public BigDecimal getQuantity() { return quantity; } return recipe;
public void setQuantity(BigDecimal quantity) { this.quantity = quantity; } }
public String getUnit() { return unit; } public void setRecipe(Recipe recipe) {
public void setUnit(String unit) { this.unit = unit; } this.recipe = recipe;
}
public String getNotes() { return notes; }
public void setNotes(String notes) { this.notes = notes; } public Ingredient getIngredient() {
return ingredient;
}
public void setIngredient(Ingredient ingredient) {
this.ingredient = ingredient;
}
public BigDecimal getQuantity() {
return quantity;
}
public void setQuantity(BigDecimal quantity) {
this.quantity = quantity;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
} }
@@ -16,7 +16,8 @@ public class RecipeTagId implements Serializable {
@Column(name = "tag_id") @Column(name = "tag_id")
private Integer tagId; private Integer tagId;
public RecipeTagId() {} public RecipeTagId() {
}
public RecipeTagId(Integer recipeId, Integer tagId) { public RecipeTagId(Integer recipeId, Integer tagId) {
this.recipeId = recipeId; this.recipeId = recipeId;
@@ -39,14 +40,14 @@ public class RecipeTagId implements Serializable {
this.tagId = tagId; this.tagId = tagId;
} }
// REQUIRED for composite keys
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (!(o instanceof RecipeTagId)) return false; return true;
if (!(o instanceof RecipeTagId))
return false;
RecipeTagId that = (RecipeTagId) o; RecipeTagId that = (RecipeTagId) o;
return Objects.equals(recipeId, that.recipeId) && return Objects.equals(recipeId, that.recipeId) && Objects.equals(tagId, that.tagId);
Objects.equals(tagId, that.tagId);
} }
@Override @Override
@@ -22,7 +22,8 @@ public class Step {
@EqualsAndHashCode.Include @EqualsAndHashCode.Include
private Recipe recipe; private Recipe recipe;
public Step() {} public Step() {
}
public Step(Recipe recipe, Integer stepNumber, String instruction) { public Step(Recipe recipe, Integer stepNumber, String instruction) {
this.recipe = recipe; this.recipe = recipe;
@@ -31,8 +32,13 @@ public class Step {
} }
// Getters and setters // Getters and setters
public Integer getId() { return id; } public Integer getId() {
public void setId(Integer id) { this.id = id; } return id;
}
public void setId(Integer id) {
this.id = id;
}
public Recipe getRecipe() { public Recipe getRecipe() {
return recipe; return recipe;
@@ -42,9 +48,19 @@ public class Step {
this.recipe = recipe; this.recipe = recipe;
} }
public Integer getStepNumber() { return stepNumber; } public Integer getStepNumber() {
public void setStepNumber(Integer stepNumber) { this.stepNumber = stepNumber; } return stepNumber;
}
public String getInstruction() { return instruction; }
public void setInstruction(String instruction) { this.instruction = instruction; } public void setStepNumber(Integer stepNumber) {
this.stepNumber = stepNumber;
}
public String getInstruction() {
return instruction;
}
public void setInstruction(String instruction) {
this.instruction = instruction;
}
} }
@@ -20,8 +20,8 @@ public class Tag {
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "tags") @ManyToMany(fetch = FetchType.LAZY, mappedBy = "tags")
private Set<Recipe> recipes = new HashSet<>(); private Set<Recipe> recipes = new HashSet<>();
// Required by JPA public Tag() {
public Tag() {} }
public Tag(String name) { public Tag(String name) {
this.name = name; this.name = name;
@@ -52,5 +52,4 @@ public class Tag {
this.recipes = recipes; this.recipes = recipes;
} }
} }
@@ -43,13 +43,12 @@ public class User {
// Favorite relationship and also junction table // Favorite relationship and also junction table
@ManyToMany(fetch = FetchType.LAZY) @ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "favorites", @JoinTable(name = "favorites", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = {
joinColumns = {@JoinColumn(name = "userId")}, @JoinColumn(name = "recipeId") })
inverseJoinColumns = {@JoinColumn(name = "recipeId")})
private Set<Recipe> FavRecipes = new HashSet<>(); private Set<Recipe> FavRecipes = new HashSet<>();
// Constructors public User() {
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;
@@ -59,24 +58,53 @@ public class User {
this.createdAt = createdAt; this.createdAt = createdAt;
} }
public Integer getId() {
return id;
}
public Integer getId() { return id; } public void setId(Integer id) {
public void setId(Integer id) { this.id = id; } this.id = id;
}
public String getUsername() { return username; } public String getUsername() {
public void setUsername(String username) { this.username = username; } return username;
}
public String getRole() { return role; } public void setUsername(String username) {
public void setRole(String role) { this.role = role; } this.username = username;
}
public String getEmail() { return email; } public String getRole() {
public void setEmail(String email) { this.email = email; } return role;
}
public String getHashedpassword() { return hashedpassword; } public void setRole(String role) {
public void setHashedpassword(String hashedpassword) { this.hashedpassword = hashedpassword; } this.role = role;
}
public LocalDateTime getCreatedAt() { return createdAt; } public String getEmail() {
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHashedpassword() {
return hashedpassword;
}
public void setHashedpassword(String hashedpassword) {
this.hashedpassword = hashedpassword;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public Set<Recipe> getFavRecipes() { public Set<Recipe> getFavRecipes() {
return FavRecipes; return FavRecipes;
@@ -86,5 +114,4 @@ public class User {
FavRecipes = favRecipes; FavRecipes = favRecipes;
} }
} }
@@ -26,7 +26,6 @@ public class NotFoundException extends RuntimeException{
return fieldName; return fieldName;
} }
public Object getFieldValue() { public Object getFieldValue() {
return fieldValue; return fieldValue;
} }
@@ -5,7 +5,6 @@ import com.example.demo.entity.Image;
import java.util.List; import java.util.List;
public interface ImageRepo extends JpaRepository<Image, Integer> { public interface ImageRepo extends JpaRepository<Image, Integer> {
} }
@@ -5,7 +5,6 @@ import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entity.Ingredient; import com.example.demo.entity.Ingredient;
public interface IngredientRepo extends JpaRepository<Ingredient, Integer> { public interface IngredientRepo extends JpaRepository<Ingredient, Integer> {
Optional<Ingredient> findByName(String name); Optional<Ingredient> findByName(String name);
} }
@@ -3,7 +3,6 @@ package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entity.Recipe; import com.example.demo.entity.Recipe;
public interface RecipeRepo extends JpaRepository<Recipe, Integer> { public interface RecipeRepo extends JpaRepository<Recipe, Integer> {
} }
@@ -1,6 +1,5 @@
package com.example.demo.repository; package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entity.RecipeTag; import com.example.demo.entity.RecipeTag;
import com.example.demo.entity.RecipeTagId; import com.example.demo.entity.RecipeTagId;
@@ -8,5 +8,4 @@ import java.util.List;
public interface StepRepo extends JpaRepository<Step, Integer> { public interface StepRepo extends JpaRepository<Step, Integer> {
} }
@@ -33,7 +33,6 @@ 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;
@@ -42,7 +41,9 @@ private StepRepo stepRepository;
private ImageRepo imageRepository; private ImageRepo imageRepository;
private TagRepo tagRepository; private TagRepo tagRepository;
public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository, RecipeIngredientRepo recipeIngredientRepository, UserRepo userRepository, StepRepo stepRepository, ImageRepo imageRepository, TagRepo tagRepository) { public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository,
RecipeIngredientRepo recipeIngredientRepository, UserRepo userRepository, StepRepo stepRepository,
ImageRepo imageRepository, TagRepo tagRepository) {
super(); super();
this.recipeRepository = recipeRepository; this.recipeRepository = recipeRepository;
this.ingredientRepository = ingredientRepository; this.ingredientRepository = ingredientRepository;
@@ -55,48 +56,23 @@ private TagRepo tagRepository;
public RecipeDto convertToDto(Recipe recipe) { public RecipeDto convertToDto(Recipe recipe) {
List<RecipeIngredientDto> ingredientDtos = recipe.getRecipeIngredients().stream() List<RecipeIngredientDto> ingredientDtos = recipe.getRecipeIngredients().stream()
.map(ri -> new RecipeIngredientDto( .map(ri -> new RecipeIngredientDto(ri.getIngredient().getName(), ri.getQuantity(), ri.getUnit(),
ri.getIngredient().getName(), ri.getNotes()))
ri.getQuantity(),
ri.getUnit(),
ri.getNotes()
))
.toList(); .toList();
List<StepDto> stepDtos = recipe.getSteps().stream() List<StepDto> stepDtos = recipe.getSteps().stream()
.map(ri -> new StepDto( .map(ri -> new StepDto(ri.getStepNumber(), ri.getInstruction())).toList();
ri.getStepNumber(),
ri.getInstruction()
))
.toList();
List<ImageDto> imageDtos = recipe.getImages().stream() List<ImageDto> imageDtos = recipe.getImages().stream().map(ri -> new ImageDto(ri.getImageUrl())).toList();
.map(ri -> new ImageDto(
ri.getImageUrl()
))
.toList();
List<TagDto> tagDtos = recipe.getTags().stream() List<TagDto> tagDtos = recipe.getTags().stream().map(ri -> new TagDto(ri.getName())).toList();
.map(ri -> new TagDto(
ri.getName()
))
.toList();
UserDto userDto = new UserDto(recipe.getUser().getId(), recipe.getUser().getUsername(), recipe.getUser().getEmail()); UserDto userDto = new UserDto(recipe.getUser().getId(), recipe.getUser().getUsername(),
recipe.getUser().getEmail());
return new RecipeDto( return new RecipeDto(recipe.getTitle(), recipe.getDescription(), recipe.getPrepTimeMinutes(),
recipe.getTitle(), recipe.getCookTimeMinutes(), recipe.getServings(), userDto, recipe.getStatus(), ingredientDtos,
recipe.getDescription(), stepDtos, imageDtos, tagDtos);
recipe.getPrepTimeMinutes(),
recipe.getCookTimeMinutes(),
recipe.getServings(),
userDto,
recipe.getStatus(),
ingredientDtos,
stepDtos,
imageDtos,
tagDtos
);
} }
@Override @Override
@@ -105,35 +81,20 @@ private TagRepo tagRepository;
User user = userRepository.findById(dto.getUserDto().getId()) User user = userRepository.findById(dto.getUserDto().getId())
.orElseThrow(() -> new NotFoundException("User", "id", dto.getUserDto().getId())); .orElseThrow(() -> new NotFoundException("User", "id", dto.getUserDto().getId()));
Recipe recipe = new Recipe( Recipe recipe = new Recipe(dto.getTitle(), dto.getDescription(), dto.getPrepTimeMinutes(),
dto.getTitle(), dto.getCookTimeMinutes(), dto.getServings(), user, dto.getStatus());
dto.getDescription(),
dto.getPrepTimeMinutes(),
dto.getCookTimeMinutes(),
dto.getServings(),
user,
dto.getStatus()
);
for (RecipeIngredientDto riDto : dto.getIngredients()) { for (RecipeIngredientDto riDto : dto.getIngredients()) {
Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName()) Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName())
.orElseGet(() -> new Ingredient(riDto.getIngredientName())); .orElseGet(() -> new Ingredient(riDto.getIngredientName()));
if (ingredient.getId() == null) { if (ingredient.getId() == null) {
ingredientRepository.save(ingredient); ingredientRepository.save(ingredient);
} }
RecipeIngredient ri = new RecipeIngredient( RecipeIngredient ri = new RecipeIngredient(recipe, ingredient, riDto.getQuantity(), riDto.getUnit(),
recipe, riDto.getNotes());
ingredient,
riDto.getQuantity(),
riDto.getUnit(),
riDto.getNotes()
);
recipe.getRecipeIngredients().add(ri); recipe.getRecipeIngredients().add(ri);
} }
@@ -152,20 +113,9 @@ private TagRepo tagRepository;
} }
} }
// if (dto.getTags() != null) {
// for (TagDto tagDto : dto.getTags()) {
// Tag tag = new Tag(tagDto.getName());
// tagRepository.save(tag);
// recipe.getTags().add(tag);
// }
// }
for (TagDto tDto : dto.getTags()) { for (TagDto tDto : dto.getTags()) {
Tag tag = tagRepository.findByName(tDto.getName()).orElseGet(() -> new Tag(tDto.getName()));
Tag tag = tagRepository.findByName(tDto.getName())
.orElseGet(() -> new Tag(tDto.getName()));
if (tag.getId() == null) { if (tag.getId() == null) {
tagRepository.save(tag); tagRepository.save(tag);
@@ -194,8 +144,7 @@ private TagRepo tagRepository;
@Override @Override
@Transactional @Transactional
public RecipeDto getRecipeById(Integer Id) { public RecipeDto getRecipeById(Integer Id) {
return convertToDto(recipeRepository.findById(Id).orElseThrow(() -> return convertToDto(recipeRepository.findById(Id).orElseThrow(() -> new NotFoundException("Recipe", "id", Id)));
new NotFoundException("Recipe", "id", Id)));
} }
@Override @Override
@@ -211,7 +160,6 @@ private TagRepo tagRepository;
existingRecipe.setServings(recipeDto.getServings()); existingRecipe.setServings(recipeDto.getServings());
existingRecipe.setStatus(recipeDto.getStatus()); existingRecipe.setStatus(recipeDto.getStatus());
List<RecipeIngredientDto> updatedIngredients = recipeDto.getIngredients(); List<RecipeIngredientDto> updatedIngredients = recipeDto.getIngredients();
List<RecipeIngredient> ingredientsToRemove = new ArrayList<>(); List<RecipeIngredient> ingredientsToRemove = new ArrayList<>();
@@ -246,13 +194,12 @@ private TagRepo tagRepository;
for (RecipeIngredientDto riDto : updatedIngredients) { for (RecipeIngredientDto riDto : updatedIngredients) {
// go through the old list of ingredients until we find a match with updated list // go through the old list of ingredients until we find a match with updated
// list
RecipeIngredient existingRI = existingRecipe.getRecipeIngredients().stream() RecipeIngredient existingRI = existingRecipe.getRecipeIngredients().stream()
.filter(ri -> ri.getIngredient().getName().equals(riDto.getIngredientName())) .filter(ri -> ri.getIngredient().getName().equals(riDto.getIngredientName())).findFirst()
.findFirst()
.orElse(null); .orElse(null);
// if old ingredient just update parameters // if old ingredient just update parameters
if (existingRI != null) { if (existingRI != null) {
@@ -271,13 +218,8 @@ private TagRepo tagRepository;
ingredientRepository.save(ingredient); ingredientRepository.save(ingredient);
} }
RecipeIngredient newRI = new RecipeIngredient( RecipeIngredient newRI = new RecipeIngredient(existingRecipe, ingredient, riDto.getQuantity(),
existingRecipe, riDto.getUnit(), riDto.getNotes());
ingredient,
riDto.getQuantity(),
riDto.getUnit(),
riDto.getNotes()
);
existingRecipe.getRecipeIngredients().add(newRI); existingRecipe.getRecipeIngredients().add(newRI);
} }
@@ -289,7 +231,8 @@ private TagRepo tagRepository;
boolean existsInUpdatedList = updatedSteps.stream() boolean existsInUpdatedList = updatedSteps.stream()
.anyMatch(dto -> dto.getStepNumber().equals(step.getStepNumber())); .anyMatch(dto -> dto.getStepNumber().equals(step.getStepNumber()));
if (!existsInUpdatedList) stepsToRemove.add(step); if (!existsInUpdatedList)
stepsToRemove.add(step);
} }
// delete those steps // delete those steps
existingRecipe.getSteps().removeAll(stepsToRemove); existingRecipe.getSteps().removeAll(stepsToRemove);
@@ -299,9 +242,7 @@ private TagRepo tagRepository;
// find matching step by step number // find matching step by step number
Step existingStep = existingRecipe.getSteps().stream() Step existingStep = existingRecipe.getSteps().stream()
.filter(s -> s.getStepNumber().equals(stepDto.getStepNumber())) .filter(s -> s.getStepNumber().equals(stepDto.getStepNumber())).findFirst().orElse(null);
.findFirst()
.orElse(null);
// if there's a match update the instruction string // if there's a match update the instruction string
if (existingStep != null) { if (existingStep != null) {
@@ -321,50 +262,43 @@ private TagRepo tagRepository;
for (Image image : existingRecipe.getImages()) { for (Image image : existingRecipe.getImages()) {
boolean existsInUpdatedList = updatedImages.stream() boolean existsInUpdatedList = updatedImages.stream()
.anyMatch(dto -> dto.getImageUrl().equals(image.getImageUrl())); .anyMatch(dto -> dto.getImageUrl().equals(image.getImageUrl()));
if (!existsInUpdatedList) imagesToRemove.add(image); if (!existsInUpdatedList)
imagesToRemove.add(image);
} }
existingRecipe.getImages().removeAll(imagesToRemove); existingRecipe.getImages().removeAll(imagesToRemove);
for (ImageDto imageDto : updatedImages) { for (ImageDto imageDto : updatedImages) {
Image existingImage = existingRecipe.getImages().stream() Image existingImage = existingRecipe.getImages().stream()
.filter(img -> img.getImageUrl().equals(imageDto.getImageUrl())) .filter(img -> img.getImageUrl().equals(imageDto.getImageUrl())).findFirst().orElse(null);
.findFirst()
.orElse(null);
if (existingImage != null) { if (existingImage != null) {
existingImage.setImageUrl(imageDto.getImageUrl()); existingImage.setImageUrl(imageDto.getImageUrl());
} } else {
else {
Image newImage = new Image(existingRecipe, imageDto.getImageUrl()); Image newImage = new Image(existingRecipe, imageDto.getImageUrl());
existingRecipe.getImages().add(newImage); existingRecipe.getImages().add(newImage);
} }
} }
} }
// same process as above just with tags instead, except for saving the tag // same process as above just with tags instead, except for saving the tag
// since the relationship for this one was slightly different // since the relationship for this one was slightly different
if (updatedTags != null) { if (updatedTags != null) {
for (Tag tag : existingRecipe.getTags()) { for (Tag tag : existingRecipe.getTags()) {
boolean existsInUpdatedList = updatedTags.stream() boolean existsInUpdatedList = updatedTags.stream().anyMatch(dto -> dto.getName().equals(tag.getName()));
.anyMatch(dto -> dto.getName().equals(tag.getName())); if (!existsInUpdatedList)
if (!existsInUpdatedList) tagsToRemove.add(tag); tagsToRemove.add(tag);
} }
existingRecipe.getTags().removeAll(tagsToRemove); existingRecipe.getTags().removeAll(tagsToRemove);
for (TagDto tagDto : updatedTags) { for (TagDto tagDto : updatedTags) {
Tag existingTag = existingRecipe.getTags().stream() Tag existingTag = existingRecipe.getTags().stream()
.filter(tag -> tag.getName().equals(tagDto.getName())) .filter(tag -> tag.getName().equals(tagDto.getName())).findFirst().orElse(null);
.findFirst()
.orElse(null);
if (existingTag != null) { if (existingTag != null) {
existingTag.setName(tagDto.getName()); existingTag.setName(tagDto.getName());
} } else {
else {
Tag newTag = tagRepository.findByName(tagDto.getName()) Tag newTag = tagRepository.findByName(tagDto.getName())
.orElseGet(() -> tagRepository.save(new Tag(tagDto.getName()))); .orElseGet(() -> tagRepository.save(new Tag(tagDto.getName())));
@@ -378,8 +312,7 @@ private TagRepo tagRepository;
@Override @Override
public void deleteRecipe(Integer Id) { public void deleteRecipe(Integer Id) {
recipeRepository.findById(Id) recipeRepository.findById(Id).orElseThrow(() -> new NotFoundException("Recipe", "id", Id));
.orElseThrow(() -> new NotFoundException("Recipe", "id", Id));
recipeRepository.deleteById(Id); recipeRepository.deleteById(Id);
} }
@@ -34,11 +34,7 @@ public class UserServiceImpl implements UserService{
} }
public UserDto convertToDto(User user) { public UserDto convertToDto(User user) {
return new UserDto( return new UserDto(user.getId(), user.getUsername(), user.getEmail());
user.getId(),
user.getUsername(),
user.getEmail()
);
} }
@Override @Override
@@ -61,31 +57,28 @@ public class UserServiceImpl implements UserService{
@Override @Override
public UserDto getUserById(Integer Id) { public UserDto getUserById(Integer Id) {
return convertToDto(userRepository.findById(Id).orElseThrow(() -> return convertToDto(userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id)));
new NotFoundException("User", "id", Id)));
} }
@Override @Override
@Transactional @Transactional
public UserDto saveFavorite(Integer userId, Integer recipeId) { public UserDto saveFavorite(Integer userId, Integer recipeId) {
User existingUser = userRepository.findById(userId).orElseThrow( User existingUser = userRepository.findById(userId)
() -> new NotFoundException("User", "id", userId)); .orElseThrow(() -> new NotFoundException("User", "id", userId));
Recipe existingRecipe = recipeRepository.findById(recipeId).orElseThrow( Recipe existingRecipe = recipeRepository.findById(recipeId)
() -> new NotFoundException("Recipe", "id", recipeId)); .orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));
existingUser.getFavRecipes().add(existingRecipe); existingUser.getFavRecipes().add(existingRecipe);
userRepository.save(existingUser); userRepository.save(existingUser);
return convertToDto(existingUser); return convertToDto(existingUser);
} }
@Override @Override
public UserDto updateUser(User user, Integer Id) { public UserDto updateUser(User user, Integer Id) {
User existingUser = userRepository.findById(Id).orElseThrow( User existingUser = userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id));
() -> new NotFoundException("User", "id", Id));
existingUser.setUsername(user.getUsername()); existingUser.setUsername(user.getUsername());
existingUser.setEmail(user.getEmail()); existingUser.setEmail(user.getEmail());
@@ -97,25 +90,22 @@ public class UserServiceImpl implements UserService{
@Override @Override
public void deleteUser(Integer Id) { public void deleteUser(Integer Id) {
userRepository.findById(Id).orElseThrow( userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id));
() -> new NotFoundException("User", "id", Id));
userRepository.deleteById(Id); userRepository.deleteById(Id);
} }
@Override @Override
@Transactional @Transactional
public void deleteFavorite(Integer userId, Integer recipeId) { public void deleteFavorite(Integer userId, Integer recipeId) {
User existingUser = userRepository.findById(userId).orElseThrow( User existingUser = userRepository.findById(userId)
() -> new NotFoundException("User", "id", userId)); .orElseThrow(() -> new NotFoundException("User", "id", userId));
Recipe existingRecipe = recipeRepository.findById(recipeId).orElseThrow( Recipe existingRecipe = recipeRepository.findById(recipeId)
() -> new NotFoundException("Recipe", "id", recipeId)); .orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));
userRepository.save(existingUser); userRepository.save(existingUser);
existingUser.getFavRecipes().remove(existingRecipe); existingUser.getFavRecipes().remove(existingRecipe);
} }
} }
@@ -10,10 +10,15 @@ import com.example.demo.entity.User;
public interface RecipeService { public interface RecipeService {
RecipeDto convertToDto(Recipe recipe); RecipeDto convertToDto(Recipe recipe);
RecipeDto saveRecipe(RecipeDto recipe); RecipeDto saveRecipe(RecipeDto recipe);
List<RecipeDto> getAllRecipes(); List<RecipeDto> getAllRecipes();
RecipeDto getRecipeById(Integer recipeId); RecipeDto getRecipeById(Integer recipeId);
RecipeDto updateRecipe(RecipeDto recipedto, Integer Id); RecipeDto updateRecipe(RecipeDto recipedto, Integer Id);
void deleteRecipe(Integer Id); void deleteRecipe(Integer Id);
} }
@@ -7,11 +7,18 @@ import com.example.demo.entity.User;
public interface UserService { public interface UserService {
UserDto convertToDto(User user); UserDto convertToDto(User user);
User saveUser(User user); User saveUser(User user);
List<UserDto> getAllUsers(); List<UserDto> getAllUsers();
UserDto getUserById(Integer Id); UserDto getUserById(Integer Id);
UserDto saveFavorite(Integer userId, Integer recipeId); UserDto saveFavorite(Integer userId, Integer recipeId);
UserDto updateUser(User user, Integer Id); UserDto updateUser(User user, Integer Id);
void deleteUser(Integer Id); void deleteUser(Integer Id);
void deleteFavorite(Integer userId, Integer recipeId); void deleteFavorite(Integer userId, Integer recipeId);
} }