1313import com .example .purebasketbe .global .exception .CustomException ;
1414import com .example .purebasketbe .global .exception .ErrorCode ;
1515import lombok .RequiredArgsConstructor ;
16+ import lombok .extern .slf4j .Slf4j ;
1617import org .springframework .stereotype .Service ;
1718import org .springframework .transaction .annotation .Transactional ;
1819
1920import java .util .List ;
2021
22+ @ Slf4j
2123@ Service
2224@ RequiredArgsConstructor
2325public class CartService {
@@ -30,18 +32,16 @@ public class CartService {
3032 @ Transactional
3133 public void addToCart (Long productId , CartRequestDto requestDto , Member member ) {
3234 Product product = findAndValidateProduct (productId );
35+ checkIfExist (product , member );
3336 Cart newCart = Cart .of (product , member , requestDto );
3437 cartRepository .save (newCart );
3538 }
3639
3740 @ Transactional (readOnly = true )
3841 public List <CartResponseDto > getCartList (Member member ) {
3942 return cartRepository .findAllByMember (member ).stream ()
40- .map (cart -> {
41- Product product = findAndValidateProduct (cart .getProduct ().getId ());
42- Image image = findImage (product .getId ());
43- return CartResponseDto .of (product , image , cart );
44- }).toList ();
43+ .filter (cart -> !cart .getProduct ().isDeleted ())
44+ .map (CartResponseDto ::from ).toList ();
4545 }
4646
4747 @ Transactional
@@ -58,24 +58,26 @@ public void deleteCart(Long productId, Member member) {
5858
5959 @ Transactional
6060 public void addRecipeRelatedProductsToCarts (Long recipeId , Member member ) {
61- Recipe recipe = recipeRepository .findById (recipeId ).orElseThrow (() ->
62- new CustomException (ErrorCode .RECIPE_NOT_FOUND )
63- );
61+ Recipe recipe = getRecipeById (recipeId );
6462
6563 List <Product > productList = recipe .getProductList ();
6664 cartRepository .deleteAllByMemberAndProductIn (member , productList );
67- List <Cart > cartList = productList .stream ().map (product -> Cart .of (product , member , null )).toList ();
65+ List <Cart > cartList = productList .stream ().map (product -> Cart .of (product , member )).toList ();
6866 cartRepository .saveAll (cartList );
6967 }
7068
69+
7170 private Product findAndValidateProduct (Long productId ) {
72- Product product = productRepository .findById (productId ).orElseThrow (
71+ return productRepository .findByIdAndDeleted (productId , false ).orElseThrow (
7372 () -> new CustomException (ErrorCode .PRODUCT_NOT_FOUND )
7473 );
75- if (product .getStock () <= 0 || product .isDeleted ()) {
76- throw new CustomException (ErrorCode .NOT_ENOUGH_PRODUCT );
74+
75+ }
76+
77+ private void checkIfExist (Product product , Member member ) {
78+ if (cartRepository .existsProductByMember (product , member )) {
79+ throw new CustomException (ErrorCode .PRODUCT_ALREADY_ADDED );
7780 }
78- return product ;
7981 }
8082
8183 private Cart findAndValidateCart (Long productId , Member member ) {
@@ -88,4 +90,10 @@ private Image findImage(Long productId) {
8890 return imageRepository .findAllByProductId (productId ).stream ().findFirst ()
8991 .orElseThrow (() -> new CustomException (ErrorCode .INVALID_IMAGE ));
9092 }
93+
94+ private Recipe getRecipeById (Long recipeId ) {
95+ return recipeRepository .findById (recipeId ).orElseThrow (() ->
96+ new CustomException (ErrorCode .RECIPE_NOT_FOUND )
97+ );
98+ }
9199}
0 commit comments