Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RecipeIngredient> recipeIngredients = request.getIngredients().stream()
.map(ingredient -> ingredient.toEntity(recipe))
.toList();
List<RecipeProcess> recipeProcesses = request.getProcesses().stream()
.map(process -> process.toEntity(recipe))
.toList();


recipeRepository.save(request.toRecipeEntity(user.getUserId()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/recipe/app/src/recipe/domain/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ingredientNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading