From 4bdb449f472565b61d322ba0c34da710222e33d6 Mon Sep 17 00:00:00 2001 From: durn Date: Wed, 4 Mar 2026 01:27:15 -0700 Subject: [PATCH] cleaned up code format --- .../demo/controller/RecipeController.java | 40 +- .../demo/controller/UserController.java | 58 +- .../java/com/example/demo/dto/ImageDto.java | 7 +- .../java/com/example/demo/dto/RecipeDto.java | 54 +- .../example/demo/dto/RecipeIngredientDto.java | 31 +- .../java/com/example/demo/dto/StepDto.java | 39 +- .../java/com/example/demo/dto/TagDto.java | 12 +- .../java/com/example/demo/dto/UserDto.java | 34 +- .../java/com/example/demo/entity/Image.java | 68 ++- .../com/example/demo/entity/Ingredient.java | 56 +- .../java/com/example/demo/entity/Recipe.java | 229 ++++---- .../example/demo/entity/RecipeIngredient.java | 109 ++-- .../com/example/demo/entity/RecipeTagId.java | 69 +-- .../java/com/example/demo/entity/Step.java | 68 ++- .../java/com/example/demo/entity/Tag.java | 57 +- .../java/com/example/demo/entity/User.java | 119 ++-- .../demo/exception/NotFoundException.java | 9 +- .../example/demo/repository/FavoriteRepo.java | 2 +- .../example/demo/repository/ImageRepo.java | 3 +- .../demo/repository/IngredientRepo.java | 1 - .../demo/repository/RecipeIngredientRepo.java | 2 +- .../example/demo/repository/RecipeRepo.java | 1 - .../demo/repository/RecipeTagRepo.java | 1 - .../com/example/demo/repository/StepRepo.java | 1 - .../com/example/demo/repository/UserRepo.java | 2 +- .../demo/service/Impl/RecipeServiceImpl.java | 515 ++++++++---------- .../demo/service/Impl/UserServiceImpl.java | 84 ++- .../example/demo/service/RecipeService.java | 9 +- .../com/example/demo/service/UserService.java | 7 + 29 files changed, 865 insertions(+), 822 deletions(-) diff --git a/demo/src/main/java/com/example/demo/controller/RecipeController.java b/demo/src/main/java/com/example/demo/controller/RecipeController.java index 9650a16..6e36c50 100644 --- a/demo/src/main/java/com/example/demo/controller/RecipeController.java +++ b/demo/src/main/java/com/example/demo/controller/RecipeController.java @@ -21,50 +21,48 @@ import com.example.demo.service.RecipeService; @RestController @RequestMapping("/api/recipes") public class RecipeController { - + private RecipeService recipeService; public RecipeController(RecipeService recipeService) { super(); this.recipeService = recipeService; } - - //build create recipe REST API + + // build create recipe REST API @PostMapping - public ResponseEntity saveUser(@RequestBody Recipe recipe){ + public ResponseEntity saveUser(@RequestBody Recipe recipe) { RecipeDto recipeDto = recipeService.convertToDto(recipe); return new ResponseEntity(recipeService.saveRecipe(recipeDto), HttpStatus.CREATED); } - - //build get all recipes REST API + + // build get all recipes REST API @GetMapping - public List getAllRecipes(){ + public List getAllRecipes() { return recipeService.getAllRecipes(); } - - //build get recipe by id REST API + + // build get recipe by id REST API // http://localhost:8080/api/recipes/(id number goes here) @GetMapping("{id}") - public ResponseEntity getRecipeById(@PathVariable("id") Integer recipeId){ + public ResponseEntity getRecipeById(@PathVariable("id") Integer recipeId) { return new ResponseEntity(recipeService.getRecipeById(recipeId), HttpStatus.OK); } - - //build update recipe REST API - //http://localhost:8080/api/recipes/(id number goes here) + + // build update recipe REST API + // http://localhost:8080/api/recipes/(id number goes here) @PutMapping("{id}") - public ResponseEntity updateUser(@PathVariable("id") Integer recipeId, @RequestBody Recipe recipe){ + public ResponseEntity updateUser(@PathVariable("id") Integer recipeId, @RequestBody Recipe recipe) { RecipeDto recipeDto = recipeService.convertToDto(recipe); return new ResponseEntity(recipeService.updateRecipe(recipeDto, recipeId), HttpStatus.OK); } - - //build delete recipe REST API - //http://localhost:8080/api/recipes/(id number goes here) + + // build delete recipe REST API + // http://localhost:8080/api/recipes/(id number goes here) @DeleteMapping("{id}") - public ResponseEntity deleteUser(@PathVariable("id") Integer recipeId){ + public ResponseEntity deleteUser(@PathVariable("id") Integer recipeId) { recipeService.deleteRecipe(recipeId); return new ResponseEntity("Recipe deleted succesfully!", HttpStatus.OK); } - - - + } diff --git a/demo/src/main/java/com/example/demo/controller/UserController.java b/demo/src/main/java/com/example/demo/controller/UserController.java index 1f0ee4e..4097702 100644 --- a/demo/src/main/java/com/example/demo/controller/UserController.java +++ b/demo/src/main/java/com/example/demo/controller/UserController.java @@ -20,63 +20,63 @@ import com.example.demo.service.UserService; @RestController @RequestMapping("/api/users") public class UserController { - + private UserService userService; public UserController(UserService userService) { super(); this.userService = userService; } - - //build create user REST API + + // build create user REST API @PostMapping - public ResponseEntity saveUser(@RequestBody User user){ - + public ResponseEntity saveUser(@RequestBody User user) { + return new ResponseEntity(userService.saveUser(user), HttpStatus.CREATED); } - - //build get all users REST API + + // build get all users REST API @GetMapping - public List getAllUsers(){ + public List getAllUsers() { return userService.getAllUsers(); } - - //build get user by id REST API + + // build get user by id REST API // http://localhost:8080/api/users/(id number goes here) @GetMapping("{id}") - public ResponseEntity getUserById(@PathVariable("id") Integer userId){ + public ResponseEntity getUserById(@PathVariable("id") Integer userId) { return new ResponseEntity(userService.getUserById(userId), HttpStatus.OK); } - - //build create favorite REST API + + // build create favorite REST API @PostMapping("/{userId}/favorites/{recipeId}") - public ResponseEntity saveFavorite( @PathVariable Integer userId, @PathVariable Integer recipeId) { + public ResponseEntity saveFavorite(@PathVariable Integer userId, @PathVariable Integer recipeId) { - UserDto updatedUser = userService.saveFavorite(userId, recipeId); + UserDto updatedUser = userService.saveFavorite(userId, recipeId); - return new ResponseEntity<>(updatedUser, HttpStatus.OK); + return new ResponseEntity<>(updatedUser, HttpStatus.OK); } - - //build update user REST API + + // build update user REST API // http://localhost:8080/api/users/(id number goes here) @PutMapping("{id}") - public ResponseEntity updateUser(@PathVariable("id") Integer userId, @RequestBody User user){ + public ResponseEntity updateUser(@PathVariable("id") Integer userId, @RequestBody User user) { return new ResponseEntity(userService.updateUser(user, userId), HttpStatus.OK); } - - //build delete user REST API + + // build delete user REST API @DeleteMapping("{id}") - public ResponseEntity deleteUser(@PathVariable("id") Integer userId){ + public ResponseEntity deleteUser(@PathVariable("id") Integer userId) { userService.deleteUser(userId); return new ResponseEntity("User deleted succesfully!", HttpStatus.OK); } - - //build delete favorite REST API - @DeleteMapping("/{userId}/favorites/{recipeId}") - public ResponseEntity deleteFavorite( @PathVariable Integer userId, @PathVariable Integer recipeId) { - userService.deleteFavorite(userId, recipeId); - return new ResponseEntity("Favorite deleted succesfully!", HttpStatus.OK); - } + // build delete favorite REST API + @DeleteMapping("/{userId}/favorites/{recipeId}") + public ResponseEntity deleteFavorite(@PathVariable Integer userId, @PathVariable Integer recipeId) { + + userService.deleteFavorite(userId, recipeId); + return new ResponseEntity("Favorite deleted succesfully!", HttpStatus.OK); + } } diff --git a/demo/src/main/java/com/example/demo/dto/ImageDto.java b/demo/src/main/java/com/example/demo/dto/ImageDto.java index c68ec9e..42902d8 100644 --- a/demo/src/main/java/com/example/demo/dto/ImageDto.java +++ b/demo/src/main/java/com/example/demo/dto/ImageDto.java @@ -2,9 +2,7 @@ package com.example.demo.dto; public class ImageDto { String imageUrl; - - - + public ImageDto() { super(); } @@ -21,6 +19,5 @@ public class ImageDto { public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } - - + } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/dto/RecipeDto.java b/demo/src/main/java/com/example/demo/dto/RecipeDto.java index 30927be..8ddae95 100644 --- a/demo/src/main/java/com/example/demo/dto/RecipeDto.java +++ b/demo/src/main/java/com/example/demo/dto/RecipeDto.java @@ -5,27 +5,25 @@ import java.util.List; import com.example.demo.entity.Recipe; public class RecipeDto { - private String title; - private String description; - private Integer prepTimeMinutes; - private Integer cookTimeMinutes; - private Integer servings; - private UserDto userDto; - private String status; - private List ingredients; - private List steps; - private List images; - private List tags; - - - - - public RecipeDto() { + private String title; + private String description; + private Integer prepTimeMinutes; + private Integer cookTimeMinutes; + private Integer servings; + private UserDto userDto; + private String status; + private List ingredients; + private List steps; + private List images; + private List tags; + + public RecipeDto() { super(); } public RecipeDto(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes, - Integer servings, UserDto userDto, String status, List ingredients, List steps, List images, List tags) { + Integer servings, UserDto userDto, String status, List ingredients, + List steps, List images, List tags) { super(); this.title = title; this.description = description; @@ -39,48 +37,61 @@ public class RecipeDto { this.images = images; this.tags = tags; } - + // getters and setters - + 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; } + public Integer getPrepTimeMinutes() { return prepTimeMinutes; } + public void setPrepTimeMinutes(Integer prepTimeMinutes) { this.prepTimeMinutes = prepTimeMinutes; } + public Integer getCookTimeMinutes() { return cookTimeMinutes; } + public void setCookTimeMinutes(Integer cookTimeMinutes) { this.cookTimeMinutes = cookTimeMinutes; } + public Integer getServings() { return servings; } + public void setServings(Integer servings) { this.servings = servings; } + public String getStatus() { return status; } + public void setStatus(String status) { this.status = status; } + public List getIngredients() { return ingredients; } + public void setIngredients(List ingredients) { this.ingredients = ingredients; } @@ -92,7 +103,6 @@ public class RecipeDto { public void setSteps(List steps) { this.steps = steps; } - public List getImages() { return images; @@ -101,7 +111,6 @@ public class RecipeDto { public void setImages(List images) { this.images = images; } - public List getTags() { return tags; @@ -118,6 +127,5 @@ public class RecipeDto { public void setUserDto(UserDto userDto) { this.userDto = userDto; } - - + } diff --git a/demo/src/main/java/com/example/demo/dto/RecipeIngredientDto.java b/demo/src/main/java/com/example/demo/dto/RecipeIngredientDto.java index 7f63677..f20e670 100644 --- a/demo/src/main/java/com/example/demo/dto/RecipeIngredientDto.java +++ b/demo/src/main/java/com/example/demo/dto/RecipeIngredientDto.java @@ -3,18 +3,14 @@ package com.example.demo.dto; import java.math.BigDecimal; public class RecipeIngredientDto { - private String ingredientName; - private BigDecimal quantity; - private String unit; - private String notes; - - - + private String ingredientName; + private BigDecimal quantity; + private String unit; + private String notes; - - public RecipeIngredientDto() { - super(); - } + public RecipeIngredientDto() { + super(); + } public RecipeIngredientDto(String ingredientName, BigDecimal quantity, String unit, String notes) { super(); @@ -23,31 +19,38 @@ public class RecipeIngredientDto { this.unit = unit; this.notes = notes; } - - // getters and setters + + // getters and setters public String getIngredientName() { return ingredientName; } + public void setIngredientName(String ingredientName) { this.ingredientName = ingredientName; } + 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; } - + } diff --git a/demo/src/main/java/com/example/demo/dto/StepDto.java b/demo/src/main/java/com/example/demo/dto/StepDto.java index dac9030..47357bb 100644 --- a/demo/src/main/java/com/example/demo/dto/StepDto.java +++ b/demo/src/main/java/com/example/demo/dto/StepDto.java @@ -1,29 +1,30 @@ package com.example.demo.dto; public class StepDto { - private Integer stepNumber; - private String instruction; + private Integer stepNumber; + private String instruction; - public StepDto() {} + public StepDto() { + } - public StepDto(Integer stepNumber, String instruction) { - this.stepNumber = stepNumber; - this.instruction = instruction; - } + public StepDto(Integer stepNumber, String instruction) { + this.stepNumber = stepNumber; + this.instruction = instruction; + } - public Integer getStepNumber() { - return stepNumber; - } + public Integer getStepNumber() { + return stepNumber; + } - public void setStepNumber(Integer stepNumber) { - this.stepNumber = stepNumber; - } + public void setStepNumber(Integer stepNumber) { + this.stepNumber = stepNumber; + } - public String getInstruction() { - return instruction; - } + public String getInstruction() { + return instruction; + } - public void setInstruction(String instruction) { - this.instruction = instruction; - } + public void setInstruction(String instruction) { + this.instruction = instruction; + } } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/dto/TagDto.java b/demo/src/main/java/com/example/demo/dto/TagDto.java index b89af97..9c90469 100644 --- a/demo/src/main/java/com/example/demo/dto/TagDto.java +++ b/demo/src/main/java/com/example/demo/dto/TagDto.java @@ -1,28 +1,22 @@ package com.example.demo.dto; public class TagDto { - private String name; - + private String name; + public TagDto() { super(); } - - public TagDto(String name) { super(); this.name = name; } - - public String getName() { return name; } - - public void setName(String name) { this.name = name; - } + } } diff --git a/demo/src/main/java/com/example/demo/dto/UserDto.java b/demo/src/main/java/com/example/demo/dto/UserDto.java index 7f41309..7b8a011 100644 --- a/demo/src/main/java/com/example/demo/dto/UserDto.java +++ b/demo/src/main/java/com/example/demo/dto/UserDto.java @@ -1,25 +1,26 @@ package com.example.demo.dto; public class UserDto { - private Integer id; - private String username; - private String email; + private Integer id; + private String username; + private String email; - public UserDto() {} + public UserDto() { + } - public UserDto(Integer id, String username, String email) { - this.id = id; - this.username = username; - this.email = email; - } + public UserDto(Integer id, String username, String email) { + this.id = id; + this.username = username; + this.email = email; + } - 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 getUsername() { return username; @@ -36,6 +37,5 @@ public class UserDto { public void setEmail(String email) { this.email = email; } - - + } diff --git a/demo/src/main/java/com/example/demo/entity/Image.java b/demo/src/main/java/com/example/demo/entity/Image.java index 3ae18f7..e899a27 100644 --- a/demo/src/main/java/com/example/demo/entity/Image.java +++ b/demo/src/main/java/com/example/demo/entity/Image.java @@ -6,37 +6,43 @@ import lombok.EqualsAndHashCode; import java.time.LocalDateTime; @Entity -@Table(name = "recipe_images") // keep table name the same +@Table(name = "recipe_images") public class Image { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "recipe_id", nullable = false) - @EqualsAndHashCode.Include - private Recipe recipe; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "recipe_id", nullable = false) + @EqualsAndHashCode.Include + private Recipe recipe; - @Column(name = "image_url", nullable = false) - private String imageUrl; + @Column(name = "image_url", nullable = false) + private String imageUrl; - @Column(name = "created_at") - private LocalDateTime createdAt; + @Column(name = "created_at") + private LocalDateTime createdAt; - public Image() {} + public Image() { + } - public Image(Recipe recipe, String imageUrl) { - this.recipe = recipe; - this.imageUrl = imageUrl; - this.createdAt = LocalDateTime.now(); - } + public Image(Recipe recipe, String imageUrl) { + this.recipe = recipe; + this.imageUrl = imageUrl; + this.createdAt = LocalDateTime.now(); + } - // Getters and setters - public Integer getId() { return id; } - public void setId(Integer id) { this.id = id; } + // Getters and setters + public Integer getId() { + return id; + } - public Recipe getRecipe() { + public void setId(Integer id) { + this.id = id; + } + + public Recipe getRecipe() { return recipe; } @@ -44,9 +50,19 @@ public class Image { this.recipe = recipe; } - public String getImageUrl() { return imageUrl; } - public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } + public String getImageUrl() { + 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; + } } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/entity/Ingredient.java b/demo/src/main/java/com/example/demo/entity/Ingredient.java index 4cce815..6f932f0 100644 --- a/demo/src/main/java/com/example/demo/entity/Ingredient.java +++ b/demo/src/main/java/com/example/demo/entity/Ingredient.java @@ -16,38 +16,44 @@ import jakarta.persistence.CascadeType; import jakarta.persistence.Column; @Entity -@Table(name = "ingredients", uniqueConstraints = { - @UniqueConstraint(columnNames = "name") -}) +@Table(name = "ingredients", uniqueConstraints = { @UniqueConstraint(columnNames = "name") }) public class Ingredient { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; - + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @Column(nullable = false, unique = true) - private String name; - - @OneToMany(mappedBy = "ingredient") - private Set recipeIngredients = new HashSet<>(); + @Column(nullable = false, unique = true) + private String name; - // Default constructor required by JPA - public Ingredient() {} + @OneToMany(mappedBy = "ingredient") + private Set recipeIngredients = new HashSet<>(); - // Convenience constructor - public Ingredient(String name) { - this.name = name; - } + public Ingredient() { + } - // Getters and setters - public Integer getId() { return id; } - public void setId(Integer id) { this.id = id; } + public Ingredient(String name) { + this.name = name; + } - public String getName() { return name; } - public void setName(String name) { this.name = name; } - - public Set getRecipeIngredients() { + // Getters and setters + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getRecipeIngredients() { return recipeIngredients; } diff --git a/demo/src/main/java/com/example/demo/entity/Recipe.java b/demo/src/main/java/com/example/demo/entity/Recipe.java index 20b3264..228a83d 100644 --- a/demo/src/main/java/com/example/demo/entity/Recipe.java +++ b/demo/src/main/java/com/example/demo/entity/Recipe.java @@ -1,6 +1,7 @@ package com.example.demo.entity; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import lombok.EqualsAndHashCode; import java.math.BigDecimal; @@ -12,101 +13,78 @@ import java.util.Set; @Table(name = "recipes") public class Recipe { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - private String title; + @NotBlank(message = "Please provide a recipe title") + private String title; - @Column(columnDefinition = "TEXT") - private String description; + @Column(columnDefinition = "TEXT") + private String description; - private Integer prepTimeMinutes; - private Integer cookTimeMinutes; - private Integer servings; + private Integer prepTimeMinutes; + private Integer cookTimeMinutes; + private Integer servings; - private String status; + 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 recipeIngredients = new HashSet<>(); - - // Recipe Steps relationship - @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) - private Set steps = new HashSet<>(); - - // Recipe Images relationship - @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) - private Set images = new HashSet<>(); - - // Recipe Tag relationship and also junction table - @ManyToMany(fetch = FetchType.LAZY) - @JoinTable(name = "recipe_tags", - joinColumns = {@JoinColumn(name = "recipe_id")}, - inverseJoinColumns = {@JoinColumn(name = "tag_id")}) - private Set tags = new HashSet<>(); - - // User is the manager for this relationship - @ManyToMany(fetch = FetchType.LAZY, mappedBy = "FavRecipes") - private Set users = new HashSet<>(); + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + @EqualsAndHashCode.Include + private User user; - // Default constructor - public Recipe() {} + // Recipe ingredients relationship + @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + private Set recipeIngredients = new HashSet<>(); - // Convenience constructor - public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes, - Integer servings, User user, String status) { - this.title = title; - this.description = description; - this.prepTimeMinutes = prepTimeMinutes; - this.cookTimeMinutes = cookTimeMinutes; - this.servings = servings; - this.user = user; - this.status = status; - this.createdAt = LocalDateTime.now(); - this.updatedAt = LocalDateTime.now(); - } + // Recipe Steps relationship + @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + private Set steps = new HashSet<>(); - // 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); - } - } + // Recipe Images relationship + @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + private Set images = new HashSet<>(); + // Recipe Tag relationship and also junction table + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable(name = "recipe_tags", joinColumns = { @JoinColumn(name = "recipe_id") }, inverseJoinColumns = { + @JoinColumn(name = "tag_id") }) + private Set tags = new HashSet<>(); - // Getters and setters - public Integer getId() { return id; } - public void setId(Integer id) { this.id = id; } + // User is the manager for this relationship + @ManyToMany(fetch = FetchType.LAZY, mappedBy = "FavRecipes") + private Set users = new HashSet<>(); - public User getUser() { + public Recipe() { + } + + public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes, Integer servings, + User user, String status) { + this.title = title; + this.description = description; + this.prepTimeMinutes = prepTimeMinutes; + this.cookTimeMinutes = cookTimeMinutes; + this.servings = servings; + this.user = user; + this.status = status; + this.createdAt = LocalDateTime.now(); + this.updatedAt = LocalDateTime.now(); + } + + // Getters and setters + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public User getUser() { return user; } @@ -114,33 +92,77 @@ public class Recipe { this.user = user; } - public String getTitle() { return title; } - public void setTitle(String title) { this.title = title; } - + public String getTitle() { + return title; + } - public String getDescription() { return description; } - public void setDescription(String description) { this.description = description; } + public void setTitle(String title) { + this.title = title; + } - public Integer getPrepTimeMinutes() { return prepTimeMinutes; } - public void setPrepTimeMinutes(Integer prepTimeMinutes) { this.prepTimeMinutes = prepTimeMinutes; } + public String getDescription() { + return description; + } - public Integer getCookTimeMinutes() { return cookTimeMinutes; } - public void setCookTimeMinutes(Integer cookTimeMinutes) { this.cookTimeMinutes = cookTimeMinutes; } + public void setDescription(String description) { + this.description = description; + } - public Integer getServings() { return servings; } - public void setServings(Integer servings) { this.servings = servings; } + public Integer getPrepTimeMinutes() { + return prepTimeMinutes; + } - public String getStatus() { return status; } - public void setStatus(String status) { this.status = status; } + public void setPrepTimeMinutes(Integer prepTimeMinutes) { + this.prepTimeMinutes = prepTimeMinutes; + } - public LocalDateTime getCreatedAt() { return createdAt; } - public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } + public Integer getCookTimeMinutes() { + return cookTimeMinutes; + } - public LocalDateTime getUpdatedAt() { return updatedAt; } - public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; } + public void setCookTimeMinutes(Integer cookTimeMinutes) { + this.cookTimeMinutes = cookTimeMinutes; + } - public Set getRecipeIngredients() { return recipeIngredients; } - public void setRecipeIngredients(Set recipeIngredients) { this.recipeIngredients = recipeIngredients; } + public Integer getServings() { + return servings; + } + + public void setServings(Integer servings) { + 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 getRecipeIngredients() { + return recipeIngredients; + } + + public void setRecipeIngredients(Set recipeIngredients) { + this.recipeIngredients = recipeIngredients; + } public Set getSteps() { return steps; @@ -173,7 +195,4 @@ public class Recipe { public void setUsers(Set users) { this.users = users; } - - - } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/entity/RecipeIngredient.java b/demo/src/main/java/com/example/demo/entity/RecipeIngredient.java index 3ff17c7..19eccd1 100644 --- a/demo/src/main/java/com/example/demo/entity/RecipeIngredient.java +++ b/demo/src/main/java/com/example/demo/entity/RecipeIngredient.java @@ -5,59 +5,88 @@ import java.math.BigDecimal; import lombok.EqualsAndHashCode; @Entity -@Table(name = "recipe_ingredient_junction", - uniqueConstraints = {@UniqueConstraint(columnNames = {"recipe_id", "ingredient_id"})}) -@EqualsAndHashCode( - onlyExplicitlyIncluded = true - ) +@Table(name = "recipe_ingredient_junction", uniqueConstraints = { + @UniqueConstraint(columnNames = { "recipe_id", "ingredient_id" }) }) +@EqualsAndHashCode(onlyExplicitlyIncluded = true) public class RecipeIngredient { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "recipe_id", nullable = false) - @EqualsAndHashCode.Include - private Recipe recipe; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "recipe_id", nullable = false) + @EqualsAndHashCode.Include + private Recipe recipe; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "ingredient_id", nullable = false) - @EqualsAndHashCode.Include - private Ingredient ingredient; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "ingredient_id", nullable = false) + @EqualsAndHashCode.Include + private Ingredient ingredient; - private BigDecimal quantity; + private BigDecimal quantity; - private String unit; + private String unit; - private String notes; + private String notes; - public RecipeIngredient() {} + public RecipeIngredient() { + } - public RecipeIngredient(Recipe recipe, Ingredient ingredient, BigDecimal quantity, String unit, String notes) { - this.recipe = recipe; - this.ingredient = ingredient; - this.quantity = quantity; - this.unit = unit; - this.notes = notes; - } + public RecipeIngredient(Recipe recipe, Ingredient ingredient, BigDecimal quantity, String unit, String notes) { + this.recipe = recipe; + this.ingredient = ingredient; + this.quantity = quantity; + this.unit = unit; + this.notes = notes; + } - // Getters and setters - public Integer getId() { return id; } - public void setId(Integer id) { this.id = id; } + // Getters and setters + public Integer getId() { + 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() { + return recipe; + } - public BigDecimal getQuantity() { return quantity; } - public void setQuantity(BigDecimal quantity) { this.quantity = quantity; } + public void setRecipe(Recipe recipe) { + this.recipe = recipe; + } - public String getUnit() { return unit; } - public void setUnit(String unit) { this.unit = unit; } + public Ingredient getIngredient() { + return ingredient; + } - public String getNotes() { return notes; } - public void setNotes(String notes) { this.notes = notes; } + 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; + } } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/entity/RecipeTagId.java b/demo/src/main/java/com/example/demo/entity/RecipeTagId.java index 2a5bd59..d0890bf 100644 --- a/demo/src/main/java/com/example/demo/entity/RecipeTagId.java +++ b/demo/src/main/java/com/example/demo/entity/RecipeTagId.java @@ -8,49 +8,50 @@ import java.util.Objects; @Embeddable public class RecipeTagId implements Serializable { - private static final long serialVersionUID = 5272431101708393749L; + private static final long serialVersionUID = 5272431101708393749L; @Column(name = "recipe_id") - private Integer recipeId; + private Integer recipeId; - @Column(name = "tag_id") - private Integer tagId; + @Column(name = "tag_id") + private Integer tagId; - public RecipeTagId() {} + public RecipeTagId() { + } - public RecipeTagId(Integer recipeId, Integer tagId) { - this.recipeId = recipeId; - this.tagId = tagId; - } + public RecipeTagId(Integer recipeId, Integer tagId) { + this.recipeId = recipeId; + this.tagId = tagId; + } - public Integer getRecipeId() { - return recipeId; - } + public Integer getRecipeId() { + return recipeId; + } - public void setRecipeId(Integer recipeId) { - this.recipeId = recipeId; - } + public void setRecipeId(Integer recipeId) { + this.recipeId = recipeId; + } - public Integer getTagId() { - return tagId; - } + public Integer getTagId() { + return tagId; + } - public void setTagId(Integer tagId) { - this.tagId = tagId; - } + public void setTagId(Integer tagId) { + this.tagId = tagId; + } - // REQUIRED for composite keys - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof RecipeTagId)) return false; - RecipeTagId that = (RecipeTagId) o; - return Objects.equals(recipeId, that.recipeId) && - Objects.equals(tagId, that.tagId); - } + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof RecipeTagId)) + return false; + RecipeTagId that = (RecipeTagId) o; + return Objects.equals(recipeId, that.recipeId) && Objects.equals(tagId, that.tagId); + } - @Override - public int hashCode() { - return Objects.hash(recipeId, tagId); - } + @Override + public int hashCode() { + return Objects.hash(recipeId, tagId); + } } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/entity/Step.java b/demo/src/main/java/com/example/demo/entity/Step.java index da6c90e..462494a 100644 --- a/demo/src/main/java/com/example/demo/entity/Step.java +++ b/demo/src/main/java/com/example/demo/entity/Step.java @@ -7,34 +7,40 @@ import lombok.EqualsAndHashCode; @Table(name = "steps") public class Step { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @Column(name = "step_number", nullable = false) - private Integer stepNumber; + @Column(name = "step_number", nullable = false) + private Integer stepNumber; - @Column(name = "instruction", nullable = false, columnDefinition = "TEXT") - private String instruction; + @Column(name = "instruction", nullable = false, columnDefinition = "TEXT") + private String instruction; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "recipe_id", nullable = false) - @EqualsAndHashCode.Include - private Recipe recipe; - - public Step() {} + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "recipe_id", nullable = false) + @EqualsAndHashCode.Include + private Recipe recipe; - public Step(Recipe recipe, Integer stepNumber, String instruction) { - this.recipe = recipe; - this.stepNumber = stepNumber; - this.instruction = instruction; - } + public Step() { + } - // Getters and setters - public Integer getId() { return id; } - public void setId(Integer id) { this.id = id; } + public Step(Recipe recipe, Integer stepNumber, String instruction) { + this.recipe = recipe; + this.stepNumber = stepNumber; + this.instruction = instruction; + } - public Recipe getRecipe() { + // Getters and setters + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Recipe getRecipe() { return recipe; } @@ -42,9 +48,19 @@ public class Step { this.recipe = recipe; } - public Integer getStepNumber() { return stepNumber; } - public void setStepNumber(Integer stepNumber) { this.stepNumber = stepNumber; } + public Integer getStepNumber() { + 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; + } } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/entity/Tag.java b/demo/src/main/java/com/example/demo/entity/Tag.java index 7d899df..987771c 100644 --- a/demo/src/main/java/com/example/demo/entity/Tag.java +++ b/demo/src/main/java/com/example/demo/entity/Tag.java @@ -9,40 +9,40 @@ import jakarta.persistence.*; @Table(name = "tags") public class Tag { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @Column(nullable = false, unique = true) - private String name; - - // Recipe is the manager for this relationship - @ManyToMany(fetch = FetchType.LAZY, mappedBy = "tags") - private Set recipes = new HashSet<>(); + @Column(nullable = false, unique = true) + private String name; - // Required by JPA - public Tag() {} + // Recipe is the manager for this relationship + @ManyToMany(fetch = FetchType.LAZY, mappedBy = "tags") + private Set recipes = new HashSet<>(); - public Tag(String name) { - this.name = name; - } + public Tag() { + } - // Getters and setters - public Integer getId() { - return id; - } + public Tag(String name) { + this.name = name; + } - public void setId(Integer id) { - this.id = id; - } + // Getters and setters + public Integer getId() { + return id; + } - public String getName() { - return name; - } + public void setId(Integer id) { + this.id = id; + } - public void setName(String name) { - this.name = name; - } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } public Set getRecipes() { return recipes; @@ -51,6 +51,5 @@ public class Tag { public void setRecipes(Set recipes) { this.recipes = recipes; } - - + } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/entity/User.java b/demo/src/main/java/com/example/demo/entity/User.java index 3eca2fc..7d2b671 100644 --- a/demo/src/main/java/com/example/demo/entity/User.java +++ b/demo/src/main/java/com/example/demo/entity/User.java @@ -20,63 +20,91 @@ import java.util.Set; @Table(name = "users") public class User { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; - @Column(nullable = false, unique = true) - private String username; + @Column(nullable = false, unique = true) + private String username; - private String role; + private String role; - @Column(unique = true) - private String email; + @Column(unique = true) + private String email; - private String hashedpassword; + private String hashedpassword; - @Column(name = "created_at") - private LocalDateTime createdAt; - - // User Recipe relationship - @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) - private Set recipes = new HashSet<>(); - - // Favorite relationship and also junction table - @ManyToMany(fetch = FetchType.LAZY) - @JoinTable(name = "favorites", - joinColumns = {@JoinColumn(name = "userId")}, - inverseJoinColumns = {@JoinColumn(name = "recipeId")}) - private Set FavRecipes = new HashSet<>(); + @Column(name = "created_at") + private LocalDateTime createdAt; - // Constructors - public User() {} + // User Recipe relationship + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) + private Set recipes = new HashSet<>(); - public User(String username, String role, String email, String hashedpassword, LocalDateTime createdAt) { - this.username = username; - this.role = role; - this.email = email; - this.hashedpassword = hashedpassword; - this.createdAt = createdAt; - } + // Favorite relationship and also junction table + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable(name = "favorites", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { + @JoinColumn(name = "recipeId") }) + private Set FavRecipes = new HashSet<>(); - - public Integer getId() { return id; } - public void setId(Integer id) { this.id = id; } + public User() { + } - public String getUsername() { return username; } - public void setUsername(String username) { this.username = username; } + public User(String username, String role, String email, String hashedpassword, LocalDateTime createdAt) { + this.username = username; + this.role = role; + this.email = email; + this.hashedpassword = hashedpassword; + this.createdAt = createdAt; + } - public String getRole() { return role; } - public void setRole(String role) { this.role = role; } + public Integer getId() { + return id; + } - public String getEmail() { return email; } - public void setEmail(String email) { this.email = email; } + public void setId(Integer id) { + this.id = id; + } - public String getHashedpassword() { return hashedpassword; } - public void setHashedpassword(String hashedpassword) { this.hashedpassword = hashedpassword; } + public String getUsername() { + return username; + } - public LocalDateTime getCreatedAt() { return createdAt; } - public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } + public void setUsername(String username) { + this.username = username; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public String getEmail() { + 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 getFavRecipes() { return FavRecipes; @@ -85,6 +113,5 @@ public class User { public void setFavRecipes(Set favRecipes) { FavRecipes = favRecipes; } - - + } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/exception/NotFoundException.java b/demo/src/main/java/com/example/demo/exception/NotFoundException.java index 6791fa1..66b3240 100644 --- a/demo/src/main/java/com/example/demo/exception/NotFoundException.java +++ b/demo/src/main/java/com/example/demo/exception/NotFoundException.java @@ -3,14 +3,14 @@ package com.example.demo.exception; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; -@ResponseStatus (value = HttpStatus.NOT_FOUND) -public class NotFoundException extends RuntimeException{ - +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class NotFoundException extends RuntimeException { + private static final long serialVersionUID = 1l; private String resourceName; private String fieldName; private Object fieldValue; - + public NotFoundException(String resourceName, String fieldName, Object fieldValue) { super(String.format("%s not found with %s : %s", resourceName, fieldName, fieldValue)); this.resourceName = resourceName; @@ -26,7 +26,6 @@ public class NotFoundException extends RuntimeException{ return fieldName; } - public Object getFieldValue() { return fieldValue; } diff --git a/demo/src/main/java/com/example/demo/repository/FavoriteRepo.java b/demo/src/main/java/com/example/demo/repository/FavoriteRepo.java index 86bed8e..678c97b 100644 --- a/demo/src/main/java/com/example/demo/repository/FavoriteRepo.java +++ b/demo/src/main/java/com/example/demo/repository/FavoriteRepo.java @@ -7,5 +7,5 @@ import com.example.demo.entity.FavoriteId; import java.util.List; public interface FavoriteRepo extends JpaRepository { - + } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/repository/ImageRepo.java b/demo/src/main/java/com/example/demo/repository/ImageRepo.java index 9152699..0e36e6e 100644 --- a/demo/src/main/java/com/example/demo/repository/ImageRepo.java +++ b/demo/src/main/java/com/example/demo/repository/ImageRepo.java @@ -5,7 +5,6 @@ import com.example.demo.entity.Image; import java.util.List; - public interface ImageRepo extends JpaRepository { - + } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/repository/IngredientRepo.java b/demo/src/main/java/com/example/demo/repository/IngredientRepo.java index 7f0e379..a45192a 100644 --- a/demo/src/main/java/com/example/demo/repository/IngredientRepo.java +++ b/demo/src/main/java/com/example/demo/repository/IngredientRepo.java @@ -5,7 +5,6 @@ import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.Ingredient; - public interface IngredientRepo extends JpaRepository { Optional findByName(String name); } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java b/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java index 4eefe46..e121dc5 100644 --- a/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java +++ b/demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java @@ -6,6 +6,6 @@ import com.example.demo.entity.Recipe; import com.example.demo.entity.RecipeIngredient; public interface RecipeIngredientRepo extends JpaRepository { - // Custom query: find all ingredients for a recipe + // Custom query: find all ingredients for a recipe void deleteByRecipe(Recipe recipe); } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/repository/RecipeRepo.java b/demo/src/main/java/com/example/demo/repository/RecipeRepo.java index 973fed4..e04744b 100644 --- a/demo/src/main/java/com/example/demo/repository/RecipeRepo.java +++ b/demo/src/main/java/com/example/demo/repository/RecipeRepo.java @@ -3,7 +3,6 @@ package com.example.demo.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.Recipe; - public interface RecipeRepo extends JpaRepository { } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/repository/RecipeTagRepo.java b/demo/src/main/java/com/example/demo/repository/RecipeTagRepo.java index 86fc59e..841230f 100644 --- a/demo/src/main/java/com/example/demo/repository/RecipeTagRepo.java +++ b/demo/src/main/java/com/example/demo/repository/RecipeTagRepo.java @@ -1,6 +1,5 @@ package com.example.demo.repository; - import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.RecipeTag; import com.example.demo.entity.RecipeTagId; diff --git a/demo/src/main/java/com/example/demo/repository/StepRepo.java b/demo/src/main/java/com/example/demo/repository/StepRepo.java index eb1486b..7bc8ee4 100644 --- a/demo/src/main/java/com/example/demo/repository/StepRepo.java +++ b/demo/src/main/java/com/example/demo/repository/StepRepo.java @@ -8,5 +8,4 @@ import java.util.List; public interface StepRepo extends JpaRepository { - } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/repository/UserRepo.java b/demo/src/main/java/com/example/demo/repository/UserRepo.java index 3e8949f..7dc9f3e 100644 --- a/demo/src/main/java/com/example/demo/repository/UserRepo.java +++ b/demo/src/main/java/com/example/demo/repository/UserRepo.java @@ -5,5 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.User; public interface UserRepo extends JpaRepository { - + } \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java b/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java index 3d8fa8e..57bb65b 100644 --- a/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java +++ b/demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java @@ -31,18 +31,19 @@ import com.example.demo.service.RecipeService; import jakarta.transaction.Transactional; @Service -public class RecipeServiceImpl implements RecipeService{ +public class RecipeServiceImpl implements RecipeService { - -private RecipeRepo recipeRepository; -private IngredientRepo ingredientRepository; -private RecipeIngredientRepo recipeIngredientRepository; -private UserRepo userRepository; -private StepRepo stepRepository; -private ImageRepo imageRepository; -private TagRepo tagRepository; - - public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository, RecipeIngredientRepo recipeIngredientRepository, UserRepo userRepository, StepRepo stepRepository, ImageRepo imageRepository, TagRepo tagRepository) { + private RecipeRepo recipeRepository; + private IngredientRepo ingredientRepository; + private RecipeIngredientRepo recipeIngredientRepository; + private UserRepo userRepository; + private StepRepo stepRepository; + private ImageRepo imageRepository; + private TagRepo tagRepository; + + public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository, + RecipeIngredientRepo recipeIngredientRepository, UserRepo userRepository, StepRepo stepRepository, + ImageRepo imageRepository, TagRepo tagRepository) { super(); this.recipeRepository = recipeRepository; this.ingredientRepository = ingredientRepository; @@ -52,335 +53,267 @@ private TagRepo tagRepository; this.imageRepository = imageRepository; this.tagRepository = tagRepository; } - + public RecipeDto convertToDto(Recipe recipe) { - List ingredientDtos = recipe.getRecipeIngredients().stream() - .map(ri -> new RecipeIngredientDto( - ri.getIngredient().getName(), - ri.getQuantity(), - ri.getUnit(), - ri.getNotes() - )) - .toList(); - - List stepDtos = recipe.getSteps().stream() - .map(ri -> new StepDto( - ri.getStepNumber(), - ri.getInstruction() - )) - .toList(); - - List imageDtos = recipe.getImages().stream() - .map(ri -> new ImageDto( - ri.getImageUrl() - )) - .toList(); - - List tagDtos = recipe.getTags().stream() - .map(ri -> new TagDto( - ri.getName() - )) - .toList(); - - UserDto userDto = new UserDto(recipe.getUser().getId(), recipe.getUser().getUsername(), recipe.getUser().getEmail()); - - return new RecipeDto( - recipe.getTitle(), - recipe.getDescription(), - recipe.getPrepTimeMinutes(), - recipe.getCookTimeMinutes(), - recipe.getServings(), - userDto, - recipe.getStatus(), - ingredientDtos, - stepDtos, - imageDtos, - tagDtos - ); + List ingredientDtos = recipe.getRecipeIngredients().stream() + .map(ri -> new RecipeIngredientDto(ri.getIngredient().getName(), ri.getQuantity(), ri.getUnit(), + ri.getNotes())) + .toList(); + + List stepDtos = recipe.getSteps().stream() + .map(ri -> new StepDto(ri.getStepNumber(), ri.getInstruction())).toList(); + + List imageDtos = recipe.getImages().stream().map(ri -> new ImageDto(ri.getImageUrl())).toList(); + + List tagDtos = recipe.getTags().stream().map(ri -> new TagDto(ri.getName())).toList(); + + UserDto userDto = new UserDto(recipe.getUser().getId(), recipe.getUser().getUsername(), + recipe.getUser().getEmail()); + + return new RecipeDto(recipe.getTitle(), recipe.getDescription(), recipe.getPrepTimeMinutes(), + recipe.getCookTimeMinutes(), recipe.getServings(), userDto, recipe.getStatus(), ingredientDtos, + stepDtos, imageDtos, tagDtos); } @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())); + .orElseThrow(() -> new NotFoundException("User", "id", dto.getUserDto().getId())); - Recipe recipe = new Recipe( - dto.getTitle(), - dto.getDescription(), - dto.getPrepTimeMinutes(), - dto.getCookTimeMinutes(), - dto.getServings(), - user, - dto.getStatus() - ); + Recipe recipe = new Recipe(dto.getTitle(), 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()) - .orElseGet(() -> new Ingredient(riDto.getIngredientName())); + Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName()) + .orElseGet(() -> new Ingredient(riDto.getIngredientName())); - - if (ingredient.getId() == null) { - ingredientRepository.save(ingredient); - } + if (ingredient.getId() == null) { + ingredientRepository.save(ingredient); + } - RecipeIngredient ri = new RecipeIngredient( - recipe, - ingredient, - riDto.getQuantity(), - riDto.getUnit(), - riDto.getNotes() - ); + RecipeIngredient ri = new RecipeIngredient(recipe, ingredient, riDto.getQuantity(), riDto.getUnit(), + riDto.getNotes()); - recipe.getRecipeIngredients().add(ri); - } - - if (dto.getSteps() != null) { - for (StepDto stepDto : dto.getSteps()) { - Step step = new Step(recipe, stepDto.getStepNumber(), stepDto.getInstruction()); - recipe.getSteps().add(step); - } - } - - if (dto.getImages() != null) { - for (ImageDto imageDto : dto.getImages()) { - Image image = new Image(recipe, imageDto.getImageUrl()); - recipe.getImages().add(image); - } - } - -// 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()) { + recipe.getRecipeIngredients().add(ri); + } - - Tag tag = tagRepository.findByName(tDto.getName()) - .orElseGet(() -> new Tag(tDto.getName())); + if (dto.getSteps() != null) { + for (StepDto stepDto : dto.getSteps()) { + Step step = new Step(recipe, stepDto.getStepNumber(), stepDto.getInstruction()); + recipe.getSteps().add(step); + } + } - - if (tag.getId() == null) { - tagRepository.save(tag); - } - recipe.getTags().add(tag); - } - - Recipe saved = recipeRepository.save(recipe); - - return convertToDto(saved); - } + if (dto.getImages() != null) { + for (ImageDto imageDto : dto.getImages()) { + Image image = new Image(recipe, imageDto.getImageUrl()); + recipe.getImages().add(image); + } + } + + for (TagDto tDto : dto.getTags()) { + + Tag tag = tagRepository.findByName(tDto.getName()).orElseGet(() -> new Tag(tDto.getName())); + + if (tag.getId() == null) { + tagRepository.save(tag); + } + recipe.getTags().add(tag); + } + + Recipe saved = recipeRepository.save(recipe); + + return convertToDto(saved); + } @Override @Transactional public List getAllRecipes() { - + List list = new ArrayList<>(); - for (Recipe recipe : recipeRepository.findAll()) { - RecipeDto recipeDto = convertToDto(recipe); - list.add(recipeDto); - } - - return list; + for (Recipe recipe : recipeRepository.findAll()) { + RecipeDto recipeDto = convertToDto(recipe); + list.add(recipeDto); + } + + return list; } @Override @Transactional public RecipeDto getRecipeById(Integer Id) { - return convertToDto(recipeRepository.findById(Id).orElseThrow(() -> - new NotFoundException("Recipe", "id", Id))); + return convertToDto(recipeRepository.findById(Id).orElseThrow(() -> new NotFoundException("Recipe", "id", Id))); } @Override @Transactional public RecipeDto updateRecipe(RecipeDto recipeDto, Integer id) { - Recipe existingRecipe = recipeRepository.findById(id) - .orElseThrow(() -> new NotFoundException("Recipe", "id", id)); + Recipe existingRecipe = recipeRepository.findById(id) + .orElseThrow(() -> new NotFoundException("Recipe", "id", id)); - existingRecipe.setTitle(recipeDto.getTitle()); - existingRecipe.setDescription(recipeDto.getDescription()); - existingRecipe.setPrepTimeMinutes(recipeDto.getPrepTimeMinutes()); - existingRecipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes()); - existingRecipe.setServings(recipeDto.getServings()); - existingRecipe.setStatus(recipeDto.getStatus()); - + existingRecipe.setTitle(recipeDto.getTitle()); + existingRecipe.setDescription(recipeDto.getDescription()); + existingRecipe.setPrepTimeMinutes(recipeDto.getPrepTimeMinutes()); + existingRecipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes()); + existingRecipe.setServings(recipeDto.getServings()); + existingRecipe.setStatus(recipeDto.getStatus()); - List updatedIngredients = recipeDto.getIngredients(); - List ingredientsToRemove = new ArrayList<>(); - - List updatedSteps = recipeDto.getSteps(); - List stepsToRemove = new ArrayList<>(); - - List updatedImages = recipeDto.getImages(); - List imagesToRemove = new ArrayList<>(); - - List updatedTags = recipeDto.getTags(); - List tagsToRemove = new ArrayList<>(); + List updatedIngredients = recipeDto.getIngredients(); + List ingredientsToRemove = new ArrayList<>(); - for (RecipeIngredient ri : existingRecipe.getRecipeIngredients()) { + List updatedSteps = recipeDto.getSteps(); + List stepsToRemove = new ArrayList<>(); - boolean existsInUpdatedList = false; - for (RecipeIngredientDto dto : updatedIngredients) { - String updatedName = dto.getIngredientName(); - String existingName = ri.getIngredient().getName(); + List updatedImages = recipeDto.getImages(); + List imagesToRemove = new ArrayList<>(); - if (updatedName.equals(existingName)) { - existsInUpdatedList = true; - break; - } - } + List updatedTags = recipeDto.getTags(); + List tagsToRemove = new ArrayList<>(); - if (!existsInUpdatedList) { - ingredientsToRemove.add(ri); - } - } - - existingRecipe.getRecipeIngredients().removeAll(ingredientsToRemove); + for (RecipeIngredient ri : existingRecipe.getRecipeIngredients()) { - for (RecipeIngredientDto riDto : updatedIngredients) { - - // go through the old list of ingredients until we find a match with updated list - RecipeIngredient existingRI = existingRecipe.getRecipeIngredients().stream() - .filter(ri -> ri.getIngredient().getName().equals(riDto.getIngredientName())) - .findFirst() - .orElse(null); - - - // if old ingredient just update parameters - if (existingRI != null) { - - existingRI.setQuantity(riDto.getQuantity()); - existingRI.setUnit(riDto.getUnit()); - existingRI.setNotes(riDto.getNotes()); - } - - // if new ingredient, have to make a whole new thing - else { - - Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName()) - .orElseGet(() -> new Ingredient(riDto.getIngredientName())); + boolean existsInUpdatedList = false; + for (RecipeIngredientDto dto : updatedIngredients) { + String updatedName = dto.getIngredientName(); + String existingName = ri.getIngredient().getName(); - if (ingredient.getId() == null) { - ingredientRepository.save(ingredient); - } + if (updatedName.equals(existingName)) { + existsInUpdatedList = true; + break; + } + } - RecipeIngredient newRI = new RecipeIngredient( - existingRecipe, - ingredient, - riDto.getQuantity(), - riDto.getUnit(), - riDto.getNotes() - ); + if (!existsInUpdatedList) { + ingredientsToRemove.add(ri); + } + } - existingRecipe.getRecipeIngredients().add(newRI); - } - } - - if(updatedSteps != null) { - // find steps that weren't included - for (Step step : existingRecipe.getSteps()) { - boolean existsInUpdatedList = updatedSteps.stream() - .anyMatch(dto -> dto.getStepNumber().equals(step.getStepNumber())); - - if (!existsInUpdatedList) stepsToRemove.add(step); - } - // delete those steps - existingRecipe.getSteps().removeAll(stepsToRemove); - - // go through updated steps - for (StepDto stepDto : updatedSteps) { - - // find matching step by step number - Step existingStep = existingRecipe.getSteps().stream() - .filter(s -> s.getStepNumber().equals(stepDto.getStepNumber())) - .findFirst() - .orElse(null); - - // if there's a match update the instruction string - if (existingStep != null) { - existingStep.setInstruction(stepDto.getInstruction()); - } - - // if no match then make a whole new step - else { - Step newStep = new Step(existingRecipe, stepDto.getStepNumber(), stepDto.getInstruction()); - existingRecipe.getSteps().add(newStep); - } - } - } - - //same process as above just with images instead - if (updatedImages != null) { - for (Image image : existingRecipe.getImages()) { - boolean existsInUpdatedList = updatedImages.stream() - .anyMatch(dto -> dto.getImageUrl().equals(image.getImageUrl())); - if (!existsInUpdatedList) imagesToRemove.add(image); - } + existingRecipe.getRecipeIngredients().removeAll(ingredientsToRemove); - existingRecipe.getImages().removeAll(imagesToRemove); + for (RecipeIngredientDto riDto : updatedIngredients) { - for (ImageDto imageDto : updatedImages) { - Image existingImage = existingRecipe.getImages().stream() - .filter(img -> img.getImageUrl().equals(imageDto.getImageUrl())) - .findFirst() - .orElse(null); + // go through the old list of ingredients until we find a match with updated + // list + RecipeIngredient existingRI = existingRecipe.getRecipeIngredients().stream() + .filter(ri -> ri.getIngredient().getName().equals(riDto.getIngredientName())).findFirst() + .orElse(null); - if (existingImage != null) { - existingImage.setImageUrl(imageDto.getImageUrl()); - } - else { - Image newImage = new Image(existingRecipe, imageDto.getImageUrl()); - existingRecipe.getImages().add(newImage); - } - } - } - - - - //same process as above just with tags instead, except for saving the tag - // since the relationship for this one was slightly different - if (updatedTags != null) { - for (Tag tag : existingRecipe.getTags()) { - boolean existsInUpdatedList = updatedTags.stream() - .anyMatch(dto -> dto.getName().equals(tag.getName())); - if (!existsInUpdatedList) tagsToRemove.add(tag); - } + // if old ingredient just update parameters + if (existingRI != null) { - existingRecipe.getTags().removeAll(tagsToRemove); + existingRI.setQuantity(riDto.getQuantity()); + existingRI.setUnit(riDto.getUnit()); + existingRI.setNotes(riDto.getNotes()); + } - for (TagDto tagDto : updatedTags) { - Tag existingTag = existingRecipe.getTags().stream() - .filter(tag -> tag.getName().equals(tagDto.getName())) - .findFirst() - .orElse(null); + // if new ingredient, have to make a whole new thing + else { - if (existingTag != null) { - existingTag.setName(tagDto.getName()); - } - else { - Tag newTag = tagRepository.findByName(tagDto.getName()) - .orElseGet(() -> tagRepository.save(new Tag(tagDto.getName()))); + Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName()) + .orElseGet(() -> new Ingredient(riDto.getIngredientName())); - existingRecipe.getTags().add(newTag); - } - } - } - recipeRepository.save(existingRecipe); - return convertToDto(existingRecipe); + if (ingredient.getId() == null) { + ingredientRepository.save(ingredient); + } + + RecipeIngredient newRI = new RecipeIngredient(existingRecipe, ingredient, riDto.getQuantity(), + riDto.getUnit(), riDto.getNotes()); + + existingRecipe.getRecipeIngredients().add(newRI); + } + } + + if (updatedSteps != null) { + // find steps that weren't included + for (Step step : existingRecipe.getSteps()) { + boolean existsInUpdatedList = updatedSteps.stream() + .anyMatch(dto -> dto.getStepNumber().equals(step.getStepNumber())); + + if (!existsInUpdatedList) + stepsToRemove.add(step); + } + // delete those steps + existingRecipe.getSteps().removeAll(stepsToRemove); + + // go through updated steps + for (StepDto stepDto : updatedSteps) { + + // find matching step by step number + Step existingStep = existingRecipe.getSteps().stream() + .filter(s -> s.getStepNumber().equals(stepDto.getStepNumber())).findFirst().orElse(null); + + // if there's a match update the instruction string + if (existingStep != null) { + existingStep.setInstruction(stepDto.getInstruction()); + } + + // if no match then make a whole new step + else { + Step newStep = new Step(existingRecipe, stepDto.getStepNumber(), stepDto.getInstruction()); + existingRecipe.getSteps().add(newStep); + } + } + } + + // same process as above just with images instead + if (updatedImages != null) { + for (Image image : existingRecipe.getImages()) { + boolean existsInUpdatedList = updatedImages.stream() + .anyMatch(dto -> dto.getImageUrl().equals(image.getImageUrl())); + if (!existsInUpdatedList) + imagesToRemove.add(image); + } + + existingRecipe.getImages().removeAll(imagesToRemove); + + for (ImageDto imageDto : updatedImages) { + Image existingImage = existingRecipe.getImages().stream() + .filter(img -> img.getImageUrl().equals(imageDto.getImageUrl())).findFirst().orElse(null); + + if (existingImage != null) { + existingImage.setImageUrl(imageDto.getImageUrl()); + } else { + Image newImage = new Image(existingRecipe, imageDto.getImageUrl()); + existingRecipe.getImages().add(newImage); + } + } + } + + // same process as above just with tags instead, except for saving the tag + // since the relationship for this one was slightly different + if (updatedTags != null) { + for (Tag tag : existingRecipe.getTags()) { + boolean existsInUpdatedList = updatedTags.stream().anyMatch(dto -> dto.getName().equals(tag.getName())); + if (!existsInUpdatedList) + tagsToRemove.add(tag); + } + + existingRecipe.getTags().removeAll(tagsToRemove); + + for (TagDto tagDto : updatedTags) { + Tag existingTag = existingRecipe.getTags().stream() + .filter(tag -> tag.getName().equals(tagDto.getName())).findFirst().orElse(null); + + if (existingTag != null) { + existingTag.setName(tagDto.getName()); + } else { + Tag newTag = tagRepository.findByName(tagDto.getName()) + .orElseGet(() -> tagRepository.save(new Tag(tagDto.getName()))); + + existingRecipe.getTags().add(newTag); + } + } + } + recipeRepository.save(existingRecipe); + return convertToDto(existingRecipe); } @Override public void deleteRecipe(Integer Id) { - recipeRepository.findById(Id) - .orElseThrow(() -> new NotFoundException("Recipe", "id", Id)); + recipeRepository.findById(Id).orElseThrow(() -> new NotFoundException("Recipe", "id", Id)); recipeRepository.deleteById(Id); - + } } diff --git a/demo/src/main/java/com/example/demo/service/Impl/UserServiceImpl.java b/demo/src/main/java/com/example/demo/service/Impl/UserServiceImpl.java index d3cef4c..498f93e 100644 --- a/demo/src/main/java/com/example/demo/service/Impl/UserServiceImpl.java +++ b/demo/src/main/java/com/example/demo/service/Impl/UserServiceImpl.java @@ -22,25 +22,21 @@ import com.example.demo.service.UserService; import jakarta.transaction.Transactional; @Service -public class UserServiceImpl implements UserService{ - +public class UserServiceImpl implements UserService { + private UserRepo userRepository; private RecipeRepo recipeRepository; - + public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository) { super(); this.userRepository = userRepository; this.recipeRepository = recipeRepository; } - + public UserDto convertToDto(User user) { - return new UserDto( - user.getId(), - user.getUsername(), - user.getEmail() - ); + return new UserDto(user.getId(), user.getUsername(), user.getEmail()); } - + @Override public User saveUser(User user) { return userRepository.save(user); @@ -48,74 +44,68 @@ public class UserServiceImpl implements UserService{ @Override public List getAllUsers() { - + List list = new ArrayList<>(); - for (User user : userRepository.findAll()) { - UserDto userDto = convertToDto(user); - list.add(userDto); - } - - return list; + for (User user : userRepository.findAll()) { + UserDto userDto = convertToDto(user); + list.add(userDto); + } + + return list; } @Override public UserDto getUserById(Integer Id) { - - return convertToDto(userRepository.findById(Id).orElseThrow(() -> - new NotFoundException("User", "id", Id))); + + return convertToDto(userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id))); } - + @Override @Transactional public UserDto saveFavorite(Integer userId, Integer recipeId) { - User existingUser = userRepository.findById(userId).orElseThrow( - () -> new NotFoundException("User", "id", userId)); - - Recipe existingRecipe = recipeRepository.findById(recipeId).orElseThrow( - () -> new NotFoundException("Recipe", "id", recipeId)); - + User existingUser = userRepository.findById(userId) + .orElseThrow(() -> new NotFoundException("User", "id", userId)); + + Recipe existingRecipe = recipeRepository.findById(recipeId) + .orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId)); + existingUser.getFavRecipes().add(existingRecipe); - userRepository.save(existingUser); - - + userRepository.save(existingUser); + return convertToDto(existingUser); } @Override public UserDto updateUser(User user, Integer Id) { - - User existingUser = userRepository.findById(Id).orElseThrow( - () -> new NotFoundException("User", "id", Id)); - + + User existingUser = userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id)); + existingUser.setUsername(user.getUsername()); existingUser.setEmail(user.getEmail()); - + userRepository.save(existingUser); - + return convertToDto(existingUser); } @Override public void deleteUser(Integer Id) { - userRepository.findById(Id).orElseThrow( - () -> new NotFoundException("User", "id", Id)); + userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id)); userRepository.deleteById(Id); } @Override @Transactional public void deleteFavorite(Integer userId, Integer recipeId) { - User existingUser = userRepository.findById(userId).orElseThrow( - () -> new NotFoundException("User", "id", userId)); - - Recipe existingRecipe = recipeRepository.findById(recipeId).orElseThrow( - () -> new NotFoundException("Recipe", "id", recipeId)); + User existingUser = userRepository.findById(userId) + .orElseThrow(() -> new NotFoundException("User", "id", userId)); + + Recipe existingRecipe = recipeRepository.findById(recipeId) + .orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId)); userRepository.save(existingUser); - + existingUser.getFavRecipes().remove(existingRecipe); - + } - - } diff --git a/demo/src/main/java/com/example/demo/service/RecipeService.java b/demo/src/main/java/com/example/demo/service/RecipeService.java index df91ae6..b728cf8 100644 --- a/demo/src/main/java/com/example/demo/service/RecipeService.java +++ b/demo/src/main/java/com/example/demo/service/RecipeService.java @@ -9,11 +9,16 @@ import com.example.demo.entity.Recipe; import com.example.demo.entity.User; public interface RecipeService { - RecipeDto convertToDto(Recipe recipe) ; + RecipeDto convertToDto(Recipe recipe); + RecipeDto saveRecipe(RecipeDto recipe); + List getAllRecipes(); + RecipeDto getRecipeById(Integer recipeId); + RecipeDto updateRecipe(RecipeDto recipedto, Integer Id); + void deleteRecipe(Integer Id); - + } diff --git a/demo/src/main/java/com/example/demo/service/UserService.java b/demo/src/main/java/com/example/demo/service/UserService.java index d4de0ea..1eef88a 100644 --- a/demo/src/main/java/com/example/demo/service/UserService.java +++ b/demo/src/main/java/com/example/demo/service/UserService.java @@ -7,11 +7,18 @@ import com.example.demo.entity.User; public interface UserService { UserDto convertToDto(User user); + User saveUser(User user); + List getAllUsers(); + UserDto getUserById(Integer Id); + UserDto saveFavorite(Integer userId, Integer recipeId); + UserDto updateUser(User user, Integer Id); + void deleteUser(Integer Id); + void deleteFavorite(Integer userId, Integer recipeId); }