Skip to content

Commit

Permalink
Merge pull request #26 from marinesnow34/cartReset
Browse files Browse the repository at this point in the history
Feat: 장바구니 삭제 구현
  • Loading branch information
marinesnow34 authored Nov 19, 2023
2 parents 6612270 + 26693d8 commit fcb59e8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/domain/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Getter
Expand All @@ -28,6 +29,7 @@
@AllArgsConstructor
@Slf4j
@Builder
@Setter
public class Cart extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public enum ExceptionCode {
INVALID_OPTION_COUNT(400, "Invalid option count."),
INVALID_OPTION(400, "Invalid option."),
OPTION_NOT_FOUND(404, "Option does not exists."),
CART_ITEM_NOT_FOUND(404, "Cart item does not exists.");
CART_ITEM_NOT_FOUND(404, "Cart item does not exists."),
CART_NOT_FOUND(404, "Cart does not exists."),
ITEM_NOT_SAME_STORE(400, "Item is not same store.");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -59,4 +60,10 @@ public ResponseEntity<CartItemDeleteRes> deleteCartItem(@AuthenticationPrincipal
CartItemDeleteRes cartItemDeleteRes = orderService.deleteCart(userDetails, cartItemDeleteReq);
return new ResponseEntity<>(cartItemDeleteRes, HttpStatus.OK);
}

@DeleteMapping("/cart/reset")
public ResponseEntity<CartResetRes> resetCart(@AuthenticationPrincipal CustomUserDetails userDetails) {
CartResetRes cartResetRes = orderService.resetCart(userDetails);
return new ResponseEntity<>(cartResetRes, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;

public interface OrderService {
Expand All @@ -17,4 +18,6 @@ public interface OrderService {
CartEidtRes editCart(CustomUserDetails userDetails, CartEditReq cartEditReq);

CartItemDeleteRes deleteCart(CustomUserDetails userDetails, CartItemDeleteReq cartItemDeleteReq);

CartResetRes resetCart(CustomUserDetails userDetails);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.OrderMapper;

Expand Down Expand Up @@ -65,6 +66,7 @@ public CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq)
verifyCartAddReq(foodie, cartAddReq);

Cart cart = cartRepository.findByUserInfoAndIsDeletedFalse(user).orElseGet(() -> makeCart(user, store));
verifyItemsInCart(cart, store);
CartItem cartItem = makeCartItem(cart, foodie, cartAddReq.getCount());
List<CartOption> cartOptions = cartAddReq.getOptions().stream()
.map(option -> makeCartOption(cartItem, option))
Expand All @@ -77,6 +79,12 @@ public CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq)
return orderMapper.cartToCartAddRes(cartItem);
}

private void verifyItemsInCart(Cart cart, Store store) {
if (!cart.getStore().equals(store)) {
throw new BusinessLogicException(ExceptionCode.ITEM_NOT_SAME_STORE);
}
}

@Override
public CartEidtRes editCart(CustomUserDetails userDetails, CartEditReq cartEditReq) {
CartItem cartItem = getCartItem(cartEditReq.getIdx());
Expand All @@ -99,6 +107,27 @@ public CartItemDeleteRes deleteCart(CustomUserDetails userDetails, CartItemDelet
return orderMapper.cartToCartItemDeleteRes(cartItem);
}

@Override
public CartResetRes resetCart(CustomUserDetails userDetails) {
UserInfo user = getUserInfo(userDetails);
Cart cart = getCart(user);

resetCartItem(cart);

cartRepository.save(cart);
return orderMapper.cartToCartResetRes(cart);
}

private void resetCartItem(Cart cart) {
cart.setIsDeleted(true);
}

private Cart getCart(UserInfo user) {
return cartRepository.findByUserInfoAndIsDeletedFalse(user).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.CART_NOT_FOUND)
);
}

private void deleteCartItem(CartItem cartItem) {
cartItem.setIsDeleted(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CartResetRes {
private Long idx;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.springframework.stereotype.Component;

import com.readyvery.readyverydemo.domain.Cart;
import com.readyvery.readyverydemo.domain.CartItem;
import com.readyvery.readyverydemo.domain.Foodie;
import com.readyvery.readyverydemo.domain.FoodieOption;
Expand Down Expand Up @@ -76,4 +77,10 @@ public CartItemDeleteRes cartToCartItemDeleteRes(CartItem cartItem) {
.idx(cartItem.getId())
.build();
}

public CartResetRes cartToCartResetRes(Cart cart) {
return CartResetRes.builder()
.idx(cart.getId())
.build();
}
}

0 comments on commit fcb59e8

Please sign in to comment.