Skip to content

Commit

Permalink
feat: dish, meal에 soft delete 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
somln committed Oct 1, 2024
1 parent 1ba7b4e commit 2794f30
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public List<Dish> readAll(DishCategory dishCategory) {
public List<Dish> search(String keyword) {
return dishRepository.findAllByKeyword(keyword);
}

public Dish readActiveDish(TargetDish targetDish) {
return dishRepository.findActiveDishById(targetDish);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void saveDish(

List<Dish> findAllByKeyword(String keyword);

List<DishImage> findAllDishImageByDishId(TargetDish targetDish);
Dish findActiveDishById(TargetDish targetDish);

void deleteDishImages(List<DishImage> dishImages);
void findAllActiveDishById(List<Long> dishIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public Meal read(TargetMeal targetMeal) {
public List<Meal> read(MealOwner mealOwner) {
return mealRepository.findAllByMealOwner(mealOwner);
}

public Meal readActiveMeal(TargetMeal targetMeal) {
return mealRepository.findActiveMealById(targetMeal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class MealRemover {
private final MealRepository mealRepository;
private final MealValidator mealValidator;

public void remove(MealOwner mealOwner, Meal meal, List<MealDish> mealDishes) {
public void remove(MealOwner mealOwner, Meal meal) {
mealValidator.validate(mealOwner, meal);
mealRepository.delete(meal, mealDishes);
mealRepository.delete(meal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ public interface MealRepository {

void update(Meal meal, MealContent mealContent);

void delete(Meal meal, List<MealDish> mealDishes);
void delete(Meal meal);

List<MealDish> findAllMealDishByMealId(TargetMeal targetMeal);

boolean exitsByNameExcludingTargetMeal(Meal meal, String mealName);

List<Meal> findAllByMealOwner(MealOwner mealOwner);

void saveMealDish(MealDish updatedMealDish);

void delete(MealDish mealDish);

void updateMealDish(MealDish mealDish, Long dishId);

Meal findActiveMealById(TargetMeal targetMeal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MealDish> mealDishes = mealDishReader.read(targetMeal);
mealUpdater.update(mealOwner, meal, mealDishes, updatedMealContent);
}

@Transactional
public void deleteMeal(MealOwner mealOwner, TargetMeal targetMeal) {
Meal meal = mealReader.read(targetMeal);
List<MealDish> mealDishes = mealDishReader.read(targetMeal);
mealRemover.remove(mealOwner, meal, mealDishes);
Meal meal = mealReader.readActiveMeal(targetMeal);
mealRemover.remove(mealOwner, meal);
}

public MealWithDishes readMealAndDishes(TargetMeal targetMeal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +50,7 @@ public static DishEntity of(Long dishStoreId, Long dishCategoryId, DishContent d
.dishState(dishState)
.dishStoreId(dishStoreId)
.dishCategoryId(dishCategoryId)
.isDeleted(false)
.build();
}

Expand Down Expand Up @@ -91,4 +94,8 @@ public void decreaseQuantity(int amount) {
}
}
}

public void delete() {
this.isDeleted = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,7 @@ public void saveDish(

@Override
public void deleteDish(Dish dish) {
jpaDishRepository.deleteById(dish.getDishId());
List<DishImage> dishImages = findAllDishImageByDishId(TargetDish.from(dish.getDishId()));
deleteDishImages(dishImages);
}

@Override
public void deleteDishImages(List<DishImage> dishImages) {
dishImages.forEach(
dishImage -> {
Long imageId = dishImage.imageId();
jpaImageRepository.deleteById(imageId);
});

dishImages.forEach(dishImage -> jpaDishImageRepository.deleteByDishId(dishImage.dishId()));
jpaDishRepository.getReferenceById(dish.getDishId()).delete();
}

@Override
Expand All @@ -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
Expand Down Expand Up @@ -124,21 +111,28 @@ public List<DishCategory> findAllCategories() {
.toList();
}

@Override
public List<DishImage> findAllDishImageByDishId(TargetDish targetDish) {
return jpaDishImageRepository.findAllByDishId(targetDish.dishId()).stream()
.map(DishImageEntity::toDomain)
public List<Dish> findAllByKeyword(String keyword) {
return jpaDishRepository.findByIsDeletedFalseAndDishNameContaining(keyword).stream()
.map(this::toDishWithImage)
.toList();
}

public List<Dish> 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<Long> dishIds) {
jpaDishRepository.findAllByDishIdInAndIsDeletedFalse(dishIds).stream()
.map(this::toDishWithImage)
.toList();
}

public List<Dish> findAllByCategory(DishCategory dishCategory) {
return jpaDishRepository.findByDishCategoryId(dishCategory.getCategoryId()).stream()
return jpaDishRepository.findByDishCategoryIdAndIsDeletedFalse(dishCategory.getCategoryId()).stream()
.map(this::toDishWithImage)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DishEntity, Long> {

List<DishEntity> findByDishCategoryId(Long categoryId);

boolean existsByDishStoreIdAndDishName(Long dishStoreId, String dishName);

boolean existsByDishStoreIdAndDishNameAndDishIdNot(
Long dishStoreId, String dishName, Long dishId);

List<DishEntity> findByDishNameContaining(String keyword);
List<DishEntity> findByIsDeletedFalseAndDishNameContaining(String keyword);

Optional<DishEntity> findByDishIdAndIsDeletedFalse(Long aLong);

Optional<DishEntity> findByDishCategoryIdAndIsDeletedFalse(Long categoryId);

boolean existsByDishStoreIdAndDishNameAndIsDeletedFalse(Long storeId, String dishName);

List<DishEntity> findAllByDishIdInAndIsDeletedFalse(List<Long> dishIds);

;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -62,4 +65,8 @@ public void update(MealInfo mealInfo) {
this.mealName = mealInfo.mealName();
this.mealPrice = mealInfo.mealPrice();
}

public void delete() {
this.isDeleted = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CoreMealRepository implements MealRepository {

@Override
public boolean exitsByMealName(String mealName) {
return jpaMealRepository.existsByMealName(mealName);
return jpaMealRepository.existsByMealNameAndIsDeletedFalse(mealName);
}

@Override
Expand All @@ -46,13 +46,10 @@ public List<Meal> 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
Expand Down Expand Up @@ -82,9 +79,8 @@ public void update(Meal meal, MealContent updatedMealContent) {
}

@Override
public void delete(Meal meal, List<MealDish> mealDishes) {
jpaMealRepository.deleteById(meal.getMealId());
mealDishes.forEach(mealDish -> jpaMealDishRepository.deleteById(mealDish.mealDishId()));
public void delete(Meal meal) {
jpaMealRepository.getReferenceById(meal.getMealId()).delete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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;
import core.startup.mealtoktok.infra.meal.entity.MealOwnerVO;

public interface JpaMealRepository extends JpaRepository<MealEntity, Long> {

boolean existsByMealName(String mealName);

boolean existsByMealNameAndMealIdNot(String mealName, Long melaId);
boolean existsByMealNameAndMealIdNot(String mealName, Long mealId);

List<MealEntity> findAllByMealOwner(MealOwnerVO mealOwnerVO);

Optional<MealEntity> findByMealIdAndIsDeletedFalse(Long mealId);

boolean existsByMealNameAndIsDeletedFalse(String mealName);
}

0 comments on commit 2794f30

Please sign in to comment.