Skip to content

Commit

Permalink
[refac] function separation
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Oct 1, 2024
1 parent 4b80ef8 commit a36f100
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,22 @@ public class MenuCommandService {

@Transactional
public void deleteMenu(final MenuDeleteCommand command) {
Menu findMenu = menuFinder.findById(command.id());
if (!validateMenuExistInStore(command.storeId(), findMenu)) {
throw new BadRequestException(MenuErrorCode.MENU_NOT_FOUND);
}
menuDeleter.deleteMenu(findMenu);
Menu menu = getValidatedMenu(command.storeId(), command.id());
menuDeleter.deleteMenu(menu);
updateLowestPriceInStore(command.storeId());
}

@Transactional
public void modifyMenu(final MenuPatchCommand command) {
Menu findMenu = menuFinder.findById(command.id());
if (!validateMenuExistInStore(command.storeId(), findMenu)) {
throw new BadRequestException(MenuErrorCode.MENU_NOT_FOUND);
}
findMenu.update(command.name(), command.price());
Menu menu = getValidatedMenu(command.storeId(), command.id());
menu.update(command.name(), command.price());
updateLowestPriceInStore(command.storeId());
}

@Transactional
public MenuPostResponse createMenu(final MenuPostCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
if (validateConflictMenu(findStore, command.name())) {
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU);
}
validateMenuNotConflict(findStore, command.name());
Menu menu = Menu.create(findStore, command.name(), command.price());
menuUpdater.save(menu);
updateLowestPriceInStore(findStore, menu);
Expand All @@ -70,11 +62,21 @@ private void updateLowestPriceInStore(final Store store, final Menu menu) {
}
}

private Menu getValidatedMenu(long storeId, long menuId) {
Menu menu = menuFinder.findById(menuId);
if (!validateMenuExistInStore(storeId, menu)) {
throw new BadRequestException(MenuErrorCode.MENU_NOT_FOUND);
}
return menu;
}

private boolean validateMenuExistInStore(final long storeId, final Menu menu) {
return storeId == menu.getStore().getId();
}

private boolean validateConflictMenu(final Store store, final String name) {
return menuFinder.existsByStoreAndName(store, name);
private void validateMenuNotConflict(Store store, String menuName) {
if (menuFinder.existsByStoreAndName(store, menuName)) {
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU);
}
}
}

0 comments on commit a36f100

Please sign in to comment.