Skip to content

Commit

Permalink
[fix] 식당에 메뉴 1개 남았을 때 메뉴 삭제 시, 식당 삭제 (#216)
Browse files Browse the repository at this point in the history
* [refac] delete store when menu does not exist

* [refactor] change wrapper type to primitive

* [refactor] add path variable validation

* [refactor] change method name
  • Loading branch information
kgy1008 authored Nov 12, 2024
1 parent 04e70b6 commit 68baf50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.hankki.hankkiserver.api.menu.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.dto.HankkiResponse;
import org.hankki.hankkiserver.api.menu.service.MenuCommandService;
Expand All @@ -11,32 +13,41 @@
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;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
@Validated
public class MenuController {

private final MenuCommandService menuCommandService;

@DeleteMapping("/{storeId}/menus/{id}")
public HankkiResponse<Void> deleteMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id) {
public HankkiResponse<Void> deleteMenu(@PathVariable("storeId") @Min(value = 1L) final long storeId,
@PathVariable("id") @Min(value = 1L) final long id) {
menuCommandService.deleteMenu(MenuDeleteCommand.of(storeId, id));
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}

@PatchMapping("/{storeId}/menus/{id}")
public HankkiResponse<Void> updateMenu(@PathVariable("storeId") final Long storeId, @PathVariable("id") final Long id,
public HankkiResponse<Void> updateMenu(@PathVariable("storeId") @Min(value = 1L) final long storeId,
@PathVariable("id") @Min(value = 1L) final long id,
@Valid @RequestBody final MenuPostRequest request) {
menuCommandService.modifyMenu(MenuPatchCommand.of(storeId, id, request.name(), request.price()));
return HankkiResponse.success(CommonSuccessCode.OK);
}

@PostMapping("{storeId}/menus/bulk")
public HankkiResponse<MenusPostResponse> createMenu(@PathVariable final Long storeId, @Valid @RequestBody final List<MenuPostRequest> request) {
public HankkiResponse<MenusPostResponse> createMenus(@PathVariable @Min(value = 1L) final long storeId,
@Valid @RequestBody final List<MenuPostRequest> request) {
List<MenuPostCommand> command = request.stream()
.map(r -> MenuPostCommand.of(r.name(), r.price()))
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hankki.hankkiserver.api.menu.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.menu.service.command.MenuDeleteCommand;
import org.hankki.hankkiserver.api.menu.service.command.MenuPatchCommand;
Expand All @@ -11,8 +12,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuCommandService {
Expand All @@ -26,6 +25,10 @@ public class MenuCommandService {
public void deleteMenu(final MenuDeleteCommand command) {
Menu menu = menuFinder.findByStoreIdAndId(command.storeId(), command.id());
menuDeleter.deleteMenu(menu);
if (emptyMenuInStore(command.storeId())) {
deleteStore(command.storeId());
return;
}
updateLowestPriceInStore(storeFinder.findByIdWhereDeletedIsFalse(command.storeId()));
}

Expand All @@ -48,11 +51,21 @@ public MenusPostResponse createMenus(final MenusPostCommand command) {
return MenusPostResponse.of(menus);
}

private void deleteStore(final long id) {
Store store = storeFinder.findByIdWhereDeletedIsFalse(id);
store.updateLowestPrice(0);
store.softDelete();
}

private void updateLowestPriceInStore(final Store findStore) {
findStore.updateLowestPrice(menuFinder.findLowestPriceByStore(findStore));
}

private boolean validateMenuConflict(Store store, String menuName) {
private boolean validateMenuConflict(final Store store, final String menuName) {
return menuFinder.existsByStoreAndName(store, menuName);
}
}

private boolean emptyMenuInStore(final long storeId) {
return !menuFinder.existsByStoreId(storeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ public List<Menu> findAllByStore(final Store store) {
return menuRepository.findAllByStore(store);
}

protected Menu findByStoreIdAndId(final Long storeId, final Long id) {
protected Menu findByStoreIdAndId(final long storeId, final long id) {
return menuRepository.findByStoreIdAndId(storeId,id).orElseThrow(() -> new NotFoundException(MenuErrorCode.MENU_NOT_FOUND));
}

protected boolean existsByStoreAndName(final Store store, final String name) {
return menuRepository.existsByStoreAndName(store, name);
}

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

protected boolean existsByStoreId(final long storeId) {
return menuRepository.existsByStoreId(storeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

public interface MenuRepository extends JpaRepository<Menu, Long> {
List<Menu> findAllByStore(Store store);
Optional<Menu> findByStoreIdAndId(Long storeId, Long id);
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);

boolean existsByStoreId(long storeId);
}

0 comments on commit 68baf50

Please sign in to comment.