From 51ebbb40b51f633017499882cc5bd2750ad67795 Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Tue, 7 Jan 2025 00:49:31 +0900 Subject: [PATCH] [refac] optimize query execution by reducing select queries --- .../hankkiserver/api/menu/service/MenuCommandService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 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 134d027a..96325c8e 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 @@ -49,8 +49,9 @@ public void modifyMenu(final MenuPatchCommand command) { @Transactional public MenusPostResponse createMenus(final MenusPostCommand command) { Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); + List allMenus = menuFinder.findAllByStore(findStore); List menus = command.menu().stream() - .filter(c -> !validateMenuConflict(findStore, c.name())) + .filter(c -> !validateMenuConflict(allMenus, c.name())) .map(c -> Menu.create(findStore, c.name(), c.price())) .toList(); menuUpdater.saveAll(menus); @@ -69,8 +70,8 @@ private void updateLowestPriceInStore(final Store findStore) { findStore.updateLowestPrice(menuFinder.findLowestPriceByStore(findStore)); } - private boolean validateMenuConflict(final Store store, final String menuName) { - return menuFinder.existsByStoreAndName(store, menuName); + private boolean validateMenuConflict(final List menus, final String menuName) { + return menus.stream().anyMatch(menu -> menu.getName().equals(menuName)); } private void checkNoMenuInStore(final Store store, final long userId) {