Skip to content

Commit

Permalink
[fix] change logic for add menus and update lowest price
Browse files Browse the repository at this point in the history
  • Loading branch information
kgy1008 committed Oct 8, 2024
1 parent fd916f7 commit 5b48fdd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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.MenuListPostResponse;
import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse;
import org.hankki.hankkiserver.api.store.controller.request.MenuPostRequest;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
import org.springframework.web.bind.annotation.*;
Expand All @@ -35,11 +35,11 @@ public HankkiResponse<Void> updateMenu(@PathVariable("storeId") final Long store
return HankkiResponse.success(CommonSuccessCode.OK);
}

@PostMapping("/{storeId}/menus")
public HankkiResponse<MenuListPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final List<MenuPostRequest> requests) {
List<MenuPostCommand> commands = requests.stream()
.map(request -> MenuPostCommand.of(request.name(), request.price()))
@PostMapping("{storeId}/menus/bulk")
public HankkiResponse<MenusPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final List<MenuPostRequest> request) {
List<MenuPostCommand> command = request.stream()
.map(r -> MenuPostCommand.of(r.name(), r.price()))
.toList();
return HankkiResponse.success(CommonSuccessCode.CREATED, menuCommandService.createMenus(MenusPostCommand.of(storeId, commands)));
return HankkiResponse.success(CommonSuccessCode.CREATED, menuCommandService.createMenus(MenusPostCommand.of(storeId, command)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
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.MenusPostCommand;
import org.hankki.hankkiserver.api.menu.service.response.MenuListPostResponse;
import org.hankki.hankkiserver.api.menu.service.response.MenuPostResponse;
import org.hankki.hankkiserver.api.menu.service.response.MenusPostResponse;
import org.hankki.hankkiserver.api.store.service.StoreFinder;
import org.hankki.hankkiserver.common.code.MenuErrorCode;
import org.hankki.hankkiserver.common.exception.ConflictException;
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -40,38 +38,26 @@ public void modifyMenu(final MenuPatchCommand command) {
}

@Transactional
public MenuListPostResponse createMenus(final MenusPostCommand commands) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(commands.storeId());
List<MenuPostResponse> menus = commands.menu().stream()
.map(command -> {
validateMenuNotConflict(findStore, command.name());
Menu menu = Menu.create(findStore, command.name(), command.price());
public MenusPostResponse createMenus(final MenusPostCommand command) {
Store findStore = storeFinder.findByIdWhereDeletedIsFalse(command.storeId());
List<MenuPostResponse> menus = command.menu().stream()
.filter(c -> !validateMenuConflict(findStore, c.name()))
.map(c -> {
Menu menu = Menu.create(findStore, c.name(), c.price());
menuUpdater.save(menu);
return MenuPostResponse.of(menu);
})
.toList();
updateLowestPriceInStore(findStore);
return MenuListPostResponse.of(menus);
return MenusPostResponse.of(menus);
}


private void updateLowestPriceInStore(final Store findStore) {
int lowestPrice = menuFinder.findAllByStore(findStore).stream()
.mapToInt(Menu::getPrice)
.min()
.orElse(0);
findStore.updateLowestPrice(lowestPrice);
findStore.updateLowestPrice(menuFinder.findLowestPriceByStore(findStore));
}

private void updateLowestPriceInStore(final Store store, final Menu menu) {
if (store.getLowestPrice() > menu.getPrice()) {
store.updateLowestPrice(menu.getPrice());
}
}

private void validateMenuNotConflict(Store store, String menuName) {
if (menuFinder.existsByStoreAndName(store, menuName)) {
throw new ConflictException(MenuErrorCode.ALREADY_EXISTED_MENU);
}
private boolean validateMenuConflict(Store store, String menuName) {
return menuFinder.existsByStoreAndName(store, menuName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ protected Menu findByStoreIdAndId(final Long storeId, final Long id) {
protected boolean existsByStoreAndName(final Store store, final String name) {
return menuRepository.existsByStoreAndName(store, name);
}

protected int findLowestPriceByStore(Store store) {
return menuRepository.findLowestPriceByStore(store);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hankki.hankkiserver.api.menu.service.response;

import java.util.List;

public record MenusPostResponse(
List<MenuPostResponse> menuList
) {
public static MenusPostResponse of(List<MenuPostResponse> menus) {
return new MenusPostResponse(menus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.hankki.hankkiserver.domain.menu.model.Menu;
import org.hankki.hankkiserver.domain.store.model.Store;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;
Expand All @@ -11,4 +12,7 @@ public interface MenuRepository extends JpaRepository<Menu, Long> {
List<Menu> findAllByStore(Store store);
Optional<Menu> findByStoreIdAndId(Long storeId, Long id);
boolean existsByStoreAndName(Store store, String name);

@Query("SELECT MIN(m.price) FROM Menu m WHERE m.store = :store")
int findLowestPriceByStore(Store store);
}

0 comments on commit 5b48fdd

Please sign in to comment.