From 2794f30a8f4da806baa7ce3614a518792a097ce4 Mon Sep 17 00:00:00 2001 From: Somin Park Date: Tue, 1 Oct 2024 18:31:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20dish,=20meal=EC=97=90=20soft=20delete?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dishstore/DishReader.java | 4 ++ .../domain/dishstore/DishRemover.java | 4 -- .../domain/dishstore/DishRepository.java | 4 +- .../domain/dishstore/DishService.java | 4 +- .../mealtoktok/domain/meal/MealReader.java | 4 ++ .../mealtoktok/domain/meal/MealRemover.java | 4 +- .../domain/meal/MealRepository.java | 8 ++-- .../mealtoktok/domain/meal/MealService.java | 7 ++-- .../infra/dishstore/entity/DishEntity.java | 7 ++++ .../repository/CoreDishRepository.java | 38 ++++++++----------- .../repository/JpaDishRepository.java | 16 +++++--- .../infra/meal/entity/MealEntity.java | 7 ++++ .../meal/repository/CoreMealRepository.java | 18 ++++----- .../meal/repository/JpaMealRepository.java | 11 ++++-- 14 files changed, 76 insertions(+), 60 deletions(-) diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishReader.java b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishReader.java index efbdc0f..2007f0b 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishReader.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishReader.java @@ -23,4 +23,8 @@ public List readAll(DishCategory dishCategory) { public List search(String keyword) { return dishRepository.findAllByKeyword(keyword); } + + public Dish readActiveDish(TargetDish targetDish) { + return dishRepository.findActiveDishById(targetDish); + } } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRemover.java b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRemover.java index 7993440..eb305f5 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRemover.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRemover.java @@ -14,12 +14,8 @@ public class DishRemover { private final DishRepository dishRepository; - private final ImageReader imageReader; - private final ImageRemover imageRemover; public void remove(Dish dish) { dishRepository.deleteDish(dish); - Image image = imageReader.read(TargetImage.from(dish.getDishImage().getId())); - imageRemover.remove(image); } } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRepository.java b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRepository.java index 021238b..3982d62 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRepository.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishRepository.java @@ -37,7 +37,7 @@ void saveDish( List findAllByKeyword(String keyword); - List findAllDishImageByDishId(TargetDish targetDish); + Dish findActiveDishById(TargetDish targetDish); - void deleteDishImages(List dishImages); + void findAllActiveDishById(List dishIds); } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishService.java b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishService.java index 296619f..30b3754 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishService.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/dishstore/DishService.java @@ -33,13 +33,13 @@ public void createDish( @Transactional public void deleteDish(TargetDish targetDish) { - Dish dish = dishReader.read(targetDish); + Dish dish = dishReader.readActiveDish(targetDish); dishRemover.remove(dish); } @Transactional public void updateDish(TargetDish targetDish, File uploadImage, DishContent dishContent) { - Dish dish = dishReader.read(targetDish); + Dish dish = dishReader.readActiveDish(targetDish); DishStore dishStore = dishStoreReader.read(TargetDishStore.from(dish.getDishStoreId())); dishUpdater.update(dishStore, dish, uploadImage, dishContent); } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealReader.java b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealReader.java index 82a67f5..ca64e0d 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealReader.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealReader.java @@ -19,4 +19,8 @@ public Meal read(TargetMeal targetMeal) { public List read(MealOwner mealOwner) { return mealRepository.findAllByMealOwner(mealOwner); } + + public Meal readActiveMeal(TargetMeal targetMeal) { + return mealRepository.findActiveMealById(targetMeal); + } } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRemover.java b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRemover.java index f248d05..cd8324c 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRemover.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRemover.java @@ -13,8 +13,8 @@ public class MealRemover { private final MealRepository mealRepository; private final MealValidator mealValidator; - public void remove(MealOwner mealOwner, Meal meal, List mealDishes) { + public void remove(MealOwner mealOwner, Meal meal) { mealValidator.validate(mealOwner, meal); - mealRepository.delete(meal, mealDishes); + mealRepository.delete(meal); } } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRepository.java b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRepository.java index 18fac18..83cf776 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRepository.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealRepository.java @@ -12,7 +12,7 @@ public interface MealRepository { void update(Meal meal, MealContent mealContent); - void delete(Meal meal, List mealDishes); + void delete(Meal meal); List findAllMealDishByMealId(TargetMeal targetMeal); @@ -20,9 +20,7 @@ public interface MealRepository { List findAllByMealOwner(MealOwner mealOwner); - void saveMealDish(MealDish updatedMealDish); - - void delete(MealDish mealDish); - void updateMealDish(MealDish mealDish, Long dishId); + + Meal findActiveMealById(TargetMeal targetMeal); } diff --git a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealService.java b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealService.java index 533b795..add76a6 100644 --- a/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealService.java +++ b/domain/src/main/java/core/startup/mealtoktok/domain/meal/MealService.java @@ -26,16 +26,15 @@ public void createMeal(MealOwner mealOwner, MealContent newMealContent) { @Transactional public void updateMeal( MealOwner mealOwner, TargetMeal targetMeal, MealContent updatedMealContent) { - Meal meal = mealReader.read(targetMeal); + Meal meal = mealReader.readActiveMeal(targetMeal); List mealDishes = mealDishReader.read(targetMeal); mealUpdater.update(mealOwner, meal, mealDishes, updatedMealContent); } @Transactional public void deleteMeal(MealOwner mealOwner, TargetMeal targetMeal) { - Meal meal = mealReader.read(targetMeal); - List mealDishes = mealDishReader.read(targetMeal); - mealRemover.remove(mealOwner, meal, mealDishes); + Meal meal = mealReader.readActiveMeal(targetMeal); + mealRemover.remove(mealOwner, meal); } public MealWithDishes readMealAndDishes(TargetMeal targetMeal) { diff --git a/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/entity/DishEntity.java b/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/entity/DishEntity.java index 4ab4351..9fb6d73 100644 --- a/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/entity/DishEntity.java +++ b/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/entity/DishEntity.java @@ -37,6 +37,8 @@ public class DishEntity { private Long dishCategoryId; + private boolean isDeleted; + public static DishEntity of(Long dishStoreId, Long dishCategoryId, DishContent dishContent) { DishState dishState = dishContent.dishQuantity() == 0 ? DishState.SOLD_OUT : DishState.ON_SALE; @@ -48,6 +50,7 @@ public static DishEntity of(Long dishStoreId, Long dishCategoryId, DishContent d .dishState(dishState) .dishStoreId(dishStoreId) .dishCategoryId(dishCategoryId) + .isDeleted(false) .build(); } @@ -91,4 +94,8 @@ public void decreaseQuantity(int amount) { } } } + + public void delete() { + this.isDeleted = true; + } } diff --git a/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/CoreDishRepository.java b/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/CoreDishRepository.java index 2a2f76c..011ad34 100644 --- a/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/CoreDishRepository.java +++ b/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/CoreDishRepository.java @@ -42,20 +42,7 @@ public void saveDish( @Override public void deleteDish(Dish dish) { - jpaDishRepository.deleteById(dish.getDishId()); - List dishImages = findAllDishImageByDishId(TargetDish.from(dish.getDishId())); - deleteDishImages(dishImages); - } - - @Override - public void deleteDishImages(List dishImages) { - dishImages.forEach( - dishImage -> { - Long imageId = dishImage.imageId(); - jpaImageRepository.deleteById(imageId); - }); - - dishImages.forEach(dishImage -> jpaDishImageRepository.deleteByDishId(dishImage.dishId())); + jpaDishRepository.getReferenceById(dish.getDishId()).delete(); } @Override @@ -70,7 +57,7 @@ public void updateDish(Dish dish, DishContent dishContent, Image image) { @Override public boolean existsByDishStoreIdAndDishName(DishStore dishStore, String dishName) { - return jpaDishRepository.existsByDishStoreIdAndDishName(dishStore.getStoreId(), dishName); + return jpaDishRepository.existsByDishStoreIdAndDishNameAndIsDeletedFalse(dishStore.getStoreId(), dishName); } @Override @@ -124,21 +111,28 @@ public List findAllCategories() { .toList(); } - @Override - public List findAllDishImageByDishId(TargetDish targetDish) { - return jpaDishImageRepository.findAllByDishId(targetDish.dishId()).stream() - .map(DishImageEntity::toDomain) + public List findAllByKeyword(String keyword) { + return jpaDishRepository.findByIsDeletedFalseAndDishNameContaining(keyword).stream() + .map(this::toDishWithImage) .toList(); } - public List findAllByKeyword(String keyword) { - return jpaDishRepository.findByDishNameContaining(keyword).stream() + @Override + public Dish findActiveDishById(TargetDish targetDish) { + return jpaDishRepository.findByDishIdAndIsDeletedFalse(targetDish.dishId()) + .map(this::toDishWithImage) + .orElseThrow(() -> DishNotFoundException.EXCEPTION); + } + + @Override + public void findAllActiveDishById(List dishIds) { + jpaDishRepository.findAllByDishIdInAndIsDeletedFalse(dishIds).stream() .map(this::toDishWithImage) .toList(); } public List findAllByCategory(DishCategory dishCategory) { - return jpaDishRepository.findByDishCategoryId(dishCategory.getCategoryId()).stream() + return jpaDishRepository.findByDishCategoryIdAndIsDeletedFalse(dishCategory.getCategoryId()).stream() .map(this::toDishWithImage) .toList(); } diff --git a/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/JpaDishRepository.java b/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/JpaDishRepository.java index 8f07242..c6d8cd5 100644 --- a/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/JpaDishRepository.java +++ b/infra/src/main/java/core/startup/mealtoktok/infra/dishstore/repository/JpaDishRepository.java @@ -2,19 +2,25 @@ import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import core.startup.mealtoktok.infra.dishstore.entity.DishEntity; public interface JpaDishRepository extends JpaRepository { - List findByDishCategoryId(Long categoryId); - - boolean existsByDishStoreIdAndDishName(Long dishStoreId, String dishName); - boolean existsByDishStoreIdAndDishNameAndDishIdNot( Long dishStoreId, String dishName, Long dishId); - List findByDishNameContaining(String keyword); + List findByIsDeletedFalseAndDishNameContaining(String keyword); + + Optional findByDishIdAndIsDeletedFalse(Long aLong); + + Optional findByDishCategoryIdAndIsDeletedFalse(Long categoryId); + + boolean existsByDishStoreIdAndDishNameAndIsDeletedFalse(Long storeId, String dishName); + + List findAllByDishIdInAndIsDeletedFalse(List dishIds); + ; } diff --git a/infra/src/main/java/core/startup/mealtoktok/infra/meal/entity/MealEntity.java b/infra/src/main/java/core/startup/mealtoktok/infra/meal/entity/MealEntity.java index 7059c32..8d93ac0 100644 --- a/infra/src/main/java/core/startup/mealtoktok/infra/meal/entity/MealEntity.java +++ b/infra/src/main/java/core/startup/mealtoktok/infra/meal/entity/MealEntity.java @@ -42,11 +42,14 @@ public class MealEntity { @AttributeOverride(name = "userId", column = @Column(name = "meal_owner_id")) private MealOwnerVO mealOwner; + private boolean isDeleted; + public static MealEntity from(Meal meal) { return MealEntity.builder() .mealName(meal.getMealInfo().mealName()) .mealPrice(meal.getMealInfo().mealPrice()) .mealOwner(MealOwnerVO.from(meal.getMealOwner())) + .isDeleted(false) .build(); } @@ -62,4 +65,8 @@ public void update(MealInfo mealInfo) { this.mealName = mealInfo.mealName(); this.mealPrice = mealInfo.mealPrice(); } + + public void delete() { + this.isDeleted = true; + } } diff --git a/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/CoreMealRepository.java b/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/CoreMealRepository.java index f706a90..25692de 100644 --- a/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/CoreMealRepository.java +++ b/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/CoreMealRepository.java @@ -30,7 +30,7 @@ public class CoreMealRepository implements MealRepository { @Override public boolean exitsByMealName(String mealName) { - return jpaMealRepository.existsByMealName(mealName); + return jpaMealRepository.existsByMealNameAndIsDeletedFalse(mealName); } @Override @@ -46,13 +46,10 @@ public List findAllByMealOwner(MealOwner mealOwner) { } @Override - public void saveMealDish(MealDish updatedMealDish) { - jpaMealDishRepository.save(MealDishEntity.from(updatedMealDish)); - } - - @Override - public void delete(MealDish mealDish) { - jpaMealDishRepository.deleteById(mealDish.mealDishId()); + public Meal findActiveMealById(TargetMeal targetMeal) { + return jpaMealRepository.findByMealIdAndIsDeletedFalse(targetMeal.meadId()) + .map(MealEntity::toDomain) + .orElseThrow(() -> MealNotFoundException.EXCEPTION); } @Override @@ -82,9 +79,8 @@ public void update(Meal meal, MealContent updatedMealContent) { } @Override - public void delete(Meal meal, List mealDishes) { - jpaMealRepository.deleteById(meal.getMealId()); - mealDishes.forEach(mealDish -> jpaMealDishRepository.deleteById(mealDish.mealDishId())); + public void delete(Meal meal) { + jpaMealRepository.getReferenceById(meal.getMealId()).delete(); } @Override diff --git a/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/JpaMealRepository.java b/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/JpaMealRepository.java index 5fefe16..ef9acc4 100644 --- a/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/JpaMealRepository.java +++ b/infra/src/main/java/core/startup/mealtoktok/infra/meal/repository/JpaMealRepository.java @@ -1,7 +1,10 @@ package core.startup.mealtoktok.infra.meal.repository; +import core.startup.mealtoktok.domain.meal.Meal; import java.util.List; +import java.util.Optional; +import org.geolatte.geom.M; import org.springframework.data.jpa.repository.JpaRepository; import core.startup.mealtoktok.infra.meal.entity.MealEntity; @@ -9,9 +12,11 @@ public interface JpaMealRepository extends JpaRepository { - boolean existsByMealName(String mealName); - - boolean existsByMealNameAndMealIdNot(String mealName, Long melaId); + boolean existsByMealNameAndMealIdNot(String mealName, Long mealId); List findAllByMealOwner(MealOwnerVO mealOwnerVO); + + Optional findByMealIdAndIsDeletedFalse(Long mealId); + + boolean existsByMealNameAndIsDeletedFalse(String mealName); }