Skip to content

Commit

Permalink
[refac] divide method
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Oct 1, 2024
1 parent 7d8358c commit ac8b25d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,34 @@ public class MenuCommandService {

@Transactional
public void deleteMenu(final MenuDeleteCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
Menu findMenu = menuFinder.findById(command.id());
Store findStore = validateMenuExistInStore(command.storeId(), findMenu);
validateMenuExistInStore(findStore, findMenu);
menuDeleter.deleteMenu(findMenu);
updateLowestPriceInStore(findStore);
}

@Transactional
public void modifyMenu(final MenuPatchCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
Menu findMenu = menuFinder.findById(command.id());
Store findStore = validateMenuExistInStore(command.storeId(), findMenu);
validateMenuExistInStore(findStore, findMenu);
findMenu.update(command.name(), command.price());
updateLowestPriceInStore(findStore);
}

@Transactional
public MenuPostResponse createMenu(final MenuPostCommand request) {
Store findStore = validateConflictMenu(request.storeId(), request.name());
Menu menu = Menu.create(findStore, request.name(), request.price());
public MenuPostResponse createMenu(final MenuPostCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
validateConflictMenu(findStore, command.name());
Menu menu = Menu.create(findStore, command.name(), command.price());
menuUpdater.save(menu);
updateLowestPriceInStore(findStore, menu);
return MenuPostResponse.of(menu);
}

private void updateLowestPriceInStore(final Store store) {
int lowestPrice = menuFinder.findByStore(store).stream()
int lowestPrice = menuFinder.findAllByStore(store).stream()
.mapToInt(Menu::getPrice)
.min()
.orElse(0);
Expand All @@ -61,17 +64,13 @@ private void updateLowestPriceInStore(final Store store, final Menu menu) {
}
}

private Store validateMenuExistInStore(final long storeId, final Menu menu) {
Store store = storeFinder.findByIdWhereDeletedIsFalse(storeId);
private void validateMenuExistInStore(final Store store, final Menu menu) {
menuFinder.findByStoreAndMenu(store, menu);
return store;
}

private Store validateConflictMenu(final long storeId, final String name) {
Store store = storeFinder.findByIdWhereDeletedIsFalse(storeId);
private void validateConflictMenu(final Store store, final String name) {
menuFinder.findByStoreAndName(store, name).ifPresent(menu -> {
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU);
});
return store;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ 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,7 +11,6 @@ public enum MenuErrorCode implements ErrorCode {
MENU_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 메뉴입니다."),
ALREADY_EXISTED_MENU(HttpStatus.CONFLICT, "이미 존재하는 메뉴입니다.");


private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ 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);
}

0 comments on commit ac8b25d

Please sign in to comment.