Skip to content

Commit

Permalink
[fix] update lowest price
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Oct 1, 2024
1 parent 0b1e741 commit d47fde6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ protected Menu findById(final Long id) {
return menuRepository.findById(id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND));
}

protected List<Menu> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface MenuRepository extends JpaRepository<Menu, Long> {
List<Menu> findAllByStore(Store store);
Optional<Menu> findByStoreAndId(Store store, Long id);
Optional<Menu> findByStoreAndName(Store store, String name);
List<Menu> findByStoreId(long storeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit d47fde6

Please sign in to comment.