diff --git a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java index b34d4ca6..6762539d 100644 --- a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java +++ b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuCommandService.java @@ -7,6 +7,7 @@ import org.hankki.hankkiserver.api.menu.service.response.MenuPostResponse; import org.hankki.hankkiserver.api.store.service.StoreFinder; import org.hankki.hankkiserver.common.code.MenuErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; import org.hankki.hankkiserver.common.exception.ConflictException; import org.hankki.hankkiserver.domain.menu.model.Menu; import org.hankki.hankkiserver.domain.store.model.Store; @@ -24,38 +25,43 @@ public class MenuCommandService { @Transactional public void deleteMenu(final MenuDeleteCommand command) { - Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); Menu findMenu = menuFinder.findById(command.id()); - validateMenuExistInStore(findStore, findMenu); + if (!validateMenuExistInStore(command.storeId(), findMenu)) { + throw new BadRequestException(MenuErrorCode.MENU_NOT_FOUND); + } menuDeleter.deleteMenu(findMenu); - updateLowestPriceInStore(findStore); + updateLowestPriceInStore(command.storeId()); } @Transactional public void modifyMenu(final MenuPatchCommand command) { - Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); Menu findMenu = menuFinder.findById(command.id()); - validateMenuExistInStore(findStore, findMenu); + if (!validateMenuExistInStore(command.storeId(), findMenu)) { + throw new BadRequestException(MenuErrorCode.MENU_NOT_FOUND); + } findMenu.update(command.name(), command.price()); - updateLowestPriceInStore(findStore); + updateLowestPriceInStore(command.storeId()); } @Transactional public MenuPostResponse createMenu(final MenuPostCommand command) { Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); - validateConflictMenu(findStore, command.name()); + if (validateConflictMenu(findStore, command.name())) { + throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU); + } Menu menu = Menu.create(findStore, command.name(), command.price()); menuUpdater.save(menu); updateLowestPriceInStore(findStore, menu); return MenuPostResponse.of(menu); } - private void updateLowestPriceInStore(final Store store) { - int lowestPrice = menuFinder.findAllByStore(store).stream() + private void updateLowestPriceInStore(final long storeId) { + Store findStore = storeFinder.findByIdWhereDeletedIsFalse(storeId); + int lowestPrice = menuFinder.findAllByStore(findStore).stream() .mapToInt(Menu::getPrice) .min() .orElse(0); - store.updateLowestPrice(lowestPrice); + findStore.updateLowestPrice(lowestPrice); } private void updateLowestPriceInStore(final Store store, final Menu menu) { @@ -64,13 +70,11 @@ private void updateLowestPriceInStore(final Store store, final Menu menu) { } } - private void validateMenuExistInStore(final Store store, final Menu menu) { - menuFinder.findByStoreAndMenu(store, menu); + private boolean validateMenuExistInStore(final long storeId, final Menu menu) { + return storeId == menu.getStore().getId(); } - private void validateConflictMenu(final Store store, final String name) { - menuFinder.findByStoreAndName(store, name).ifPresent(menu -> { - throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU); - }); + private boolean validateConflictMenu(final Store store, final String name) { + return menuFinder.existsByStoreAndName(store, name); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java index 9386fc26..7422c2c5 100644 --- a/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/menu/service/MenuFinder.java @@ -2,7 +2,6 @@ import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.common.code.MenuErrorCode; -import org.hankki.hankkiserver.common.exception.BadRequestException; import org.hankki.hankkiserver.common.exception.NotFoundException; import org.hankki.hankkiserver.domain.menu.model.Menu; import org.hankki.hankkiserver.domain.menu.repository.MenuRepository; @@ -10,7 +9,6 @@ import org.springframework.stereotype.Component; import java.util.List; -import java.util.Optional; @Component @RequiredArgsConstructor @@ -26,11 +24,7 @@ protected Menu findById(final Long id) { return menuRepository.findById(id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND)); } - protected Menu findByStoreAndMenu(final Store store, final Menu menu) { - return menuRepository.findByStoreAndId(store, menu.getId()).orElseThrow(() -> new BadRequestException(MenuErrorCode.MENU_NOT_FOUND)); - } - - protected Optional findByStoreAndName(final Store store, final String name) { - return menuRepository.findByStoreAndName(store, name); + protected boolean existsByStoreAndName(final Store store, final String name) { + return menuRepository.existsByStoreAndName(store, name); } } diff --git a/src/main/java/org/hankki/hankkiserver/domain/menu/repository/MenuRepository.java b/src/main/java/org/hankki/hankkiserver/domain/menu/repository/MenuRepository.java index 9f42f050..0e8b16f1 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/menu/repository/MenuRepository.java +++ b/src/main/java/org/hankki/hankkiserver/domain/menu/repository/MenuRepository.java @@ -5,10 +5,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; -import java.util.Optional; public interface MenuRepository extends JpaRepository { List findAllByStore(Store store); - Optional findByStoreAndId(Store store, Long id); - Optional findByStoreAndName(Store store, String name); + boolean existsByStoreAndName(Store store, String name); }