Skip to content

Commit

Permalink
♻️ change method name
Browse files Browse the repository at this point in the history
  • Loading branch information
tackyu committed Aug 14, 2024
1 parent caf8eb3 commit 1063dd3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import java.util.List;
import net.pengcook.ingredient.domain.IngredientRecipe;
import net.pengcook.recipe.domain.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;

public interface IngredientRecipeRepository extends JpaRepository<IngredientRecipe, Long> {

List<IngredientRecipe> findAllByRecipe(Recipe recipe);
List<IngredientRecipe> findAllByRecipeId(long recipeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public IngredientRecipe save(Ingredient ingredient, Recipe recipe, Requirement r
return ingredientRecipeRepository.save(ingredientRecipe);
}

public void deleteIngredientRecipe(Recipe recipe) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipe(recipe);
public void deleteIngredientRecipe(long recipeId) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipeId(recipeId);
for (IngredientRecipe ingredientRecipe : ingredientRecipes) {
ingredientSubstitutionService.delete(ingredientRecipe);
ingredientRecipeRepository.delete(ingredientRecipe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.pengcook.ingredient.domain.Requirement;
import net.pengcook.ingredient.repository.IngredientRecipeRepository;
import net.pengcook.ingredient.repository.IngredientSubstitutionRepository;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.repository.RecipeRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -40,11 +39,10 @@ class IngredientRecipeServiceTest {
@Test
@DisplayName("레시피에 종속된 ingredientRecipe를 제거한다.")
void deleteIngredientRecipe() {
Recipe recipe = recipeRepository.findById(1L).get();
int initialSize = ingredientRecipeRepository.findAll().size();
int targetSize = extractIngredientRecipeSize(recipe);
int targetSize = extractIngredientRecipeSize(1L);

ingredientRecipeService.deleteIngredientRecipe(recipe);
ingredientRecipeService.deleteIngredientRecipe(1L);

int expectedSize = ingredientRecipeRepository.findAll().size();
assertThat(expectedSize).isEqualTo(initialSize - targetSize);
Expand All @@ -53,26 +51,25 @@ void deleteIngredientRecipe() {
@Test
@DisplayName("레시피에 종속된 ingredientSubstitution을 제거한다.")
void deleteIngredientRecipeWithSubstitution() {
Recipe recipe = recipeRepository.findById(1L).get();
int initialSize = ingredientSubstitutionRepository.findAll().size();
int targetSize = extractIngredientSubstitutionSize(recipe);
int targetSize = extractIngredientSubstitutionSize(1L);

ingredientRecipeService.deleteIngredientRecipe(recipe);
ingredientRecipeService.deleteIngredientRecipe(1L);

int expectedSize = ingredientSubstitutionRepository.findAll().size();
assertThat(expectedSize).isEqualTo(initialSize - targetSize);
}

private int extractIngredientRecipeSize(Recipe recipe) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipe(recipe);
private int extractIngredientRecipeSize(long recipeId) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipeId(recipeId);
return ingredientRecipes.stream()
.map((ingredientRecipe -> ingredientRecipe.getIngredient().getName()))
.toList()
.size();
}

private int extractIngredientSubstitutionSize(Recipe recipe) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipe(recipe);
private int extractIngredientSubstitutionSize(long recipeId) {
List<IngredientRecipe> ingredientRecipes = ingredientRecipeRepository.findAllByRecipeId(recipeId);
return ingredientRecipes.stream()
.filter(ingredientRecipe -> ingredientRecipe.getRequirement() == Requirement.ALTERNATIVE)
.mapToInt(
Expand Down

0 comments on commit 1063dd3

Please sign in to comment.