From 51ebbb40b51f633017499882cc5bd2750ad67795 Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Tue, 7 Jan 2025 00:49:31 +0900 Subject: [PATCH 1/4] [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) { From 9d8f20d6a68462e399ab594eff9e035c8f1979d1 Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Wed, 8 Jan 2025 16:39:28 +0900 Subject: [PATCH 2/4] [refac] improve nested loop --- .../api/menu/service/MenuCommandService.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 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 96325c8e..0d2afe2e 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 @@ -1,6 +1,8 @@ package org.hankki.hankkiserver.api.menu.service; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand; import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand; @@ -49,11 +51,7 @@ 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(allMenus, c.name())) - .map(c -> Menu.create(findStore, c.name(), c.price())) - .toList(); + List menus = filterNewMenu(command, findStore); menuUpdater.saveAll(menus); updateLowestPriceInStore(findStore); return MenusPostResponse.of(menus); @@ -70,8 +68,22 @@ private void updateLowestPriceInStore(final Store findStore) { findStore.updateLowestPrice(menuFinder.findLowestPriceByStore(findStore)); } - private boolean validateMenuConflict(final List menus, final String menuName) { - return menus.stream().anyMatch(menu -> menu.getName().equals(menuName)); + private List filterNewMenu(final MenusPostCommand command, final Store store) { + Set allMenuNames = parseAllMenuNames(store); + return command.menu().stream() + .filter(c -> !validateMenuConflict(allMenuNames, c.name())) + .map(c -> Menu.create(store, c.name(), c.price())) + .toList(); + } + + private Set parseAllMenuNames(final Store store) { + return menuFinder.findAllByStore(store).stream() + .map(Menu::getName) + .collect(Collectors.toSet()); + } + + private boolean validateMenuConflict(final Set menus, final String menuName) { + return menus.contains(menuName); } private void checkNoMenuInStore(final Store store, final long userId) { From 0c11e592b839d8f581aff59dd686386663b3d3e3 Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Wed, 8 Jan 2025 16:50:10 +0900 Subject: [PATCH 3/4] [refac] delete unnecessary query --- .../hankkiserver/api/menu/service/MenuCommandService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0d2afe2e..e98a662a 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 @@ -37,7 +37,7 @@ public void deleteMenu(final MenuDeleteCommand command) { Menu menu = menuFinder.findByStoreIdAndId(findStore.getId(), command.id()); menuDeleter.deleteMenu(menu); saveToDeletedMenu(menu, findStore.getId()); - updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId())); + updateLowestPriceInStore(findStore); checkNoMenuInStore(findStore, command.userId()); } From 34e5f0b7b70419a9a41928f782fb256532ed2d1a Mon Sep 17 00:00:00 2001 From: kgy1008 Date: Wed, 8 Jan 2025 16:54:41 +0900 Subject: [PATCH 4/4] [refac] minimize interdependence between methods --- .../api/menu/service/MenuCommandService.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 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 e98a662a..752f218a 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 lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand; import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand; +import org.hankki.hankkiserver.api.menu.service.command.MenuPostCommand; import org.hankki.hankkiserver.api.menu.service.command.MenusPostCommand; import org.hankki.hankkiserver.api.menu.service.response.MenusGetResponse; import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse; @@ -51,7 +52,7 @@ public void modifyMenu(final MenuPatchCommand command) { @Transactional public MenusPostResponse createMenus(final MenusPostCommand command) { Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId()); - List menus = filterNewMenu(command, findStore); + List menus = filterNotExistedMenu(command.menu(), findStore); menuUpdater.saveAll(menus); updateLowestPriceInStore(findStore); return MenusPostResponse.of(menus); @@ -68,11 +69,11 @@ private void updateLowestPriceInStore(final Store findStore) { findStore.updateLowestPrice(menuFinder.findLowestPriceByStore(findStore)); } - private List filterNewMenu(final MenusPostCommand command, final Store store) { + private List filterNotExistedMenu(final List menus, final Store store) { Set allMenuNames = parseAllMenuNames(store); - return command.menu().stream() - .filter(c -> !validateMenuConflict(allMenuNames, c.name())) - .map(c -> Menu.create(store, c.name(), c.price())) + return menus.stream() + .filter(menu -> !validateMenuConflict(allMenuNames, menu.name())) + .map(menu -> Menu.create(store, menu.name(), menu.price())) .toList(); }