Skip to content

Commit

Permalink
SCRUM-51 feat: 상품 장바구니 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yeopyeop-82 committed Aug 13, 2024
1 parent 08038df commit 2082f6c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,4 +24,13 @@ public class CartController {
public ApiResponse<CartResponse> getCartProducts(@AuthenticationPrincipal Buyer buyer) {
return ApiResponse.onSuccess(cartService.getCartProducts(buyer));
}

@Operation(summary = "상품 장바구니 추가", description = "장바구니에 상품을 추가합니다.")
@PostMapping()
public ApiResponse<CartResponse> addCartProduct(
@AuthenticationPrincipal Buyer buyer,
@RequestBody CartRequest request
) {
return ApiResponse.onSuccess(cartService.addCartProduct(buyer, request.productId()));
}
}
Original file line number Diff line number Diff line change
@@ -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

) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@
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;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class CartService {

private final ProductRepository productRepository;

public CartResponse getCartProducts(Buyer buyer) {
// Cart가 null이 아닌 경우에만 productList를 가져오고, 그렇지 않으면 빈 리스트를 반환
List<Product> productList = Collections.emptyList();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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", "인증이 필요합니다."),
Expand Down

0 comments on commit 2082f6c

Please sign in to comment.