Skip to content

Commit

Permalink
[refac] 메뉴 추가 시 발생 쿼리문 개선 (#244)
Browse files Browse the repository at this point in the history
* [refac] optimize query execution by reducing select queries

* [refac] improve nested loop

* [refac] delete unnecessary query

* [refac] minimize interdependence between methods
  • Loading branch information
kgy1008 authored Jan 11, 2025
1 parent d21b4a3 commit f7f125f
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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;
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;
Expand Down Expand Up @@ -35,7 +38,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());
}

Expand All @@ -49,10 +52,7 @@ public void modifyMenu(final MenuPatchCommand command) {
@Transactional
public MenusPostResponse createMenus(final MenusPostCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
List<Menu> menus = command.menu().stream()
.filter(c -> !validateMenuConflict(findStore, c.name()))
.map(c -> Menu.create(findStore, c.name(), c.price()))
.toList();
List<Menu> menus = filterNotExistedMenu(command.menu(), findStore);
menuUpdater.saveAll(menus);
updateLowestPriceInStore(findStore);
return MenusPostResponse.of(menus);
Expand All @@ -69,8 +69,22 @@ 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 List<Menu> filterNotExistedMenu(final List<MenuPostCommand> menus, final Store store) {
Set<String> allMenuNames = parseAllMenuNames(store);
return menus.stream()
.filter(menu -> !validateMenuConflict(allMenuNames, menu.name()))
.map(menu -> Menu.create(store, menu.name(), menu.price()))
.toList();
}

private Set<String> parseAllMenuNames(final Store store) {
return menuFinder.findAllByStore(store).stream()
.map(Menu::getName)
.collect(Collectors.toSet());
}

private boolean validateMenuConflict(final Set<String> menus, final String menuName) {
return menus.contains(menuName);
}

private void checkNoMenuInStore(final Store store, final long userId) {
Expand Down

0 comments on commit f7f125f

Please sign in to comment.