Skip to content

Commit b7b34a0

Browse files
authored
Merge pull request #60 from Gachigage/feature/product
[Feat] 거래시 제품 수량 감소
2 parents 69b49f0 + 3cc921b commit b7b34a0

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/main/java/com/gachigage/global/error/ErrorCode.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public enum ErrorCode {
2121
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "C006", "서버 내부 오류가 발생했습니다."),
2222

2323
// 이미지 업로드 오류
24-
IMAGE_UPLOAD_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "I001", "이미지 업로드에 실패했습니다.");
24+
IMAGE_UPLOAD_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "I001", "이미지 업로드에 실패했습니다."),
25+
26+
// 제품 관룐 에러
27+
INSUFFICIENT_STOCK(HttpStatus.BAD_REQUEST, "P001", "재고가 부족합니다.");
2528

2629
private final HttpStatus httpStatus;
2730
private final String code;

src/main/java/com/gachigage/product/domain/Product.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,11 @@ private void addImage(ProductImage image) {
281281
this.images.add(image);
282282
image.setProduct(this);
283283
}
284+
285+
public void deduceStock(Long quantity) {
286+
if (this.stock < quantity) {
287+
throw new CustomException(ErrorCode.INSUFFICIENT_STOCK, "재고가 부족합니다.");
288+
}
289+
this.stock -= quantity;
290+
}
284291
}

src/main/java/com/gachigage/trade/service/TradeService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
67

78
import com.gachigage.chat.domain.ChatRoom;
89
import com.gachigage.chat.repository.ChatRoomRepository;
@@ -26,6 +27,7 @@ public class TradeService {
2627
private final ChatRoomRepository chatRoomRepository;
2728
private final ProductPriceRepository productPriceRepository;
2829

30+
@Transactional
2931
public Trade createTrade(Long chatRoomId, Long productPriceId) {
3032
ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId)
3133
.orElseThrow(() -> new CustomException(ErrorCode.INVALID_INPUT_VALUE, "존재하지 않는 채팅방입니다."));
@@ -35,6 +37,8 @@ public Trade createTrade(Long chatRoomId, Long productPriceId) {
3537
.findById(productPriceId)
3638
.orElseThrow(() -> new CustomException(ErrorCode.INVALID_INPUT_VALUE, "존재하지 않는 상품 가격 정보입니다."));
3739

40+
tradeProduct.deduceStock((long)productPrice.getQuantity());
41+
3842
Trade trade = Trade.builder()
3943
.seller(chatRoom.getSeller())
4044
.buyer(chatRoom.getBuyer())

0 commit comments

Comments
 (0)