Skip to content

Commit

Permalink
[refac] modify verification method
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Oct 1, 2024
1 parent ac8b25d commit 4b80ef8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.hankki.hankkiserver.api.menu.service.response.MenuPostResponse;
import org.hankki.hankkiserver.api.store.service.StoreFinder;
import org.hankki.hankkiserver.common.code.MenuErrorCode;
import org.hankki.hankkiserver.common.exception.BadRequestException;
import org.hankki.hankkiserver.common.exception.ConflictException;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.store.model.Store;
Expand All @@ -24,38 +25,43 @@ public class MenuCommandService {

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

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

@Transactional
public MenuPostResponse createMenu(final MenuPostCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
validateConflictMenu(findStore, command.name());
if (validateConflictMenu(findStore, command.name())) {
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU);
}
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.findAllByStore(store).stream()
private void updateLowestPriceInStore(final long storeId) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(storeId);
int lowestPrice = menuFinder.findAllByStore(findStore).stream()
.mapToInt(Menu::getPrice)
.min()
.orElse(0);
store.updateLowestPrice(lowestPrice);
findStore.updateLowestPrice(lowestPrice);
}

private void updateLowestPriceInStore(final Store store, final Menu menu) {
Expand All @@ -64,13 +70,11 @@ private void updateLowestPriceInStore(final Store store, final Menu menu) {
}
}

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

private void validateConflictMenu(final Store store, final String name) {
menuFinder.findByStoreAndName(store, name).ifPresent(menu -> {
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU);
});
private boolean validateConflictMenu(final Store store, final String name) {
return menuFinder.existsByStoreAndName(store, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.code.MenuErrorCode;
import org.hankki.hankkiserver.common.exception.BadRequestException;
import org.hankki.hankkiserver.common.exception.NotFoundException;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.menu.repository.MenuRepository;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
@RequiredArgsConstructor
Expand All @@ -26,11 +24,7 @@ protected Menu findById(final Long id) {
return menuRepository.findById(id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND));
}

protected Menu findByStoreAndMenu(final Store store, final Menu menu) {
return menuRepository.findByStoreAndId(store, menu.getId()).orElseThrow(() -> new BadRequestException(MenuErrorCode.MENU_NOT_FOUND));
}

protected Optional<Menu> findByStoreAndName(final Store store, final String name) {
return menuRepository.findByStoreAndName(store, name);
protected boolean existsByStoreAndName(final Store store, final String name) {
return menuRepository.existsByStoreAndName(store, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

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);
boolean existsByStoreAndName(Store store, String name);
}

0 comments on commit 4b80ef8

Please sign in to comment.