Update 2 files

- /demo/src/main/java/com/example/demo/repository/RecipeIngredientRepo.java
- /demo/src/main/java/com/example/demo/service/Impl/RecipeServiceImpl.java
This commit is contained in:
Madeleine Stamp
2026-03-05 09:55:47 -07:00
parent fe2cd38277
commit 549b81112e
2 changed files with 7 additions and 28 deletions
@@ -1,5 +1,7 @@
package com.example.demo.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entity.Recipe;
@@ -139,14 +139,14 @@ public class RecipeServiceImpl implements RecipeService {
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public RecipeDto getRecipeById(Integer recipeId) {
Recipe recipe = recipeRepo.findById(recipeId)
.orElseThrow(() -> new NotFoundException("Recipe not found"));
.orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));
// Start with base fields
RecipeDto dto = convertToDto(recipe);
// Overwrite lists using repo results (no mutation of Recipe.steps / recipeIngredients)
dto.setSteps(
stepRepo.findByRecipeId(recipeId).stream()
stepRepo.findById(recipeId).stream()
.map(s -> new com.example.demo.dto.StepDto(s.getStepNumber(), s.getInstruction()))
.collect(java.util.stream.Collectors.toList())
);
@@ -170,7 +170,7 @@ public class RecipeServiceImpl implements RecipeService {
public RecipeDto updateRecipe(RecipeDto recipeDto, Integer id) {
Recipe recipe = recipeRepo.findById(id)
.orElseThrow(() -> new NotFoundException("Recipe not found"));
.orElseThrow(() -> new NotFoundException("Recipe", "id", recipeDto));
List<RecipeIngredientDto> ingredientDtos =
recipeDto.getIngredients() == null ? Collections.emptyList() : recipeDto.getIngredients();
@@ -205,7 +205,7 @@ public class RecipeServiceImpl implements RecipeService {
@Transactional
public void deleteRecipe(Integer id) {
if (!recipeRepo.existsById(id)) {
throw new NotFoundException("Recipe not found");
throw new NotFoundException("Recipe not found", null, id);
}
recipeRepo.deleteById(id);
}
@@ -215,7 +215,7 @@ public class RecipeServiceImpl implements RecipeService {
private User resolveUser(UserDto incoming, User existing) {
if (incoming != null && incoming.getId() != null) {
return userRepo.findById(incoming.getId())
.orElseThrow(() -> new NotFoundException("User not found: " + incoming.getId()));
.orElseThrow(() -> new NotFoundException("User not found: " + incoming.getId(), null, existing));
}
if (existing != null) return existing;
@@ -261,28 +261,5 @@ public class RecipeServiceImpl implements RecipeService {
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;
Ingredient ing = ingredientRepo.findByNameIgnoreCase(rid.getIngredientName().trim())
.orElseGet(() -> {
Ingredient i = new Ingredient();
i.setName(rid.getIngredientName().trim());
return ingredientRepo.save(i);
});
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());
recipe.getRecipeIngredients().add(ri);
}
return null;
}*/
}