Skip to content

Commit 08780bd

Browse files
authored
Merge pull request #117 from Recipe-Project/fix/recipe_update_id_null_error
Fix/recipe update id null error
2 parents 673a92f + 39d675f commit 08780bd

7 files changed

Lines changed: 47 additions & 65 deletions

File tree

src/main/java/com/recipe/app/src/recipe/application/RecipeService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.recipe.app.src.common.utils.BadWordFiltering;
55
import com.recipe.app.src.recipe.application.dto.RecipeRequest;
66
import com.recipe.app.src.recipe.domain.Recipe;
7+
import com.recipe.app.src.recipe.domain.RecipeIngredient;
8+
import com.recipe.app.src.recipe.domain.RecipeProcess;
79
import com.recipe.app.src.recipe.exception.NotFoundRecipeException;
810
import com.recipe.app.src.recipe.infra.RecipeRepository;
911
import com.recipe.app.src.user.domain.User;
@@ -40,6 +42,15 @@ public void create(User user, RecipeRequest request) {
4042
badWordFiltering.check(request.getTitle());
4143
badWordFiltering.check(request.getIntroduction());
4244

45+
Recipe recipe = request.toRecipeEntity(user.getUserId());
46+
List<RecipeIngredient> recipeIngredients = request.getIngredients().stream()
47+
.map(ingredient -> ingredient.toEntity(recipe))
48+
.toList();
49+
List<RecipeProcess> recipeProcesses = request.getProcesses().stream()
50+
.map(process -> process.toEntity(recipe))
51+
.toList();
52+
53+
4354
recipeRepository.save(request.toRecipeEntity(user.getUserId()));
4455
}
4556

src/main/java/com/recipe/app/src/recipe/application/dto/RecipeRequest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ public Recipe toRecipeEntity(Long userId) {
5757
.userId(userId)
5858
.build();
5959

60-
ingredients.forEach(ingredient -> ingredient.toEntity(recipe));
61-
processes.forEach(process -> process.toEntity(recipe));
60+
if (ingredients != null) {
61+
ingredients.forEach(ingredient -> ingredient.toEntity(recipe));
62+
}
63+
if (processes != null) {
64+
processes.forEach(process -> process.toEntity(recipe));
65+
}
6266

6367
return recipe;
6468
}

src/main/java/com/recipe/app/src/recipe/domain/Recipe.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,26 @@ public void updateRecipe(Recipe updateRecipe) {
106106
this.imgUrl = updateRecipe.imgUrl;
107107
this.hiddenYn = updateRecipe.hiddenYn;
108108

109-
ingredients.forEach(RecipeIngredient::delete);
110-
this.ingredients = updateRecipe.ingredients;
111-
112-
processes.forEach(RecipeProcess::delete);
113-
this.processes = updateRecipe.processes;
109+
ingredients.clear();
110+
111+
if (updateRecipe.ingredients != null && !updateRecipe.ingredients.isEmpty()) {
112+
updateRecipe.ingredients.forEach(ing -> {
113+
ing.setRecipe(this); // 부모 Recipe 변경
114+
ingredients.add(ing);
115+
});
116+
updateRecipe.ingredients.clear(); // updateRecipe에서는 제거
117+
}
118+
119+
processes.clear();
120+
121+
// 새 요리 과정 추가
122+
if (updateRecipe.processes != null && !updateRecipe.processes.isEmpty()) {
123+
updateRecipe.processes.forEach(proc -> {
124+
proc.setRecipe(this); // 부모 Recipe 변경
125+
processes.add(proc);
126+
});
127+
updateRecipe.processes.clear(); // updateRecipe에서는 제거
128+
}
114129
}
115130

116131
public boolean isHidden() {

src/main/java/com/recipe/app/src/recipe/domain/RecipeIngredient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ public RecipeIngredient(Long recipeIngredientId, Recipe recipe, String ingredien
5353

5454
this.recipeIngredientId = recipeIngredientId;
5555
this.recipe = recipe;
56-
recipe.ingredients.add(this);
56+
if (recipe != null) {
57+
recipe.ingredients.add(this);
58+
}
5759
this.ingredientName = ingredientName;
5860
this.ingredientIconId = ingredientIconId;
5961
this.quantity = quantity;
6062
this.unit = unit;
6163
}
6264

63-
public void delete() {
64-
this.recipe = null;
65+
void setRecipe(Recipe recipe) {
66+
this.recipe = recipe;
6567
}
6668

6769
public boolean hasInFridge(List<String> ingredientNames) {

src/main/java/com/recipe/app/src/recipe/domain/RecipeProcess.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public RecipeProcess(Long recipeProcessId, Recipe recipe, Integer cookingNo, Str
5151

5252
this.recipeProcessId = recipeProcessId;
5353
this.recipe = recipe;
54-
recipe.processes.add(this);
54+
if (recipe != null) {
55+
recipe.processes.add(this);
56+
}
5557
this.cookingNo = cookingNo;
5658
this.cookingDescription = cookingDescription;
5759
this.recipeProcessImgUrl = recipeProcessImgUrl;
5860
}
5961

60-
public void delete() {
61-
this.recipe = null;
62+
void setRecipe(Recipe recipe) {
63+
this.recipe = recipe;
6264
}
6365
}

src/test/groovy/com/recipe/app/src/recipe/domain/RecipeIngredientTest.groovy

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,6 @@ class RecipeIngredientTest extends Specification {
7171
null || "레시피 재료명을 입력해주세요."
7272
}
7373

74-
def "레시피 재료 삭제"() {
75-
76-
given:
77-
Recipe recipe = Recipe.builder()
78-
.recipeNm("제목")
79-
.introduction("설명")
80-
.level(RecipeLevel.NORMAL)
81-
.userId(1L)
82-
.isHidden(false)
83-
.build()
84-
85-
RecipeIngredient ingredient = RecipeIngredient.builder()
86-
.recipeIngredientId(1L)
87-
.recipe(recipe)
88-
.ingredientName("재료")
89-
.ingredientIconId(1L)
90-
.quantity("1")
91-
.unit("")
92-
.build()
93-
94-
when:
95-
ingredient.delete()
96-
97-
then:
98-
ingredient.recipe == null
99-
}
100-
10174
def "이름이 일치하는 레시피 재료가 냉장고 존재하는지 확인"() {
10275

10376
given:

src/test/groovy/com/recipe/app/src/recipe/domain/RecipeProcessTest.groovy

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,4 @@ class RecipeProcessTest extends Specification {
8989
"" || "레시피 요리 과정 설명을 입력해주세요."
9090
null || "레시피 요리 과정 설명을 입력해주세요."
9191
}
92-
93-
def "레시피 과정 삭제"() {
94-
95-
given:
96-
Recipe recipe = Recipe.builder()
97-
.recipeNm("제목")
98-
.introduction("설명")
99-
.level(RecipeLevel.NORMAL)
100-
.userId(1L)
101-
.isHidden(false)
102-
.build()
103-
104-
RecipeProcess process = RecipeProcess.builder()
105-
.recipe(recipe)
106-
.cookingNo(1)
107-
.cookingDescription("과정")
108-
.recipeProcessImgUrl("")
109-
.build()
110-
111-
when:
112-
process.delete()
113-
114-
then:
115-
process.recipe == null
116-
}
11792
}

0 commit comments

Comments
 (0)