Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 음식 상품 수량 수정 #108

Merged
merged 23 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3414520
[feat] MenuRepository 수정
kimhyun5u Aug 18, 2024
8e0cc9e
[test] UpdateMenuStockServiceTest 구현
kimhyun5u Aug 18, 2024
40507a9
[feat] UpdateMenuStockService 구현
kimhyun5u Aug 18, 2024
6d9ce65
[feat] UpdateMenuStockCommand 구현
kimhyun5u Aug 18, 2024
9fa5676
[feat] StoreNotOpenException 구현
kimhyun5u Aug 18, 2024
d39462c
[feat] NotEqualsOwnerException 구현
kimhyun5u Aug 18, 2024
2ec51f3
[feat] InvalidMenuStockUpdateException 구현
kimhyun5u Aug 18, 2024
76d638d
[feat] MenuErrorCode 수정
kimhyun5u Aug 18, 2024
ef93db6
[fix] MenuRepository 수정
kimhyun5u Aug 18, 2024
ff2ba69
[fix] UpdateMenuStockService 수정
kimhyun5u Aug 18, 2024
311b1b3
[fix] MenuErrorCode 수정
kimhyun5u Aug 18, 2024
bdf6698
[fix] NotUpdatableTimeException 구현
kimhyun5u Aug 18, 2024
14a7cd8
[test] MenuApiControllerTest 구현
kimhyun5u Aug 18, 2024
43f2777
[feat] MenuApiController 구현
kimhyun5u Aug 18, 2024
ef191b6
[feat] MenuExceptionHandler 구현
kimhyun5u Aug 18, 2024
2c8851f
[feat] UpdateMenuStockRequest 구현
kimhyun5u Aug 18, 2024
c58293e
[feat] UpdateMenuStockResponse 구현
kimhyun5u Aug 18, 2024
14ab35d
[docs] UpdateMenuStockResponse 수정
kimhyun5u Aug 18, 2024
9b4a2a2
[fix] Http Method 수정
kimhyun5u Aug 18, 2024
9b0e736
[fix] Http Method 수정
kimhyun5u Aug 18, 2024
460088f
[merge] remote-tracking branch 'origin/main' into feature/101_kimhyun…
kimhyun5u Aug 18, 2024
394301f
[docs] UpdateMenuStockService 수정
kimhyun5u Aug 18, 2024
7b52d4b
[merge] remote-tracking branch 'origin/main' into feature/101_kimhyun…
kimhyun5u Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package camp.woowak.lab.menu.exception;

import camp.woowak.lab.common.exception.ConflictException;

public class InvalidMenuStockUpdateException extends ConflictException {
public InvalidMenuStockUpdateException(String message) {
super(MenuErrorCode.INVALID_UPDATE_MENU_STOCK, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public enum MenuErrorCode implements ErrorCode {

NOT_FOUND_MENU(HttpStatus.BAD_REQUEST, "m_8", "메뉴를 찾을 수 없습니다."),
NOT_FOUND_MENU_CATEGORY(HttpStatus.BAD_REQUEST, "m_9", "메뉴 카테고리를 찾을 수 없습니다."),
NOT_ENOUGH_STOCK(HttpStatus.BAD_REQUEST, "M4", "재고가 부족합니다.");
NOT_ENOUGH_STOCK(HttpStatus.BAD_REQUEST, "M4", "재고가 부족합니다."),

INVALID_UPDATE_MENU_STOCK(HttpStatus.CONFLICT, "m_10", "메뉴의 재고를 변경할 수 없습니다."),

NOT_EQUALS_OWNER(HttpStatus.BAD_REQUEST, "m_11", "매장의 점주와 일치하지 않습니다."),
NOT_UPDATABLE_TIME(HttpStatus.CONFLICT, "m_12", "메뉴를 변경할 수 없는 시간입니다."),
;

private final int status;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package camp.woowak.lab.menu.exception;

import camp.woowak.lab.common.exception.BadRequestException;

public class NotEqualsOwnerException extends BadRequestException {
public NotEqualsOwnerException(String message) {
super(MenuErrorCode.NOT_EQUALS_OWNER, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package camp.woowak.lab.menu.exception;

import camp.woowak.lab.common.exception.ConflictException;

public class NotUpdatableTimeException extends ConflictException {
public NotUpdatableTimeException(String message) {
super(MenuErrorCode.NOT_UPDATABLE_TIME, message);
}
}
15 changes: 15 additions & 0 deletions src/main/java/camp/woowak/lab/menu/repository/MenuRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

Expand All @@ -18,4 +19,18 @@ public interface MenuRepository extends JpaRepository<Menu, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT m FROM Menu m where m.id in :ids")
List<Menu> findAllByIdForUpdate(List<Long> ids);

june-777 marked this conversation as resolved.
Show resolved Hide resolved
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT m FROM Menu m WHERE m.id = :id")
Optional<Menu> findByIdForUpdate(Long id);

/**
*
* 메뉴의 재고를 변경합니다.
* TODO: [논의] @Transactional을 Respository 단에 안둬도되는가?
* Repository 에서 직접 접근할 때 사용자가 실수해서 @Transactional 을 빼먹을 수도 있다.
*/
@Modifying
@Query("UPDATE Menu m SET m.stockCount = :stock WHERE m.id = :id")
int updateStock(@Param("id") Long id, @Param("stock") int stock);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package camp.woowak.lab.menu.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import camp.woowak.lab.cart.exception.MenuNotFoundException;
import camp.woowak.lab.menu.domain.Menu;
import camp.woowak.lab.menu.exception.InvalidMenuStockUpdateException;
import camp.woowak.lab.menu.exception.NotEqualsOwnerException;
import camp.woowak.lab.menu.exception.NotUpdatableTimeException;
import camp.woowak.lab.menu.repository.MenuRepository;
import camp.woowak.lab.menu.service.command.UpdateMenuStockCommand;

@Service
public class UpdateMenuStockService {
private final MenuRepository menuRepository;

public UpdateMenuStockService(MenuRepository menuRepository) {
this.menuRepository = menuRepository;
}

/**
*
* @throws MenuNotFoundException 메뉴를 찾을 수 없는 경우 발생한다.
* @throws NotEqualsOwnerException 메뉴를 소유한 가게의 주인이 아닌 경우 발생한다.
* @throws NotUpdatableTimeException 가게가 열려있지 않은 경우 발생한다.
* @throws InvalidMenuStockUpdateException 메뉴의 재고를 변경할 수 없는 경우 발생한다.
*/
@Transactional
public Long updateMenuStock(UpdateMenuStockCommand cmd) {
// 수량을 변경하려는 메뉴를 조회한다.
Menu targetMenu = findMenuByIdForUpdateOrThrow(cmd.menuId());

// 메뉴를 소유한 가게를 조회한다.
if (!targetMenu.getStore().isOwnedBy(cmd.vendorId())) {
throw new NotEqualsOwnerException("메뉴를 소유한 가게의 주인이 아닙니다.");
}

// 가게가 열려있는지 확인한다.
if (targetMenu.getStore().isOpen()) {
throw new NotUpdatableTimeException("가게가 열려있지 않습니다.");
kimhyun5u marked this conversation as resolved.
Show resolved Hide resolved
}

// 메뉴의 재고를 변경한다.
int modifiedRow = menuRepository.updateStock(cmd.menuId(), cmd.stock());
Hyeon-Uk marked this conversation as resolved.
Show resolved Hide resolved
if (modifiedRow != 1) { // 변경된 메뉴의 개수가 1이 아닌 경우 예외를 발생시킨다.
throw new InvalidMenuStockUpdateException("변경의 영향을 받은 메뉴의 개수가 1이 아닙니다.");
}

june-777 marked this conversation as resolved.
Show resolved Hide resolved
return targetMenu.getId();
}

private Menu findMenuByIdForUpdateOrThrow(Long menuId) {
return menuRepository.findByIdForUpdate(menuId).orElseThrow(() -> new MenuNotFoundException("메뉴를 찾을 수 없습니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package camp.woowak.lab.menu.service.command;

import java.util.UUID;

public record UpdateMenuStockCommand(Long menuId, int stock, UUID vendorId) {
}
39 changes: 39 additions & 0 deletions src/main/java/camp/woowak/lab/web/api/menu/MenuApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package camp.woowak.lab.web.api.menu;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import camp.woowak.lab.menu.service.UpdateMenuStockService;
import camp.woowak.lab.menu.service.command.UpdateMenuStockCommand;
import camp.woowak.lab.web.authentication.LoginVendor;
import camp.woowak.lab.web.authentication.annotation.AuthenticationPrincipal;
import camp.woowak.lab.web.dto.request.menu.UpdateMenuStockRequest;
import camp.woowak.lab.web.dto.response.menu.UpdateMenuStockResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class MenuApiController {
private final UpdateMenuStockService updateMenuStockService;

public MenuApiController(UpdateMenuStockService updateMenuStockService) {
this.updateMenuStockService = updateMenuStockService;
}

@ResponseStatus(HttpStatus.OK)
@PatchMapping("/menus/stock")
public UpdateMenuStockResponse updateMenuStock(@AuthenticationPrincipal LoginVendor vendor,
@Valid @RequestBody UpdateMenuStockRequest request) {
UpdateMenuStockCommand cmd = new UpdateMenuStockCommand(request.menuId(), request.stock(), vendor.getId());

Long updatedId = updateMenuStockService.updateMenuStock(cmd);

log.info("메뉴 재고 업데이트 완료: menuId={}, newStock={}, vendorId={}", updatedId, request.stock(), vendor.getId());

return new UpdateMenuStockResponse(updatedId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package camp.woowak.lab.web.api.menu;

import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ExceptionHandler;

import camp.woowak.lab.common.advice.DomainExceptionHandler;
import camp.woowak.lab.common.exception.HttpStatusException;
import camp.woowak.lab.menu.exception.InvalidMenuStockUpdateException;
import camp.woowak.lab.menu.exception.NotEqualsOwnerException;
import camp.woowak.lab.menu.exception.NotUpdatableTimeException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@DomainExceptionHandler(basePackageClasses = MenuApiController.class)
public class MenuExceptionHandler {

@ExceptionHandler(value = NotUpdatableTimeException.class)
public ProblemDetail handleInvalidMenuStockUpdateException(NotUpdatableTimeException e) {
log.warn("Conflict", e);
return getProblemDetail(HttpStatus.CONFLICT, e);
}

@ExceptionHandler(value = NotEqualsOwnerException.class)
public ProblemDetail handleNotEqualsOwnerException(NotEqualsOwnerException e) {
log.warn("Bad Request", e);
return getProblemDetail(HttpStatus.BAD_REQUEST, e);
}

@ExceptionHandler(value = InvalidMenuStockUpdateException.class)
public ProblemDetail handleInvalidMenuStockUpdateException(InvalidMenuStockUpdateException e) {
log.warn("Conflict", e);
return getProblemDetail(HttpStatus.CONFLICT, e);
}

private ProblemDetail getProblemDetail(HttpStatus status, HttpStatusException e) {
return ProblemDetail.forStatusAndDetail(status, e.errorCode().getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package camp.woowak.lab.web.dto.request.menu;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

public record UpdateMenuStockRequest(
@NotNull(message = "메뉴 ID는 필수입니다.")
Long menuId,
@Min(value = 0, message = "재고는 0 이상이어야 합니다.")
int stock) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package camp.woowak.lab.web.dto.response.menu;

public record UpdateMenuStockResponse(Long menuId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package camp.woowak.lab.menu.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.Optional;
import java.util.UUID;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import camp.woowak.lab.cart.exception.MenuNotFoundException;
import camp.woowak.lab.menu.domain.Menu;
import camp.woowak.lab.menu.exception.InvalidMenuStockUpdateException;
import camp.woowak.lab.menu.exception.NotEqualsOwnerException;
import camp.woowak.lab.menu.exception.NotUpdatableTimeException;
import camp.woowak.lab.menu.repository.MenuRepository;
import camp.woowak.lab.menu.service.command.UpdateMenuStockCommand;
import camp.woowak.lab.store.domain.Store;

@ExtendWith(MockitoExtension.class)
class UpdateMenuStockServiceTest {
@InjectMocks
private UpdateMenuStockService updateMenuStockService;

@Mock
private MenuRepository menuRepository;

@Test
@DisplayName("메뉴 재고 업데이트 테스트 - 성공")
void testUpdateMenuStock() {
// given
Long menuId = 1L;
int stock = 10;
UUID vendorId = UUID.randomUUID();
Menu fakeMenu = Mockito.mock(Menu.class);
Store fakeStore = Mockito.mock(Store.class);

given(fakeMenu.getStore()).willReturn(fakeStore);
given(fakeStore.isOwnedBy(any(UUID.class))).willReturn(true);
given(menuRepository.findByIdForUpdate(anyLong())).willReturn(Optional.of(fakeMenu));
given(fakeStore.isOpen()).willReturn(false);
given(menuRepository.updateStock(anyLong(), anyInt())).willReturn(1);

UpdateMenuStockCommand cmd = new UpdateMenuStockCommand(menuId, stock, vendorId);

// when
updateMenuStockService.updateMenuStock(cmd);

// then
verify(menuRepository, times(1)).findByIdForUpdate(anyLong());
verify(fakeMenu, times(2)).getStore();
verify(fakeStore, times(1)).isOwnedBy(any(UUID.class));
verify(fakeStore, times(1)).isOpen();
verify(menuRepository, times(1)).updateStock(anyLong(), anyInt());
}

@Test
@DisplayName("메뉴 재고 업데이트 테스트 - 메뉴를 찾을 수 없는 경우")
void testUpdateMenuStockNotFound() {
// given
Long menuId = 1L;
int stock = 10;
UUID vendorId = UUID.randomUUID();

given(menuRepository.findByIdForUpdate(anyLong())).willReturn(Optional.empty());

UpdateMenuStockCommand cmd = new UpdateMenuStockCommand(menuId, stock, vendorId);

// when
// then
assertThrows(MenuNotFoundException.class, () -> updateMenuStockService.updateMenuStock(cmd));
verify(menuRepository, times(1)).findByIdForUpdate(anyLong());
verify(menuRepository, never()).updateStock(anyLong(), anyInt());
}

@Test
@DisplayName("메뉴 재고 업데이트 테스트 - 메뉴를 소유한 가게의 주인이 아닌 경우")
void testUpdateMenuStockNotEqualsOwner() {
// given
Long menuId = 1L;
int stock = 10;
UUID vendorId = UUID.randomUUID();
Menu fakeMenu = Mockito.mock(Menu.class);
Store fakeStore = Mockito.mock(Store.class);

given(fakeMenu.getStore()).willReturn(fakeStore);
given(fakeStore.isOwnedBy(any(UUID.class))).willReturn(false);
given(menuRepository.findByIdForUpdate(anyLong())).willReturn(Optional.of(fakeMenu));

UpdateMenuStockCommand cmd = new UpdateMenuStockCommand(menuId, stock, vendorId);

// when
// then
assertThrows(NotEqualsOwnerException.class, () -> updateMenuStockService.updateMenuStock(cmd));
verify(menuRepository, times(1)).findByIdForUpdate(anyLong());
verify(fakeMenu, times(1)).getStore();
verify(fakeStore, times(1)).isOwnedBy(any(UUID.class));
verify(menuRepository, never()).updateStock(anyLong(), anyInt());
}

@Test
@DisplayName("메뉴 재고 업데이트 테스트 - 매장이 열려있는 경우")
void testUpdateMenuStockStoreNotOpen() {
// given
Long menuId = 1L;
int stock = 10;
UUID vendorId = UUID.randomUUID();
Menu fakeMenu = Mockito.mock(Menu.class);
Store fakeStore = Mockito.mock(Store.class);

given(fakeMenu.getStore()).willReturn(fakeStore);
given(fakeStore.isOwnedBy(any(UUID.class))).willReturn(true);
given(fakeStore.isOpen()).willReturn(true);
given(menuRepository.findByIdForUpdate(anyLong())).willReturn(Optional.of(fakeMenu));

UpdateMenuStockCommand cmd = new UpdateMenuStockCommand(menuId, stock, vendorId);

// when
// then
assertThrows(NotUpdatableTimeException.class, () -> updateMenuStockService.updateMenuStock(cmd));
verify(menuRepository, times(1)).findByIdForUpdate(anyLong());
verify(fakeMenu, times(2)).getStore();
verify(fakeStore, times(1)).isOwnedBy(any(UUID.class));
verify(fakeStore, times(1)).isOpen();
verify(menuRepository, never()).updateStock(anyLong(), anyInt());
}

@Test
@DisplayName("메뉴 재고 업데이트 테스트 - 메뉴의 재고를 변경할 수 없는 경우")
void testUpdateMenuStockInvalid() {
// given
Long menuId = 1L;
int stock = 10;
UUID vendorId = UUID.randomUUID();
Menu fakeMenu = Mockito.mock(Menu.class);
Store fakeStore = Mockito.mock(Store.class);

given(fakeMenu.getStore()).willReturn(fakeStore);
given(fakeStore.isOwnedBy(any(UUID.class))).willReturn(true);
given(fakeStore.isOpen()).willReturn(false);
given(menuRepository.findByIdForUpdate(anyLong())).willReturn(Optional.of(fakeMenu));
given(menuRepository.updateStock(anyLong(), anyInt())).willReturn(0);

UpdateMenuStockCommand cmd = new UpdateMenuStockCommand(menuId, stock, vendorId);

// when
// then
assertThrows(InvalidMenuStockUpdateException.class, () -> updateMenuStockService.updateMenuStock(cmd));
verify(menuRepository, times(1)).findByIdForUpdate(anyLong());
verify(fakeMenu, times(2)).getStore();
verify(fakeStore, times(1)).isOwnedBy(any(UUID.class));
verify(fakeStore, times(1)).isOpen();

verify(menuRepository, times(1)).updateStock(anyLong(), anyInt());
}
}
Loading