From 2082f6c2a90ab38137f9eca2d3f800d251a68a42 Mon Sep 17 00:00:00 2001 From: Kim Seung-yeop Date: Tue, 13 Aug 2024 12:41:15 +0900 Subject: [PATCH] =?UTF-8?q?SCRUM-51=20feat:=20=EC=83=81=ED=92=88=20?= =?UTF-8?q?=EC=9E=A5=EB=B0=94=EA=B5=AC=EB=8B=88=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../golagola/domain/buyer/entity/Buyer.java | 24 +++++++++++++++++++ .../cart/controller/CartController.java | 16 +++++++++---- .../golagola/domain/cart/dto/CartRequest.java | 9 ++++++- .../golagola/domain/cart/entity/Cart.java | 7 ++++++ .../domain/cart/service/CartService.java | 18 ++++++++++++++ .../common/code/status/ErrorStatus.java | 3 +++ 6 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java b/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java index 980dd1e..a7fac3a 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java +++ b/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java @@ -3,6 +3,7 @@ import com.kakaoteck.golagola.domain.buyer.dto.BuyerRequest; import com.kakaoteck.golagola.domain.cart.entity.Cart; import com.kakaoteck.golagola.domain.order.entity.Order; +import com.kakaoteck.golagola.domain.product.entity.Product; import com.kakaoteck.golagola.domain.review.entity.Review; import com.kakaoteck.golagola.global.common.enums.Gender; import com.kakaoteck.golagola.global.common.enums.Role; @@ -112,6 +113,29 @@ public void updateProfile(BuyerRequest.MyPagePutDto request) { this.phoneNum = request.phoneNum(); } + public void assignCart(Cart cart) { + if (cart != null) { + this.cart = cart; + cart.assignBuyer(this); + } + } + + public void addProductToCart(Product product) { + if (this.cart == null) { + this.cart = new Cart(); // 새로운 Cart 객체 생성 + this.cart.assignBuyer(this); // Cart와 Buyer 간의 양방향 연관관계 설정 + } + this.cart.getProductList().add(product); + } + + public Cart getOrCreateCart() { + if (this.cart == null) { + this.cart = new Cart(); // 새로운 Cart 객체 생성 + this.cart.assignBuyer(this); // Cart와 Buyer 간의 양방향 연관관계 설정 + } + return this.cart; + } + public static Buyer from(Long buyerId, String nickname, String realName, Gender gender, String email, String password, String address, String phoneNum, Role role, LocalDate registerDate) { return Buyer.builder() diff --git a/src/main/java/com/kakaoteck/golagola/domain/cart/controller/CartController.java b/src/main/java/com/kakaoteck/golagola/domain/cart/controller/CartController.java index 97b0c66..32c5e9c 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/cart/controller/CartController.java +++ b/src/main/java/com/kakaoteck/golagola/domain/cart/controller/CartController.java @@ -1,16 +1,15 @@ package com.kakaoteck.golagola.domain.cart.controller; import com.kakaoteck.golagola.domain.buyer.entity.Buyer; +import com.kakaoteck.golagola.domain.cart.dto.CartRequest; import com.kakaoteck.golagola.domain.cart.dto.CartResponse; import com.kakaoteck.golagola.domain.cart.service.CartService; import com.kakaoteck.golagola.global.common.ApiResponse; +import io.lettuce.core.StrAlgoArgs; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor @@ -25,4 +24,13 @@ public class CartController { public ApiResponse getCartProducts(@AuthenticationPrincipal Buyer buyer) { return ApiResponse.onSuccess(cartService.getCartProducts(buyer)); } + + @Operation(summary = "상품 장바구니 추가", description = "장바구니에 상품을 추가합니다.") + @PostMapping() + public ApiResponse addCartProduct( + @AuthenticationPrincipal Buyer buyer, + @RequestBody CartRequest request + ) { + return ApiResponse.onSuccess(cartService.addCartProduct(buyer, request.productId())); + } } diff --git a/src/main/java/com/kakaoteck/golagola/domain/cart/dto/CartRequest.java b/src/main/java/com/kakaoteck/golagola/domain/cart/dto/CartRequest.java index f5023f6..a91de42 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/cart/dto/CartRequest.java +++ b/src/main/java/com/kakaoteck/golagola/domain/cart/dto/CartRequest.java @@ -1,4 +1,11 @@ package com.kakaoteck.golagola.domain.cart.dto; -public record CartRequest() { +import com.kakaoteck.golagola.domain.product.entity.Product; +import lombok.Builder; + +@Builder +public record CartRequest( + Long productId + +) { } diff --git a/src/main/java/com/kakaoteck/golagola/domain/cart/entity/Cart.java b/src/main/java/com/kakaoteck/golagola/domain/cart/entity/Cart.java index cc0898f..ad35481 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/cart/entity/Cart.java +++ b/src/main/java/com/kakaoteck/golagola/domain/cart/entity/Cart.java @@ -29,4 +29,11 @@ public class Cart { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long cartId; + public void assignBuyer(Buyer buyer) { + if (buyer != null) { + this.buyer = buyer; + buyer.assignCart(this); + } + } + } diff --git a/src/main/java/com/kakaoteck/golagola/domain/cart/service/CartService.java b/src/main/java/com/kakaoteck/golagola/domain/cart/service/CartService.java index b31775c..1fa828a 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/cart/service/CartService.java +++ b/src/main/java/com/kakaoteck/golagola/domain/cart/service/CartService.java @@ -5,6 +5,9 @@ import com.kakaoteck.golagola.domain.cart.dto.CartRequest; import com.kakaoteck.golagola.domain.cart.dto.CartResponse; import com.kakaoteck.golagola.domain.product.entity.Product; +import com.kakaoteck.golagola.domain.product.repository.ProductRepository; +import com.kakaoteck.golagola.global.common.code.status.ErrorStatus; +import com.kakaoteck.golagola.global.common.exception.GeneralException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -12,6 +15,7 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -19,6 +23,8 @@ @Slf4j public class CartService { + private final ProductRepository productRepository; + public CartResponse getCartProducts(Buyer buyer) { // Cart가 null이 아닌 경우에만 productList를 가져오고, 그렇지 않으면 빈 리스트를 반환 List productList = Collections.emptyList(); @@ -30,4 +36,16 @@ public CartResponse getCartProducts(Buyer buyer) { .productList(productList) .build(); } + + public CartResponse addCartProduct(Buyer buyer, Long productId) { + Product product = productRepository.findById(productId) + .orElseThrow(() -> new GeneralException(ErrorStatus._NOT_FOUND_PRODUCT)); + + // Buyer의 cart에 product를 추가 + buyer.addProductToCart(product); + + return CartResponse.builder() + .productList(buyer.getCart().getProductList()) + .build(); + } } diff --git a/src/main/java/com/kakaoteck/golagola/global/common/code/status/ErrorStatus.java b/src/main/java/com/kakaoteck/golagola/global/common/code/status/ErrorStatus.java index d1b6ad6..85396c3 100644 --- a/src/main/java/com/kakaoteck/golagola/global/common/code/status/ErrorStatus.java +++ b/src/main/java/com/kakaoteck/golagola/global/common/code/status/ErrorStatus.java @@ -20,6 +20,9 @@ public enum ErrorStatus implements BaseErrorCode { _LOGIN_USER_INVALID(HttpStatus.BAD_REQUEST, "USER401", "로그인 중 오류가 발생하였습니다."), _INVALID_USER(HttpStatus.BAD_REQUEST, "USER401" , "아이디 또는 비밀번호가 틀렸습니다."), + // Product 에러 + _NOT_FOUND_PRODUCT(HttpStatus.NOT_FOUND, "USER400", "제품이 존재하지 않습니다."), + // Security 에러 INVALID_TOKEN(HttpStatus.BAD_REQUEST, "SEC4001", "잘못된 형식의 토큰입니다."), UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "SEC4010", "인증이 필요합니다."),