Skip to content

Commit

Permalink
#9 feat: 레시피글 삭제 시 관려된 recipe description과 ingredient도 함께 삭제되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Feb 20, 2024
1 parent b033664 commit 417fd59
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

import static com.kkobugi.puremarket.common.constants.Constant.INACTIVE;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -40,4 +42,8 @@ public Ingredient(Recipe recipe, String name, String quantity, IngredientType in
this.quantity = quantity;
this.ingredientType = ingredientType;
}

public void delete() {
this.setStatus(INACTIVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

public interface IngredientRepository extends JpaRepository<Ingredient, Long> {
List<Ingredient> findByRecipeAndIngredientTypeAndStatusEqualsOrderByCreatedDateDesc(Recipe recipe, IngredientType ingredientType, String status);
List<Ingredient> findByRecipeAndStatusEquals(Recipe recipe, String status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,22 @@ public void deleteRecipe(Long recipeIdx) throws BaseException {
Recipe recipe = recipeRepository.findById(recipeIdx).orElseThrow(() -> new BaseException(INVALID_RECIPE_IDX));

validateWriter(user, recipe);

recipe.delete();
boolean isDeleted = gcsService.deleteImage(recipe.getRecipeImage());
if (!isDeleted) throw new BaseException(IMAGE_DELETE_FAIL);

recipeRepository.save(recipe);

List<RecipeDescription> recipeDescriptionList = recipeDescriptionRepository.findByRecipeAndStatusEquals(recipe, ACTIVE);
for (RecipeDescription description : recipeDescriptionList) {
description.delete();
recipeDescriptionRepository.save(description);
}

List<Ingredient> ingredientList = ingredientRepository.findByRecipeAndStatusEquals(recipe, ACTIVE);
for (Ingredient ingredient : ingredientList) {
ingredient.delete();
ingredientRepository.save(ingredient);
}
} catch (BaseException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

import static com.kkobugi.puremarket.common.constants.Constant.INACTIVE;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -33,4 +35,8 @@ public RecipeDescription(Recipe recipe, Integer orderNumber, String description)
this.orderNumber = orderNumber;
this.description = description;
}

public void delete() {
this.setStatus(INACTIVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

public interface RecipeDescriptionRepository extends JpaRepository<RecipeDescription, Long> {
List<RecipeDescription> findByRecipeAndStatusEqualsOrderByCreatedDateDesc(Recipe recipe, String status);
List<RecipeDescription> findByRecipeAndStatusEquals(Recipe recipe, String status);
}

0 comments on commit 417fd59

Please sign in to comment.