From d47fde67ad93f0e46a42be06235578d0204c8d81 Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Tue, 1 Oct 2024 17:32:40 +0900 Subject: [PATCH] [fix] update lowest price --- .../api/menu/service/MenuCommandService.java | 25 +++++++++++++++++-- .../api/menu/service/MenuFinder.java | 4 +++ .../menu/repository/MenuRepository.java | 1 + .../domain/store/model/Store.java | 4 +++ 4 files changed, 32 insertions(+), 2 deletions(-) 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 1d6ef8dd..fc48fbeb 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 @@ -6,6 +6,7 @@ import org.hankki.hankkiserver.api.menu.service.command.MenuPostCommand; import org.hankki.hankkiserver.api.menu.service.response.MenuPostResponse; import org.hankki.hankkiserver.api.store.service.StoreFinder; +import org.hankki.hankkiserver.api.store.service.StoreUpdater; import org.hankki.hankkiserver.common.code.MenuErrorCode; import org.hankki.hankkiserver.common.exception.ConflictException; import org.hankki.hankkiserver.domain.menu.model.Menu; @@ -21,19 +22,24 @@ public class MenuCommandService { private final MenuFinder menuFinder; private final MenuUpdater menuUpdater; private final StoreFinder storeFinder; + private final StoreUpdater storeUpdater; @Transactional public void deleteMenu(final MenuDeleteCommand request) { + Store findStore = storeFinder.findByIdWhereDeletedIsFalse(request.storeId()); Menu findMenu = menuFinder.findById(request.id()); - validateMenuExistInStore(storeFinder.findByIdWhereDeletedIsFalse(request.storeId()), findMenu); + validateMenuExistInStore(findStore, findMenu); menuDeleter.deleteMenu(findMenu); + updateLowestPriceInStore(findStore); } @Transactional public void modifyMenu(final MenuPatchCommand request) { + Store findStore = storeFinder.findByIdWhereDeletedIsFalse(request.storeId()); Menu findMenu = menuFinder.findById(request.id()); - validateMenuExistInStore(storeFinder.findByIdWhereDeletedIsFalse(request.storeId()), findMenu); + validateMenuExistInStore(findStore, findMenu); findMenu.update(request.name(), request.price()); + updateLowestPriceInStore(findStore); } @Transactional @@ -42,9 +48,24 @@ public MenuPostResponse createMenu(final MenuPostCommand request) { validateConflictMenu(findStore, request.name()); Menu menu = Menu.create(findStore, request.name(), request.price()); menuUpdater.save(menu); + updateLowestPriceInStore(findStore, menu); return MenuPostResponse.of(menu); } + private void updateLowestPriceInStore(final Store store) { + int lowestPrice = menuFinder.findByStore(store).stream() + .mapToInt(Menu::getPrice) + .min() + .orElse(0); + store.updateLowestPrice(lowestPrice); + } + + private void updateLowestPriceInStore(final Store store, final Menu menu) { + if (store.getLowestPrice() > menu.getPrice()) { + store.updateLowestPrice(menu.getPrice()); + } + } + private void validateMenuExistInStore(final Store store, final Menu menu) { menuFinder.findByStoreAndMenu(store, menu); } 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..5902f9bb 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 @@ -26,6 +26,10 @@ protected Menu findById(final Long id) { return menuRepository.findById(id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND)); } + protected List findByStore(final Store store) { + return menuRepository.findByStoreId(store.getId()); + } + protected Menu findByStoreAndMenu(final Store store, final Menu menu) { return menuRepository.findByStoreAndId(store, menu.getId()).orElseThrow(() -> new BadRequestException(MenuErrorCode.MENU_NOT_FOUND)); } 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..52889503 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 @@ -11,4 +11,5 @@ public interface MenuRepository extends JpaRepository { List findAllByStore(Store store); Optional findByStoreAndId(Store store, Long id); Optional findByStoreAndName(Store store, String name); + List findByStoreId(long storeId); } diff --git a/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java b/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java index 1dae6986..78345239 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java +++ b/src/main/java/org/hankki/hankkiserver/domain/store/model/Store.java @@ -83,4 +83,8 @@ public void softDelete() { public String getImageUrlOrElseNull() { return images.isEmpty() ? null : images.get(0).getImageUrl(); } + + public void updateLowestPrice(int lowestPrice) { + this.lowestPrice = lowestPrice; + } }