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
Changes from 1 commit
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,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.StoreNotOpenException;
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 StoreNotOpenException 가게가 열려있지 않은 경우 발생한다.
* @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 StoreNotOpenException("가게가 열려있지 않습니다.");
}

// 메뉴의 재고를 변경한다.
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("메뉴를 찾을 수 없습니다."));
}
}