Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -4,15 +4,17 @@
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

import java.util.UUID;

/**
* 장바구니 추가/수정 요청 DTO
* - 일반 장바구니: productId, optionInfo 사용
* - 일반 장바구니: productUuid, optionInfo 사용
* - 펀딩 장바구니: fundingId, fundingPrice, fundingStock 사용
*/
@Schema(description = "장바구니 추가 요청")
public record CartRequestDto(
@Schema(description = "상품 ID (일반 장바구니만 필수)", example = "123", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
Long productId, // 상품 ID (일반 장바구니만 사용)
@Schema(description = "상품 UUID (일반 장바구니만 필수)", example = "550e8400-e29b-41d4-a716-446655440000", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
UUID productUuid, // 상품 UUID (일반 장바구니만 사용)

@NotNull(message = "수량은 필수입니다")
@Min(value = 1, message = "수량은 1개 이상이어야 합니다")
Expand Down Expand Up @@ -40,8 +42,8 @@ public record CartRequestDto(
* 유효성 검증
*/
public void validate() {
if ("NORMAL".equals(cartType) && productId == null) {
throw new IllegalArgumentException("일반 장바구니는 productId가 필수입니다.");
if ("NORMAL".equals(cartType) && productUuid == null) {
throw new IllegalArgumentException("일반 장바구니는 productUuid가 필수입니다.");
}
if ("FUNDING".equals(cartType) && fundingId == null) {
throw new IllegalArgumentException("펀딩 장바구니는 fundingId가 필수입니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.back.domain.cart.entity.Cart;

import java.time.LocalDateTime;
import java.util.UUID;

/**
* 장바구니 개별 상품 응답 DTO
*/
public record CartResponseDto(
Long cartId, // 장바구니 아이템 ID
Long productId, // 상품 ID
UUID productUuid, // 상품 UUID
String productName, // 상품명
String productImageUrl, // 상품 이미지 URL
Integer price, // 상품 가격
Expand All @@ -33,7 +34,7 @@ public static CartResponseDto from(Cart cart) {

return new CartResponseDto(
cart.getId(),
productInfo.getId(),
productInfo.getUuid(),
productInfo.getName(),
productInfo.getImageUrl(), // ProductInfo에서 이미지 URL 가져오기
productInfo.getPrice(),
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/back/domain/cart/entity/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public ProductInfo getProductInfo() {
// 펀딩 장바구니: Funding 정보 사용
return new ProductInfo(
this.funding.getId(),
null, // 펀딩은 UUID 없음
this.funding.getTitle(),
this.fundingPrice != null ? this.fundingPrice : (int) this.funding.getPrice(),
this.funding.getImageUrl() // Funding 이미지
Expand All @@ -232,6 +233,7 @@ public ProductInfo getProductInfo() {
}
return new ProductInfo(
this.product.getId(),
this.product.getProductUuid(),
this.product.getName(),
this.product.getPrice(),
imageUrl // Product 첫 번째 이미지
Expand All @@ -246,18 +248,21 @@ public ProductInfo getProductInfo() {
*/
public static class ProductInfo {
private final Long id;
private final java.util.UUID uuid;
private final String name;
private final Integer price;
private final String imageUrl;

public ProductInfo(Long id, String name, Integer price, String imageUrl) {
public ProductInfo(Long id, java.util.UUID uuid, String name, Integer price, String imageUrl) {
this.id = id;
this.uuid = uuid;
this.name = name;
this.price = price;
this.imageUrl = imageUrl;
}

public Long getId() { return id; }
public java.util.UUID getUuid() { return uuid; }
public String getName() { return name; }
public Integer getPrice() { return price; }
public String getImageUrl() { return imageUrl; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CartResponseDto addToCart(User user, CartRequestDto requestDto) {
*/
private CartResponseDto addNormalCart(User user, CartRequestDto requestDto, Cart.CartType cartType) {
// 상품 존재 확인
Product product = productRepository.findById(requestDto.productId())
Product product = productRepository.findByProductUuid(requestDto.productUuid())
.orElseThrow(() -> new ServiceException("PRODUCT_NOT_FOUND", "존재하지 않는 상품입니다."));

// 중복 확인
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
* 주문 상세 응답 DTO
Expand Down Expand Up @@ -34,7 +35,7 @@ public record OrderResponseDto(
*/
public record OrderItemResponseDto(
Long orderItemId,
Long productId,
UUID productUuid,
String productName,
String productThumbnailUrl,
Integer quantity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ private OrderResponseDto convertToOrderResponseDto(Order order) {
List<OrderResponseDto.OrderItemResponseDto> orderItemDtos = order.getOrderItems().stream()
.map(item -> new OrderResponseDto.OrderItemResponseDto(
item.getId(),
item.getProduct().getId(),
item.getProduct().getProductUuid(),
item.getProduct().getName(),
getProductThumbnailUrl(item.getProduct()),
item.getQuantity(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -57,6 +58,8 @@ class CartControllerTest {
private CartRequestDto cartRequestDto;
private CartResponseDto cartResponseDto;
private CartListResponseDto cartListResponseDto;

private static final UUID TEST_PRODUCT_UUID = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");

@BeforeEach
void setUp() {
Expand All @@ -70,18 +73,18 @@ void setUp() {
org.springframework.test.util.ReflectionTestUtils.setField(testUser, "id", 1L);

cartRequestDto = new CartRequestDto(
1L, // productId
2, // quantity
"옵션정보", // optionInfo
"NORMAL", // cartType
null, // fundingId
null, // fundingPrice
null // fundingStock
TEST_PRODUCT_UUID, // productUuid
2, // quantity
"옵션정보", // optionInfo
"NORMAL", // cartType
null, // fundingId
null, // fundingPrice
null // fundingStock
);

cartResponseDto = new CartResponseDto(
1L, // cartId
1L, // productId
TEST_PRODUCT_UUID, // productUuid
"임시 상품명", // productName
"test-image.jpg", // productImageUrl
10000, // price
Expand Down Expand Up @@ -169,9 +172,9 @@ void updateQuantity_Success() throws Exception {
Long cartId = 1L;
Integer newQuantity = 3;
CartResponseDto updatedCart = new CartResponseDto(
cartId, // cartId
1L, // productId
"임시 상품명", // productName
cartId, // cartId
TEST_PRODUCT_UUID, // productUuid
"임시 상품명", // productName
"test-image.jpg", // productImageUrl
10000, // price
newQuantity, // quantity
Expand Down Expand Up @@ -246,9 +249,9 @@ void toggleSelection_Success() throws Exception {
// given
Long cartId = 1L;
CartResponseDto toggledCart = new CartResponseDto(
cartId, // cartId
1L, // productId
"임시 상품명", // productName
cartId, // cartId
TEST_PRODUCT_UUID, // productUuid
"임시 상품명", // productName
"test-image.jpg", // productImageUrl
10000, // price
2, // quantity
Expand Down Expand Up @@ -279,7 +282,7 @@ void getSelectedCartItems_Success() throws Exception {
List<CartResponseDto> selectedItems = Arrays.asList(
new CartResponseDto(
1L, // cartId
1L, // productId
TEST_PRODUCT_UUID, // productUuid
"테스트 상품1", // productName
"test-image1.jpg", // productImageUrl
10000, // price
Expand Down
44 changes: 24 additions & 20 deletions src/test/java/com/back/domain/cart/service/CartServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
Expand Down Expand Up @@ -57,6 +58,8 @@ class CartServiceTest {
private Cart testFundingCart;
private CartRequestDto normalCartRequestDto;
private CartRequestDto fundingCartRequestDto;

private static final UUID TEST_PRODUCT_UUID = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");

@BeforeEach
void setUp() {
Expand All @@ -72,6 +75,7 @@ void setUp() {
// 테스트용 상품 생성 (Mock 사용)
testProduct = mock(Product.class);
when(testProduct.getId()).thenReturn(1L);
when(testProduct.getProductUuid()).thenReturn(TEST_PRODUCT_UUID);
when(testProduct.getName()).thenReturn("테스트 상품");
when(testProduct.getPrice()).thenReturn(10000);
when(testProduct.getDiscountPrice()).thenReturn(10000);
Expand Down Expand Up @@ -112,18 +116,18 @@ void setUp() {

// 테스트용 요청 DTO 생성 (일반)
normalCartRequestDto = new CartRequestDto(
1L, // productId
2, // quantity
"일반 상품 옵션", // optionInfo
"NORMAL", // cartType
null, // fundingId
null, // fundingPrice
null // fundingStock
TEST_PRODUCT_UUID, // productUuid
2, // quantity
"일반 상품 옵션", // optionInfo
"NORMAL", // cartType
null, // fundingId
null, // fundingPrice
null // fundingStock
);

// 테스트용 요청 DTO 생성 (펀딩)
fundingCartRequestDto = new CartRequestDto(
null, // productId (펀딩은 필요 없음)
null, // productUuid (펀딩은 필요 없음)
1, // quantity
null, // optionInfo (펀딩은 옵션 안씀)
"FUNDING", // cartType
Expand All @@ -141,7 +145,7 @@ void setUp() {
@DisplayName("장바구니에 새 상품 추가 성공")
void addToCart_NewProduct_Success() {
// Given
given(productRepository.findById(1L)).willReturn(Optional.of(testProduct));
given(productRepository.findByProductUuid(TEST_PRODUCT_UUID)).willReturn(Optional.of(testProduct));
given(cartRepository.findByUserAndProductAndCartType(eq(testUser), any(Product.class), eq(Cart.CartType.NORMAL)))
.willReturn(Optional.empty());
given(cartRepository.save(any(Cart.class))).willReturn(testNormalCart);
Expand All @@ -155,7 +159,7 @@ void addToCart_NewProduct_Success() {
assertThat(result.cartType()).isEqualTo("NORMAL");
assertThat(result.optionInfo()).isEqualTo("일반 상품 옵션");

verify(productRepository).findById(1L);
verify(productRepository).findByProductUuid(TEST_PRODUCT_UUID);
verify(cartRepository).findByUserAndProductAndCartType(eq(testUser), any(Product.class), eq(Cart.CartType.NORMAL));
verify(cartRepository).save(any(Cart.class));
}
Expand All @@ -173,7 +177,7 @@ void addToCart_ExistingProduct_QuantityIncrease() {
.optionInfo("기존 옵션")
.build();

given(productRepository.findById(1L)).willReturn(Optional.of(testProduct));
given(productRepository.findByProductUuid(TEST_PRODUCT_UUID)).willReturn(Optional.of(testProduct));
given(cartRepository.findByUserAndProductAndCartType(eq(testUser), any(Product.class), eq(Cart.CartType.NORMAL)))
.willReturn(Optional.of(existingCart));
given(cartRepository.save(existingCart)).willReturn(existingCart);
Expand All @@ -191,7 +195,7 @@ void addToCart_ExistingProduct_QuantityIncrease() {
@DisplayName("존재하지 않는 상품 추가 실패")
void addToCart_ProductNotFound_ThrowException() {
// Given
given(productRepository.findById(1L)).willReturn(Optional.empty());
given(productRepository.findByProductUuid(TEST_PRODUCT_UUID)).willReturn(Optional.empty());

// When & Then
assertThatThrownBy(() -> cartService.addToCart(testUser, normalCartRequestDto))
Expand All @@ -206,16 +210,16 @@ void addToCart_ProductNotFound_ThrowException() {
void addToCart_InvalidCartType_ThrowException() {
// Given
CartRequestDto invalidDto = new CartRequestDto(
1L, // productId
2, // quantity
null, // optionInfo
"INVALID", // cartType
null, // fundingId
null, // fundingPrice
null // fundingStock
TEST_PRODUCT_UUID, // productUuid
2, // quantity
null, // optionInfo
"INVALID", // cartType
null, // fundingId
null, // fundingPrice
null // fundingStock
);

given(productRepository.findById(1L)).willReturn(Optional.of(testProduct));
given(productRepository.findByProductUuid(TEST_PRODUCT_UUID)).willReturn(Optional.of(testProduct));

// When & Then
assertThatThrownBy(() -> cartService.addToCart(testUser, invalidDto))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -38,6 +39,8 @@ class OrderControllerTest {
private OrderRequestDto orderRequestDto;
private OrderCancelRequestDto orderCancelRequestDto;
private OrderResponseDto orderResponseDto;

private static final UUID TEST_PRODUCT_UUID = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");

@BeforeEach
void setUp() throws Exception {
Expand All @@ -56,7 +59,7 @@ void setUp() throws Exception {
orderRequestDto = new OrderRequestDto(
List.of( // orderItems
new OrderRequestDto.OrderItemRequestDto(
java.util.UUID.randomUUID(), // productUuid
TEST_PRODUCT_UUID, // productUuid
2, // quantity
"빨강, L" // optionInfo
)
Expand Down Expand Up @@ -96,7 +99,7 @@ void setUp() throws Exception {
List.of( // orderItems
new OrderResponseDto.OrderItemResponseDto(
1L, // orderItemId
1L, // productId
TEST_PRODUCT_UUID, // productUuid
"테스트 상품", // productName
"http://example.com/image.jpg", // productThumbnailUrl
2, // quantity
Expand Down