diff --git a/src/main/java/com/recipe/app/src/recipe/application/RecipeService.java b/src/main/java/com/recipe/app/src/recipe/application/RecipeService.java index e0bb9988..91b70fff 100644 --- a/src/main/java/com/recipe/app/src/recipe/application/RecipeService.java +++ b/src/main/java/com/recipe/app/src/recipe/application/RecipeService.java @@ -4,6 +4,8 @@ import com.recipe.app.src.common.utils.BadWordFiltering; import com.recipe.app.src.recipe.application.dto.RecipeRequest; import com.recipe.app.src.recipe.domain.Recipe; +import com.recipe.app.src.recipe.domain.RecipeIngredient; +import com.recipe.app.src.recipe.domain.RecipeProcess; import com.recipe.app.src.recipe.exception.NotFoundRecipeException; import com.recipe.app.src.recipe.infra.RecipeRepository; import com.recipe.app.src.user.domain.User; @@ -40,6 +42,15 @@ public void create(User user, RecipeRequest request) { badWordFiltering.check(request.getTitle()); badWordFiltering.check(request.getIntroduction()); + Recipe recipe = request.toRecipeEntity(user.getUserId()); + List recipeIngredients = request.getIngredients().stream() + .map(ingredient -> ingredient.toEntity(recipe)) + .toList(); + List recipeProcesses = request.getProcesses().stream() + .map(process -> process.toEntity(recipe)) + .toList(); + + recipeRepository.save(request.toRecipeEntity(user.getUserId())); } diff --git a/src/main/java/com/recipe/app/src/recipe/application/dto/RecipeRequest.java b/src/main/java/com/recipe/app/src/recipe/application/dto/RecipeRequest.java index db313987..7c97a6fd 100644 --- a/src/main/java/com/recipe/app/src/recipe/application/dto/RecipeRequest.java +++ b/src/main/java/com/recipe/app/src/recipe/application/dto/RecipeRequest.java @@ -57,8 +57,12 @@ public Recipe toRecipeEntity(Long userId) { .userId(userId) .build(); - ingredients.forEach(ingredient -> ingredient.toEntity(recipe)); - processes.forEach(process -> process.toEntity(recipe)); + if (ingredients != null) { + ingredients.forEach(ingredient -> ingredient.toEntity(recipe)); + } + if (processes != null) { + processes.forEach(process -> process.toEntity(recipe)); + } return recipe; } diff --git a/src/main/java/com/recipe/app/src/recipe/domain/Recipe.java b/src/main/java/com/recipe/app/src/recipe/domain/Recipe.java index 4c23aeb3..743f631e 100644 --- a/src/main/java/com/recipe/app/src/recipe/domain/Recipe.java +++ b/src/main/java/com/recipe/app/src/recipe/domain/Recipe.java @@ -106,11 +106,26 @@ public void updateRecipe(Recipe updateRecipe) { this.imgUrl = updateRecipe.imgUrl; this.hiddenYn = updateRecipe.hiddenYn; - ingredients.forEach(RecipeIngredient::delete); - this.ingredients = updateRecipe.ingredients; - - processes.forEach(RecipeProcess::delete); - this.processes = updateRecipe.processes; + ingredients.clear(); + + if (updateRecipe.ingredients != null && !updateRecipe.ingredients.isEmpty()) { + updateRecipe.ingredients.forEach(ing -> { + ing.setRecipe(this); // 부모 Recipe 변경 + ingredients.add(ing); + }); + updateRecipe.ingredients.clear(); // updateRecipe에서는 제거 + } + + processes.clear(); + + // 새 요리 과정 추가 + if (updateRecipe.processes != null && !updateRecipe.processes.isEmpty()) { + updateRecipe.processes.forEach(proc -> { + proc.setRecipe(this); // 부모 Recipe 변경 + processes.add(proc); + }); + updateRecipe.processes.clear(); // updateRecipe에서는 제거 + } } public boolean isHidden() { diff --git a/src/main/java/com/recipe/app/src/recipe/domain/RecipeIngredient.java b/src/main/java/com/recipe/app/src/recipe/domain/RecipeIngredient.java index 47eaac04..f716fa58 100644 --- a/src/main/java/com/recipe/app/src/recipe/domain/RecipeIngredient.java +++ b/src/main/java/com/recipe/app/src/recipe/domain/RecipeIngredient.java @@ -53,15 +53,17 @@ public RecipeIngredient(Long recipeIngredientId, Recipe recipe, String ingredien this.recipeIngredientId = recipeIngredientId; this.recipe = recipe; - recipe.ingredients.add(this); + if (recipe != null) { + recipe.ingredients.add(this); + } this.ingredientName = ingredientName; this.ingredientIconId = ingredientIconId; this.quantity = quantity; this.unit = unit; } - public void delete() { - this.recipe = null; + void setRecipe(Recipe recipe) { + this.recipe = recipe; } public boolean hasInFridge(List ingredientNames) { diff --git a/src/main/java/com/recipe/app/src/recipe/domain/RecipeProcess.java b/src/main/java/com/recipe/app/src/recipe/domain/RecipeProcess.java index a1ad1768..fa64bc17 100644 --- a/src/main/java/com/recipe/app/src/recipe/domain/RecipeProcess.java +++ b/src/main/java/com/recipe/app/src/recipe/domain/RecipeProcess.java @@ -51,13 +51,15 @@ public RecipeProcess(Long recipeProcessId, Recipe recipe, Integer cookingNo, Str this.recipeProcessId = recipeProcessId; this.recipe = recipe; - recipe.processes.add(this); + if (recipe != null) { + recipe.processes.add(this); + } this.cookingNo = cookingNo; this.cookingDescription = cookingDescription; this.recipeProcessImgUrl = recipeProcessImgUrl; } - public void delete() { - this.recipe = null; + void setRecipe(Recipe recipe) { + this.recipe = recipe; } } diff --git a/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeIngredientTest.groovy b/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeIngredientTest.groovy index 9b64bc4e..317d6b3e 100644 --- a/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeIngredientTest.groovy +++ b/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeIngredientTest.groovy @@ -71,33 +71,6 @@ class RecipeIngredientTest extends Specification { null || "레시피 재료명을 입력해주세요." } - def "레시피 재료 삭제"() { - - given: - Recipe recipe = Recipe.builder() - .recipeNm("제목") - .introduction("설명") - .level(RecipeLevel.NORMAL) - .userId(1L) - .isHidden(false) - .build() - - RecipeIngredient ingredient = RecipeIngredient.builder() - .recipeIngredientId(1L) - .recipe(recipe) - .ingredientName("재료") - .ingredientIconId(1L) - .quantity("1") - .unit("개") - .build() - - when: - ingredient.delete() - - then: - ingredient.recipe == null - } - def "이름이 일치하는 레시피 재료가 냉장고 존재하는지 확인"() { given: diff --git a/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeProcessTest.groovy b/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeProcessTest.groovy index 9d1f31b3..7e94862f 100644 --- a/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeProcessTest.groovy +++ b/src/test/groovy/com/recipe/app/src/recipe/domain/RecipeProcessTest.groovy @@ -89,29 +89,4 @@ class RecipeProcessTest extends Specification { "" || "레시피 요리 과정 설명을 입력해주세요." null || "레시피 요리 과정 설명을 입력해주세요." } - - def "레시피 과정 삭제"() { - - given: - Recipe recipe = Recipe.builder() - .recipeNm("제목") - .introduction("설명") - .level(RecipeLevel.NORMAL) - .userId(1L) - .isHidden(false) - .build() - - RecipeProcess process = RecipeProcess.builder() - .recipe(recipe) - .cookingNo(1) - .cookingDescription("과정") - .recipeProcessImgUrl("") - .build() - - when: - process.delete() - - then: - process.recipe == null - } }