added implementations back in

This commit is contained in:
durn
2026-04-25 11:49:25 -06:00
parent 263ec9fd07
commit 716499ffca
@@ -191,10 +191,79 @@ public class RecipeServiceImpl implements RecipeService {
dto.getCost()
);
if (dto.getIngredients() != null) {
java.util.Set<String> seenIngredientNames = new java.util.HashSet<>();
java.util.Set<Integer> seenIngredientIds = new java.util.HashSet<>();
for (int i = 0; i < dto.getIngredients().size(); i++) {
RecipeIngredientDto riDto = dto.getIngredients().get(i);
String rawName = riDto.getIngredientName();
String normalizedName = rawName == null ? "" : rawName.trim().toLowerCase();
if (normalizedName.isBlank()) {
continue;
}
if (!seenIngredientNames.add(normalizedName)) {
throw new IllegalArgumentException("Duplicate ingredient entry: " + rawName.trim());
}
Ingredient ingredient = ingredientRepository.findByNameIgnoreCase(rawName.trim())
.orElseGet(() -> new Ingredient(rawName.trim()));
if (ingredient.getId() == null) {
ingredientRepository.save(ingredient);
}
if (ingredient.getId() != null && !seenIngredientIds.add(ingredient.getId())) {
throw new IllegalArgumentException("Duplicate ingredient entry: " + rawName.trim());
}
RecipeIngredient ri = new RecipeIngredient(
recipe,
ingredient,
riDto.getQuantity(),
riDto.getUnit(),
riDto.getNotes()
);
ri.setOrderIndex(i);
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 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 getRecipeById(saved.getId());
}
@Override
@Transactional
public List<RecipeDto> getAllRecipes() {
@@ -232,12 +301,155 @@ public class RecipeServiceImpl implements RecipeService {
existingRecipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes());
existingRecipe.setServings(recipeDto.getServings());
existingRecipe.setStatus(recipeDto.getStatus());
existingRecipe.setStatus(recipeDto.getStatus());
existingRecipe.setCost(recipeDto.getCost());
List<RecipeIngredientDto> updatedIngredients = recipeDto.getIngredients();
List<RecipeIngredient> ingredientsToRemove = new ArrayList<>();
List<StepDto> updatedSteps = recipeDto.getSteps();
List<Step> stepsToRemove = new ArrayList<>();
List<ImageDto> updatedImages = recipeDto.getImages();
List<Image> imagesToRemove = new ArrayList<>();
List<TagDto> updatedTags = recipeDto.getTags();
List<Tag> tagsToRemove = new ArrayList<>();
if (updatedIngredients != null) {
for (RecipeIngredient ri : existingRecipe.getRecipeIngredients()) {
boolean existsInUpdatedList = false;
for (RecipeIngredientDto dto : updatedIngredients) {
String updatedName = dto.getIngredientName();
String existingName = ri.getIngredient().getName();
if (java.util.Objects.equals(updatedName, existingName)) {
existsInUpdatedList = true;
break;
}
}
if (!existsInUpdatedList) {
ingredientsToRemove.add(ri);
}
}
existingRecipe.getRecipeIngredients().removeAll(ingredientsToRemove);
for (RecipeIngredientDto riDto : updatedIngredients) {
RecipeIngredient existingRI = existingRecipe.getRecipeIngredients().stream()
.filter(ri -> java.util.Objects.equals(ri.getIngredient().getName(), riDto.getIngredientName()))
.findFirst()
.orElse(null);
if (existingRI != null) {
existingRI.setQuantity(riDto.getQuantity());
existingRI.setUnit(riDto.getUnit());
existingRI.setNotes(riDto.getNotes());
}
else {
Ingredient ingredient = ingredientRepository.findByNameIgnoreCase(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) {
for (Step step : existingRecipe.getSteps()) {
boolean existsInUpdatedList = updatedSteps.stream()
.anyMatch(dto -> dto.getStepNumber().equals(step.getStepNumber()));
if (!existsInUpdatedList)
stepsToRemove.add(step);
}
existingRecipe.getSteps().removeAll(stepsToRemove);
for (StepDto stepDto : updatedSteps) {
Step existingStep = existingRecipe.getSteps().stream()
.filter(s -> s.getStepNumber().equals(stepDto.getStepNumber())).findFirst().orElse(null);
if (existingStep != null) {
existingStep.setInstruction(stepDto.getInstruction());
}
else {
Step newStep = new Step(existingRecipe, stepDto.getStepNumber(), stepDto.getInstruction());
existingRecipe.getSteps().add(newStep);
}
}
}
if (updatedImages != null) {
for (Image image : existingRecipe.getImages()) {
boolean existsInUpdatedList = updatedImages.stream()
.anyMatch(dto -> java.util.Objects.equals(dto.getImageUrl(), image.getImageUrl()));
if (!existsInUpdatedList)
imagesToRemove.add(image);
}
existingRecipe.getImages().removeAll(imagesToRemove);
for (ImageDto imageDto : updatedImages) {
Image existingImage = existingRecipe.getImages().stream()
.filter(img -> java.util.Objects.equals(img.getImageUrl(), imageDto.getImageUrl()))
.findFirst()
.orElse(null);
if (existingImage != null) {
existingImage.setImageUrl(imageDto.getImageUrl());
} else {
Image newImage = new Image(existingRecipe, imageDto.getImageUrl());
existingRecipe.getImages().add(newImage);
}
}
}
if (updatedTags != null) {
for (Tag tag : existingRecipe.getTags()) {
boolean existsInUpdatedList = updatedTags.stream()
.anyMatch(dto -> java.util.Objects.equals(dto.getName(), tag.getName()));
if (!existsInUpdatedList)
tagsToRemove.add(tag);
}
existingRecipe.getTags().removeAll(tagsToRemove);
for (TagDto tagDto : updatedTags) {
Tag existingTag = existingRecipe.getTags().stream()
.filter(tag -> java.util.Objects.equals(tag.getName(), 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
@Transactional
public List<RecipeDto> getNewestRecipes(int limit) {
@@ -251,19 +463,85 @@ public class RecipeServiceImpl implements RecipeService {
@Override
@Transactional
public List<RecipeDto> getRecipes(String name, List<String> tags, List<Integer> prices,
List<Integer> cookTime, List<Integer> prepTime) {
public List<RecipeDto> getRecipes(String name, List<String> tags, List<Integer> prices, List<Integer> cookTime, List<Integer> prepTime) {
List<Recipe> recipes = recipeRepository.findAll();
List<Recipe> recipes;
if (name != null && !name.isBlank()) {
if ((name != null) && (!name.isBlank())) {
recipes = recipeRepository.findByTitleContainingIgnoreCase(name);
}
List<RecipeDto> result = new ArrayList<>();
for (Recipe r : recipes) {
result.add(convertToDto(r));
else {
recipes = recipeRepository.findAll();
}
return result;
if ((tags != null) && (!tags.isEmpty()) && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> recipe.getTags().stream().anyMatch(tag -> tags.contains(tag.getName())))
.collect(Collectors.toList());
}
if (prices != null && !prices.isEmpty() && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> {
Integer cost = recipe.getCost();
if (cost == null) {
return false;
}
for (Integer price : prices) {
if (price == 4 && cost >= 4) {
return true;
}
if (price != 4 && cost.equals(price)) {
return true;
}
}
return false;
})
.collect(Collectors.toList());
}
if (cookTime != null && !cookTime.isEmpty() && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> {
int minutes = recipe.getCookTimeMinutes();
for (Integer ct : cookTime) {
if (ct == 15 && minutes <= 15) return true;
if (ct == 30 && minutes > 15 && minutes <= 30) return true;
if (ct == 60 && minutes > 30 && minutes <= 60) return true;
if (ct == 120 && minutes > 60 && minutes <= 120) return true;
if (ct == 121 && minutes > 120) return true;
}
return false;
})
.collect(Collectors.toList());
}
if (prepTime != null && !prepTime.isEmpty() && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> {
int minutes = recipe.getPrepTimeMinutes();
for (Integer ct : prepTime) {
if (ct == 15 && minutes <= 15) return true;
if (ct == 30 && minutes > 15 && minutes <= 30) return true;
if (ct == 60 && minutes > 30 && minutes <= 60) return true;
if (ct == 240 && minutes > 60 && minutes <= 240) return true;
if (ct == 241 && minutes > 240) return true;
}
return false;
})
.collect(Collectors.toList());
}
List<RecipeDto> recipeList = new ArrayList<>();
for (Recipe recipe : recipes) {
RecipeDto dto = convertToDto(recipe);
recipeList.add(dto);
}
return recipeList;
}
}