mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
Update 4 files
- /demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java - /demo/src/main/java/com/example/demo/dto/RecipeDto.java - /demo/src/main/java/com/example/demo/repository/IngredientRepo.java - /demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java
This commit is contained in:
@@ -12,6 +12,7 @@ public class RecipeDto {
|
|||||||
private Integer servings;
|
private Integer servings;
|
||||||
private UserDto userDto;
|
private UserDto userDto;
|
||||||
private String status;
|
private String status;
|
||||||
|
private Integer id;
|
||||||
private List<RecipeIngredientDto> ingredients;
|
private List<RecipeIngredientDto> ingredients;
|
||||||
private List<StepDto> steps;
|
private List<StepDto> steps;
|
||||||
private List<ImageDto> images;
|
private List<ImageDto> images;
|
||||||
@@ -91,6 +92,10 @@ public class RecipeDto {
|
|||||||
public List<RecipeIngredientDto> getIngredients() {
|
public List<RecipeIngredientDto> getIngredients() {
|
||||||
return ingredients;
|
return ingredients;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
public void setIngredients(List<RecipeIngredientDto> ingredients) {
|
public void setIngredients(List<RecipeIngredientDto> ingredients) {
|
||||||
this.ingredients = ingredients;
|
this.ingredients = ingredients;
|
||||||
@@ -127,5 +132,8 @@ public class RecipeDto {
|
|||||||
public void setUserDto(UserDto userDto) {
|
public void setUserDto(UserDto userDto) {
|
||||||
this.userDto = userDto;
|
this.userDto = userDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ 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> findByNameIgnoreCase(String name);
|
||||||
}
|
}
|
||||||
@@ -7,5 +7,6 @@ import com.example.demo.entity.RecipeIngredient;
|
|||||||
|
|
||||||
public interface RecipeIngredientRepo extends JpaRepository<RecipeIngredient, Integer> {
|
public interface RecipeIngredientRepo extends JpaRepository<RecipeIngredient, Integer> {
|
||||||
// Custom query: find all ingredients for a recipe
|
// Custom query: find all ingredients for a recipe
|
||||||
|
List<RecipeIngredient> findByRecipeId(Integer recipeId);
|
||||||
void deleteByRecipe(Recipe recipe);
|
void deleteByRecipe(Recipe recipe);
|
||||||
}
|
}
|
||||||
@@ -1,319 +1,288 @@
|
|||||||
package com.example.demo.service.Impl;
|
package com.example.demo.service.Impl;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.example.demo.dto.RecipeDto;
|
import com.example.demo.dto.RecipeDto;
|
||||||
import com.example.demo.dto.UserDto;
|
|
||||||
import com.example.demo.dto.StepDto;
|
|
||||||
import com.example.demo.dto.TagDto;
|
|
||||||
import com.example.demo.dto.ImageDto;
|
|
||||||
import com.example.demo.dto.RecipeIngredientDto;
|
import com.example.demo.dto.RecipeIngredientDto;
|
||||||
import com.example.demo.entity.Image;
|
import com.example.demo.dto.StepDto;
|
||||||
import com.example.demo.entity.Ingredient;
|
import com.example.demo.dto.UserDto;
|
||||||
import com.example.demo.entity.Recipe;
|
import com.example.demo.entity.*;
|
||||||
import com.example.demo.entity.RecipeIngredient;
|
|
||||||
import com.example.demo.entity.Step;
|
|
||||||
import com.example.demo.entity.Tag;
|
|
||||||
import com.example.demo.entity.User;
|
|
||||||
import com.example.demo.exception.NotFoundException;
|
import com.example.demo.exception.NotFoundException;
|
||||||
import com.example.demo.repository.ImageRepo;
|
import com.example.demo.repository.*;
|
||||||
import com.example.demo.repository.IngredientRepo;
|
|
||||||
import com.example.demo.repository.RecipeIngredientRepo;
|
|
||||||
import com.example.demo.repository.RecipeRepo;
|
|
||||||
import com.example.demo.repository.StepRepo;
|
|
||||||
import com.example.demo.repository.TagRepo;
|
|
||||||
import com.example.demo.repository.UserRepo;
|
|
||||||
import com.example.demo.service.RecipeService;
|
import com.example.demo.service.RecipeService;
|
||||||
|
|
||||||
import jakarta.transaction.Transactional;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class RecipeServiceImpl implements RecipeService {
|
public class RecipeServiceImpl implements RecipeService {
|
||||||
|
|
||||||
private RecipeRepo recipeRepository;
|
private final RecipeRepo recipeRepo;
|
||||||
private IngredientRepo ingredientRepository;
|
private final UserRepo userRepo;
|
||||||
private RecipeIngredientRepo recipeIngredientRepository;
|
private final IngredientRepo ingredientRepo;
|
||||||
private UserRepo userRepository;
|
private final StepRepo stepRepo;
|
||||||
private StepRepo stepRepository;
|
private final RecipeIngredientRepo recipeIngredientRepo;
|
||||||
private ImageRepo imageRepository;
|
|
||||||
private TagRepo tagRepository;
|
|
||||||
|
|
||||||
public RecipeServiceImpl(RecipeRepo recipeRepository, IngredientRepo ingredientRepository,
|
// DEV DEFAULT — must exist in DB
|
||||||
RecipeIngredientRepo recipeIngredientRepository, UserRepo userRepository, StepRepo stepRepository,
|
private static final int DEFAULT_USER_ID = 1;
|
||||||
ImageRepo imageRepository, TagRepo tagRepository) {
|
|
||||||
super();
|
|
||||||
this.recipeRepository = recipeRepository;
|
|
||||||
this.ingredientRepository = ingredientRepository;
|
|
||||||
this.recipeIngredientRepository = recipeIngredientRepository;
|
|
||||||
this.userRepository = userRepository;
|
|
||||||
this.stepRepository = stepRepository;
|
|
||||||
this.imageRepository = imageRepository;
|
|
||||||
this.tagRepository = tagRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RecipeDto convertToDto(Recipe recipe) {
|
public RecipeServiceImpl(
|
||||||
List<RecipeIngredientDto> ingredientDtos = recipe.getRecipeIngredients().stream()
|
RecipeRepo recipeRepo,
|
||||||
.map(ri -> new RecipeIngredientDto(ri.getIngredient().getName(), ri.getQuantity(), ri.getUnit(),
|
UserRepo userRepo,
|
||||||
ri.getNotes()))
|
IngredientRepo ingredientRepo,
|
||||||
.toList();
|
StepRepo stepRepo,
|
||||||
|
RecipeIngredientRepo recipeIngredientRepo
|
||||||
|
) {
|
||||||
|
this.recipeRepo = recipeRepo;
|
||||||
|
this.userRepo = userRepo;
|
||||||
|
this.ingredientRepo = ingredientRepo;
|
||||||
|
this.stepRepo = stepRepo;
|
||||||
|
this.recipeIngredientRepo = recipeIngredientRepo;
|
||||||
|
}
|
||||||
|
|
||||||
List<StepDto> stepDtos = recipe.getSteps().stream()
|
// -------------------- convertToDto --------------------
|
||||||
.map(ri -> new StepDto(ri.getStepNumber(), ri.getInstruction())).toList();
|
// Called by controller when it receives Recipe (entity) in request body.
|
||||||
|
// MUST be null-safe and MUST never return null lists.
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RecipeDto convertToDto(Recipe recipe) {
|
||||||
|
RecipeDto dto = new RecipeDto();
|
||||||
|
|
||||||
List<ImageDto> imageDtos = recipe.getImages().stream().map(ri -> new ImageDto(ri.getImageUrl())).toList();
|
dto.setTitle(recipe.getTitle());
|
||||||
|
dto.setDescription(recipe.getDescription());
|
||||||
|
dto.setPrepTimeMinutes(recipe.getPrepTimeMinutes());
|
||||||
|
dto.setCookTimeMinutes(recipe.getCookTimeMinutes());
|
||||||
|
dto.setServings(recipe.getServings());
|
||||||
|
dto.setStatus(recipe.getStatus());
|
||||||
|
dto.setId(recipe.getId());
|
||||||
|
|
||||||
List<TagDto> tagDtos = recipe.getTags().stream().map(ri -> new TagDto(ri.getName())).toList();
|
// IMPORTANT: prevent null lists (this was your NPE earlier)
|
||||||
|
dto.setIngredients(new java.util.ArrayList<>());
|
||||||
|
dto.setSteps(new java.util.ArrayList<>());
|
||||||
|
dto.setImages(new java.util.ArrayList<>());
|
||||||
|
dto.setTags(new java.util.ArrayList<>());
|
||||||
|
|
||||||
UserDto userDto = new UserDto(recipe.getUser().getId(), recipe.getUser().getUsername(),
|
// userDto optional
|
||||||
recipe.getUser().getEmail());
|
if (recipe.getUser() != null) {
|
||||||
|
com.example.demo.dto.UserDto ud = new com.example.demo.dto.UserDto();
|
||||||
|
ud.setId(recipe.getUser().getId());
|
||||||
|
ud.setUsername(recipe.getUser().getUsername());
|
||||||
|
ud.setEmail(recipe.getUser().getEmail());
|
||||||
|
dto.setUserDto(ud);
|
||||||
|
}
|
||||||
|
|
||||||
return new RecipeDto(recipe.getTitle(), recipe.getDescription(), recipe.getPrepTimeMinutes(),
|
return dto;
|
||||||
recipe.getCookTimeMinutes(), recipe.getServings(), userDto, recipe.getStatus(), ingredientDtos,
|
}
|
||||||
stepDtos, imageDtos, tagDtos);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// -------------------- CREATE --------------------
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public RecipeDto saveRecipe(RecipeDto recipeDto) {
|
||||||
|
|
||||||
@Override
|
List<RecipeIngredientDto> ingredientDtos =
|
||||||
public RecipeDto saveRecipe(RecipeDto dto) {
|
recipeDto.getIngredients() == null ? Collections.emptyList() : recipeDto.getIngredients();
|
||||||
|
|
||||||
User user = userRepository.findById(dto.getUserDto().getId())
|
List<StepDto> stepDtos =
|
||||||
.orElseThrow(() -> new NotFoundException("User", "id", dto.getUserDto().getId()));
|
recipeDto.getSteps() == null ? Collections.emptyList() : recipeDto.getSteps();
|
||||||
|
|
||||||
Recipe recipe = new Recipe(dto.getTitle(), dto.getDescription(), dto.getPrepTimeMinutes(),
|
Recipe recipe = new Recipe();
|
||||||
dto.getCookTimeMinutes(), dto.getServings(), user, dto.getStatus());
|
recipe.setTitle(recipeDto.getTitle());
|
||||||
|
recipe.setDescription(recipeDto.getDescription());
|
||||||
|
recipe.setPrepTimeMinutes(recipeDto.getPrepTimeMinutes());
|
||||||
|
recipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes());
|
||||||
|
recipe.setServings(recipeDto.getServings());
|
||||||
|
recipe.setStatus(recipeDto.getStatus());
|
||||||
|
recipe.setCreatedAt(LocalDateTime.now());
|
||||||
|
recipe.setUpdatedAt(LocalDateTime.now());
|
||||||
|
|
||||||
for (RecipeIngredientDto riDto : dto.getIngredients()) {
|
// REQUIRED user_id
|
||||||
|
recipe.setUser(resolveUser(recipeDto.getUserDto(), null));
|
||||||
|
|
||||||
Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName())
|
// attach children (cascade = ALL on Recipe will persist them)
|
||||||
.orElseGet(() -> new Ingredient(riDto.getIngredientName()));
|
attachSteps(recipe, stepDtos);
|
||||||
|
attachRecipeIngredients(recipe, ingredientDtos);
|
||||||
|
|
||||||
if (ingredient.getId() == null) {
|
Recipe saved = recipeRepo.save(recipe);
|
||||||
ingredientRepository.save(ingredient);
|
return getRecipeById(saved.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
RecipeIngredient ri = new RecipeIngredient(recipe, ingredient, riDto.getQuantity(), riDto.getUnit(),
|
// -------------------- READ ALL --------------------
|
||||||
riDto.getNotes());
|
@Override
|
||||||
|
public List<RecipeDto> getAllRecipes() {
|
||||||
|
// Return shallow for list to avoid lazy-loading issues
|
||||||
|
return recipeRepo.findAll().stream()
|
||||||
|
.map(r -> {
|
||||||
|
RecipeDto dto = new RecipeDto();
|
||||||
|
dto.setTitle(r.getTitle());
|
||||||
|
dto.setDescription(r.getDescription());
|
||||||
|
dto.setPrepTimeMinutes(r.getPrepTimeMinutes());
|
||||||
|
dto.setCookTimeMinutes(r.getCookTimeMinutes());
|
||||||
|
dto.setServings(r.getServings());
|
||||||
|
dto.setStatus(r.getStatus());
|
||||||
|
dto.setIngredients(new ArrayList<>());
|
||||||
|
dto.setSteps(new ArrayList<>());
|
||||||
|
dto.setImages(new ArrayList<>());
|
||||||
|
dto.setTags(new ArrayList<>());
|
||||||
|
dto.setId(r.getId());
|
||||||
|
return dto;
|
||||||
|
})
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
recipe.getRecipeIngredients().add(ri);
|
// -------------------- READ ONE --------------------
|
||||||
}
|
@Override
|
||||||
|
@org.springframework.transaction.annotation.Transactional(readOnly = true)
|
||||||
|
public RecipeDto getRecipeById(Integer recipeId) {
|
||||||
|
Recipe recipe = recipeRepo.findById(recipeId)
|
||||||
|
.orElseThrow(() -> new NotFoundException("Recipe not found"));
|
||||||
|
|
||||||
if (dto.getSteps() != null) {
|
// Start with base fields
|
||||||
for (StepDto stepDto : dto.getSteps()) {
|
RecipeDto dto = convertToDto(recipe);
|
||||||
Step step = new Step(recipe, stepDto.getStepNumber(), stepDto.getInstruction());
|
|
||||||
recipe.getSteps().add(step);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dto.getImages() != null) {
|
// Overwrite lists using repo results (no mutation of Recipe.steps / recipeIngredients)
|
||||||
for (ImageDto imageDto : dto.getImages()) {
|
dto.setSteps(
|
||||||
Image image = new Image(recipe, imageDto.getImageUrl());
|
stepRepo.findByRecipeId(recipeId).stream()
|
||||||
recipe.getImages().add(image);
|
.map(s -> new com.example.demo.dto.StepDto(s.getStepNumber(), s.getInstruction()))
|
||||||
}
|
.collect(java.util.stream.Collectors.toList())
|
||||||
}
|
);
|
||||||
|
|
||||||
for (TagDto tDto : dto.getTags()) {
|
dto.setIngredients(
|
||||||
|
recipeIngredientRepo.findByRecipeId(recipeId).stream()
|
||||||
|
.map(ri -> new com.example.demo.dto.RecipeIngredientDto(
|
||||||
|
ri.getIngredient() != null ? ri.getIngredient().getName() : null,
|
||||||
|
ri.getQuantity(),
|
||||||
|
ri.getUnit(),
|
||||||
|
ri.getNotes()
|
||||||
|
))
|
||||||
|
.collect(java.util.stream.Collectors.toList())
|
||||||
|
);
|
||||||
|
|
||||||
Tag tag = tagRepository.findByName(tDto.getName()).orElseGet(() -> new Tag(tDto.getName()));
|
return dto;
|
||||||
|
}
|
||||||
|
// -------------------- UPDATE --------------------
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public RecipeDto updateRecipe(RecipeDto recipeDto, Integer id) {
|
||||||
|
|
||||||
if (tag.getId() == null) {
|
Recipe recipe = recipeRepo.findById(id)
|
||||||
tagRepository.save(tag);
|
.orElseThrow(() -> new NotFoundException("Recipe not found"));
|
||||||
}
|
|
||||||
recipe.getTags().add(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
Recipe saved = recipeRepository.save(recipe);
|
List<RecipeIngredientDto> ingredientDtos =
|
||||||
|
recipeDto.getIngredients() == null ? Collections.emptyList() : recipeDto.getIngredients();
|
||||||
|
|
||||||
return convertToDto(saved);
|
List<StepDto> stepDtos =
|
||||||
}
|
recipeDto.getSteps() == null ? Collections.emptyList() : recipeDto.getSteps();
|
||||||
|
|
||||||
@Override
|
recipe.setTitle(recipeDto.getTitle());
|
||||||
@Transactional
|
recipe.setDescription(recipeDto.getDescription());
|
||||||
public List<RecipeDto> getAllRecipes() {
|
recipe.setPrepTimeMinutes(recipeDto.getPrepTimeMinutes());
|
||||||
|
recipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes());
|
||||||
|
recipe.setServings(recipeDto.getServings());
|
||||||
|
recipe.setStatus(recipeDto.getStatus());
|
||||||
|
recipe.setUpdatedAt(LocalDateTime.now());
|
||||||
|
|
||||||
List<RecipeDto> list = new ArrayList<>();
|
// keep old user if not provided
|
||||||
for (Recipe recipe : recipeRepository.findAll()) {
|
recipe.setUser(resolveUser(recipeDto.getUserDto(), recipe.getUser()));
|
||||||
RecipeDto recipeDto = convertToDto(recipe);
|
|
||||||
list.add(recipeDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
return list;
|
// Clear old children (orphanRemoval = true will delete)
|
||||||
}
|
recipe.getSteps().clear();
|
||||||
|
recipe.getRecipeIngredients().clear();
|
||||||
|
|
||||||
@Override
|
attachSteps(recipe, stepDtos);
|
||||||
@Transactional
|
attachRecipeIngredients(recipe, ingredientDtos);
|
||||||
public RecipeDto getRecipeById(Integer Id) {
|
|
||||||
return convertToDto(recipeRepository.findById(Id).orElseThrow(() -> new NotFoundException("Recipe", "id", Id)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
Recipe saved = recipeRepo.save(recipe);
|
||||||
@Transactional
|
return getRecipeById(saved.getId());
|
||||||
public RecipeDto updateRecipe(RecipeDto recipeDto, Integer id) {
|
}
|
||||||
Recipe existingRecipe = recipeRepository.findById(id)
|
|
||||||
.orElseThrow(() -> new NotFoundException("Recipe", "id", id));
|
|
||||||
|
|
||||||
existingRecipe.setTitle(recipeDto.getTitle());
|
// -------------------- DELETE --------------------
|
||||||
existingRecipe.setDescription(recipeDto.getDescription());
|
@Override
|
||||||
existingRecipe.setPrepTimeMinutes(recipeDto.getPrepTimeMinutes());
|
@Transactional
|
||||||
existingRecipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes());
|
public void deleteRecipe(Integer id) {
|
||||||
existingRecipe.setServings(recipeDto.getServings());
|
if (!recipeRepo.existsById(id)) {
|
||||||
existingRecipe.setStatus(recipeDto.getStatus());
|
throw new NotFoundException("Recipe not found");
|
||||||
|
}
|
||||||
|
recipeRepo.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
List<RecipeIngredientDto> updatedIngredients = recipeDto.getIngredients();
|
// -------------------- Helpers --------------------
|
||||||
List<RecipeIngredient> ingredientsToRemove = new ArrayList<>();
|
|
||||||
|
|
||||||
List<StepDto> updatedSteps = recipeDto.getSteps();
|
private User resolveUser(UserDto incoming, User existing) {
|
||||||
List<Step> stepsToRemove = new ArrayList<>();
|
if (incoming != null && incoming.getId() != null) {
|
||||||
|
return userRepo.findById(incoming.getId())
|
||||||
|
.orElseThrow(() -> new NotFoundException("User not found: " + incoming.getId()));
|
||||||
|
}
|
||||||
|
if (existing != null) return existing;
|
||||||
|
|
||||||
List<ImageDto> updatedImages = recipeDto.getImages();
|
return userRepo.findById(DEFAULT_USER_ID)
|
||||||
List<Image> imagesToRemove = new ArrayList<>();
|
.orElseThrow(() -> new IllegalStateException(
|
||||||
|
"DEFAULT_USER_ID=" + DEFAULT_USER_ID + " not found. Insert a user row or change DEFAULT_USER_ID."
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
List<TagDto> updatedTags = recipeDto.getTags();
|
private void attachSteps(Recipe recipe, List<StepDto> stepDtos) {
|
||||||
List<Tag> tagsToRemove = new ArrayList<>();
|
for (StepDto sd : stepDtos) {
|
||||||
|
if (sd == null) continue;
|
||||||
|
if (sd.getInstruction() == null || sd.getInstruction().isBlank()) continue;
|
||||||
|
|
||||||
for (RecipeIngredient ri : existingRecipe.getRecipeIngredients()) {
|
Step s = new Step();
|
||||||
|
s.setRecipe(recipe);
|
||||||
|
s.setStepNumber(sd.getStepNumber() == null ? 1 : sd.getStepNumber());
|
||||||
|
s.setInstruction(sd.getInstruction());
|
||||||
|
|
||||||
boolean existsInUpdatedList = false;
|
recipe.getSteps().add(s);
|
||||||
for (RecipeIngredientDto dto : updatedIngredients) {
|
}
|
||||||
String updatedName = dto.getIngredientName();
|
}
|
||||||
String existingName = ri.getIngredient().getName();
|
|
||||||
|
|
||||||
if (updatedName.equals(existingName)) {
|
private void attachRecipeIngredients(Recipe recipe, List<RecipeIngredientDto> ingredientDtos) {
|
||||||
existsInUpdatedList = true;
|
for (RecipeIngredientDto rid : ingredientDtos) {
|
||||||
break;
|
if (rid == null) continue;
|
||||||
}
|
if (rid.getIngredientName() == null || rid.getIngredientName().isBlank()) continue;
|
||||||
}
|
|
||||||
|
|
||||||
if (!existsInUpdatedList) {
|
Ingredient ing = ingredientRepo.findByNameIgnoreCase(rid.getIngredientName().trim())
|
||||||
ingredientsToRemove.add(ri);
|
.orElseGet(() -> {
|
||||||
}
|
Ingredient i = new Ingredient();
|
||||||
}
|
i.setName(rid.getIngredientName().trim());
|
||||||
|
return ingredientRepo.save(i);
|
||||||
|
});
|
||||||
|
|
||||||
existingRecipe.getRecipeIngredients().removeAll(ingredientsToRemove);
|
RecipeIngredient ri = new RecipeIngredient();
|
||||||
|
ri.setRecipe(recipe);
|
||||||
|
ri.setIngredient(ing);
|
||||||
|
ri.setQuantity(rid.getQuantity() == null ? BigDecimal.ZERO : rid.getQuantity());
|
||||||
|
ri.setUnit(rid.getUnit());
|
||||||
|
ri.setNotes(rid.getNotes());
|
||||||
|
|
||||||
for (RecipeIngredientDto riDto : updatedIngredients) {
|
recipe.getRecipeIngredients().add(ri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
private Ingredient attachRecipeIngredients(Recipe recipe, List<RecipeIngredientDto> ingredientDtos) {
|
||||||
|
for (RecipeIngredientDto rid : ingredientDtos) {
|
||||||
|
if (rid == null) continue;
|
||||||
|
if (rid.getIngredientName() == null || rid.getIngredientName().isBlank()) continue;
|
||||||
|
|
||||||
// go through the old list of ingredients until we find a match with updated
|
Ingredient ing = ingredientRepo.findByNameIgnoreCase(rid.getIngredientName().trim())
|
||||||
// list
|
.orElseGet(() -> {
|
||||||
RecipeIngredient existingRI = existingRecipe.getRecipeIngredients().stream()
|
Ingredient i = new Ingredient();
|
||||||
.filter(ri -> ri.getIngredient().getName().equals(riDto.getIngredientName())).findFirst()
|
i.setName(rid.getIngredientName().trim());
|
||||||
.orElse(null);
|
return ingredientRepo.save(i);
|
||||||
|
});
|
||||||
|
|
||||||
// if old ingredient just update parameters
|
RecipeIngredient ri = new RecipeIngredient();
|
||||||
if (existingRI != null) {
|
ri.setRecipe(recipe);
|
||||||
|
ri.setIngredient(ing);
|
||||||
|
ri.setQuantity(rid.getQuantity() == null ? BigDecimal.ZERO : rid.getQuantity());
|
||||||
|
ri.setUnit(rid.getUnit());
|
||||||
|
ri.setNotes(rid.getNotes());
|
||||||
|
|
||||||
existingRI.setQuantity(riDto.getQuantity());
|
recipe.getRecipeIngredients().add(ri);
|
||||||
existingRI.setUnit(riDto.getUnit());
|
}
|
||||||
existingRI.setNotes(riDto.getNotes());
|
return null;
|
||||||
}
|
}*/
|
||||||
|
}
|
||||||
// if new ingredient, have to make a whole new thing
|
|
||||||
else {
|
|
||||||
|
|
||||||
Ingredient ingredient = ingredientRepository.findByName(riDto.getIngredientName())
|
|
||||||
.orElseGet(() -> new Ingredient(riDto.getIngredientName()));
|
|
||||||
|
|
||||||
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.deleteById(Id);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user