diff --git a/src/main/java/com/back/domain/cart/dto/request/CartRequestDto.java b/src/main/java/com/back/domain/cart/dto/request/CartRequestDto.java index 51ceff0f..83f93e58 100644 --- a/src/main/java/com/back/domain/cart/dto/request/CartRequestDto.java +++ b/src/main/java/com/back/domain/cart/dto/request/CartRequestDto.java @@ -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개 이상이어야 합니다") @@ -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가 필수입니다."); diff --git a/src/main/java/com/back/domain/cart/dto/response/CartResponseDto.java b/src/main/java/com/back/domain/cart/dto/response/CartResponseDto.java index 19ae9962..ea72778d 100644 --- a/src/main/java/com/back/domain/cart/dto/response/CartResponseDto.java +++ b/src/main/java/com/back/domain/cart/dto/response/CartResponseDto.java @@ -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, // 상품 가격 @@ -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(), diff --git a/src/main/java/com/back/domain/cart/entity/Cart.java b/src/main/java/com/back/domain/cart/entity/Cart.java index 0068a463..b6713981 100644 --- a/src/main/java/com/back/domain/cart/entity/Cart.java +++ b/src/main/java/com/back/domain/cart/entity/Cart.java @@ -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 이미지 @@ -232,6 +233,7 @@ public ProductInfo getProductInfo() { } return new ProductInfo( this.product.getId(), + this.product.getProductUuid(), this.product.getName(), this.product.getPrice(), imageUrl // Product 첫 번째 이미지 @@ -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; } diff --git a/src/main/java/com/back/domain/cart/service/CartService.java b/src/main/java/com/back/domain/cart/service/CartService.java index 425375bc..25258027 100644 --- a/src/main/java/com/back/domain/cart/service/CartService.java +++ b/src/main/java/com/back/domain/cart/service/CartService.java @@ -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", "존재하지 않는 상품입니다.")); // 중복 확인 diff --git a/src/main/java/com/back/domain/order/order/dto/response/OrderResponseDto.java b/src/main/java/com/back/domain/order/order/dto/response/OrderResponseDto.java index 4fafe3d3..28745aa9 100644 --- a/src/main/java/com/back/domain/order/order/dto/response/OrderResponseDto.java +++ b/src/main/java/com/back/domain/order/order/dto/response/OrderResponseDto.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; +import java.util.UUID; /** * 주문 상세 응답 DTO @@ -34,7 +35,7 @@ public record OrderResponseDto( */ public record OrderItemResponseDto( Long orderItemId, - Long productId, + UUID productUuid, String productName, String productThumbnailUrl, Integer quantity, diff --git a/src/main/java/com/back/domain/order/order/service/OrderService.java b/src/main/java/com/back/domain/order/order/service/OrderService.java index a90855eb..29701853 100644 --- a/src/main/java/com/back/domain/order/order/service/OrderService.java +++ b/src/main/java/com/back/domain/order/order/service/OrderService.java @@ -445,7 +445,7 @@ private OrderResponseDto convertToOrderResponseDto(Order order) { List 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(), diff --git a/src/test/java/com/back/domain/cart/controller/CartControllerTest.java b/src/test/java/com/back/domain/cart/controller/CartControllerTest.java index 68ac1d78..b28dfcf0 100644 --- a/src/test/java/com/back/domain/cart/controller/CartControllerTest.java +++ b/src/test/java/com/back/domain/cart/controller/CartControllerTest.java @@ -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; @@ -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() { @@ -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 @@ -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 @@ -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 @@ -279,7 +282,7 @@ void getSelectedCartItems_Success() throws Exception { List selectedItems = Arrays.asList( new CartResponseDto( 1L, // cartId - 1L, // productId + TEST_PRODUCT_UUID, // productUuid "테스트 상품1", // productName "test-image1.jpg", // productImageUrl 10000, // price diff --git a/src/test/java/com/back/domain/cart/service/CartServiceTest.java b/src/test/java/com/back/domain/cart/service/CartServiceTest.java index efe05c7e..fee30e65 100644 --- a/src/test/java/com/back/domain/cart/service/CartServiceTest.java +++ b/src/test/java/com/back/domain/cart/service/CartServiceTest.java @@ -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.*; @@ -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() { @@ -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); @@ -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 @@ -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); @@ -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)); } @@ -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); @@ -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)) @@ -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)) diff --git a/src/test/java/com/back/domain/order/order/controller/OrderControllerTest.java b/src/test/java/com/back/domain/order/order/controller/OrderControllerTest.java index 96dc5f95..4aecda04 100644 --- a/src/test/java/com/back/domain/order/order/controller/OrderControllerTest.java +++ b/src/test/java/com/back/domain/order/order/controller/OrderControllerTest.java @@ -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; @@ -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 { @@ -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 ) @@ -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