Skip to content

Commit

Permalink
[fix] 메뉴 추가 API (#202)
Browse files Browse the repository at this point in the history
* [fix] add multiple menus

* [fix] change logic for add menus and update lowest price

* [refac] change to storage logic bulk operation
  • Loading branch information
kgy1008 authored Oct 9, 2024
1 parent f3d4c4d commit 1b7de3b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion server-yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
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.response.MenuPostResponse;
import org.hankki.hankkiserver.api.menu.service.command.MenusPostCommand;
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.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
Expand All @@ -32,8 +35,11 @@ public HankkiResponse<Void> updateMenu(@PathVariable("storeId") final Long store
return HankkiResponse.success(CommonSuccessCode.OK);
}

@PostMapping("/{storeId}/menus")
public HankkiResponse<MenuPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final MenuPostRequest request) {
return HankkiResponse.success(CommonSuccessCode.CREATED, menuCommandService.createMenu(MenuPostCommand.of(storeId, 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, command)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
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.response.MenuPostResponse;
import org.hankki.hankkiserver.api.menu.service.command.MenusPostCommand;
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;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuCommandService {
Expand All @@ -37,32 +37,22 @@ public void modifyMenu(final MenuPatchCommand command) {
}

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

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

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);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.hankki.hankkiserver.api.menu.service.command;

public record MenuPostCommand(
long storeId,
String name,
int price
) {
public static MenuPostCommand of(final long storeId, final String name, final int price) {
return new MenuPostCommand(storeId, name, price);
public static MenuPostCommand of(final String name, final int price) {
return new MenuPostCommand(name, price);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hankki.hankkiserver.api.menu.service.command;

import java.util.List;

public record MenusPostCommand (
long storeId,
List<MenuPostCommand> menu
){
public static MenusPostCommand of (final long storeId, final List<MenuPostCommand> menu) {
return new MenusPostCommand(storeId, menu);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.hankki.hankkiserver.api.menu.service.response;

import org.hankki.hankkiserver.domain.menu.model.Menu;

import java.util.List;

public record MenusPostResponse(
List<MenuPostResponse> menuList
) {
public static MenusPostResponse of(List<Menu> menus) {
List<MenuPostResponse> menuResponses = menus.stream()
.map(MenuPostResponse::of)
.toList();
return new MenusPostResponse(menuResponses);
}
}
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 1b7de3b

Please sign in to comment.