Skip to content

Commit

Permalink
Merge pull request #27 from marinesnow34/cartGet
Browse files Browse the repository at this point in the history
Feat: 장바구니 조회 구현
  • Loading branch information
marinesnow34 authored Nov 19, 2023
2 parents fcb59e8 + 9952470 commit 6d0027a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
import com.readyvery.readyverydemo.src.order.dto.CartEditReq;
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.CartGetRes;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
Expand All @@ -40,6 +41,13 @@ public ResponseEntity<FoodyDetailRes> getFoody(
return new ResponseEntity<>(foodyDetailRes, HttpStatus.OK);
}

@GetMapping("/cart")
public ResponseEntity<CartGetRes> getCart(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestParam("inout") Long inout) {
CartGetRes cartGetRes = orderService.getCart(userDetails, inout);
return new ResponseEntity<>(cartGetRes, HttpStatus.OK);
}

@PostMapping("/cart")
public ResponseEntity<CartAddRes> addCart(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody CartAddReq cartAddReq) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
import com.readyvery.readyverydemo.src.order.dto.CartEditReq;
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.CartGetRes;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
Expand All @@ -20,4 +21,6 @@ public interface OrderService {
CartItemDeleteRes deleteCart(CustomUserDetails userDetails, CartItemDeleteReq cartItemDeleteReq);

CartResetRes resetCart(CustomUserDetails userDetails);

CartGetRes getCart(CustomUserDetails userDetails, Long inout);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
import com.readyvery.readyverydemo.src.order.dto.CartEditReq;
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.CartGetRes;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
Expand Down Expand Up @@ -118,6 +119,14 @@ public CartResetRes resetCart(CustomUserDetails userDetails) {
return orderMapper.cartToCartResetRes(cart);
}

@Override
public CartGetRes getCart(CustomUserDetails userDetails, Long inout) {
UserInfo user = getUserInfo(userDetails);
Cart cart = getCart(user);

return orderMapper.cartToCartGetRes(cart, inout);
}

private void resetCartItem(Cart cart) {
cart.setIsDeleted(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.readyvery.readyverydemo.src.order.dto;

import java.util.List;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CartDto {
private Long idx;
private String name;
private Long count;
private Long totalPrice;
private List<OptionDto> options;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.readyvery.readyverydemo.src.order.dto;

import java.util.List;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CartGetRes {
private String name;
private String imgUrl;
private Long totalPrice;
private List<CartDto> carts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Builder;
import lombok.Getter;

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

import com.readyvery.readyverydemo.domain.Cart;
import com.readyvery.readyverydemo.domain.CartItem;
import com.readyvery.readyverydemo.domain.CartOption;
import com.readyvery.readyverydemo.domain.Foodie;
import com.readyvery.readyverydemo.domain.FoodieOption;
import com.readyvery.readyverydemo.domain.FoodieOptionCategory;
Expand Down Expand Up @@ -35,8 +36,8 @@ private Long determinePrice(Foodie foodie, Long inout) {
} else if (Objects.equals(inout, TAKE_OUT)) {
return foodie.getTakeOut() != null ? foodie.getTakeOut().getPrice() : foodie.getPrice();
}
// inout이 EAT_IN도 TAKE_OUT도 아닌 경우, null을 반환
return null;
// inout이 EAT_IN도 TAKE_OUT도 아닌 경우, 기본 값 반환
return foodie.getPrice();
}

private OptionCategoryDto foodieOptionCategoryToOptionCategoryDto(FoodieOptionCategory category) {
Expand Down Expand Up @@ -83,4 +84,54 @@ public CartResetRes cartToCartResetRes(Cart cart) {
.idx(cart.getId())
.build();
}

public CartGetRes cartToCartGetRes(Cart cart, Long inout) {
return CartGetRes.builder()
.name(cart.getStore().getName())
.imgUrl(cart.getStore().getImgs().get(0).getImgUrl())
.carts(
cart.getCartItems()
.stream()
.map(cartItem -> cartItemToCartDto(cartItem, inout))
.toList())
.totalPrice(
cart.getCartItems()
.stream()
.mapToLong(cartItem -> cartItemTotalPrice(cartItem, inout))
.sum())
.build();
}

private CartDto cartItemToCartDto(CartItem cartItem, Long inout) {
return CartDto.builder()
.idx(cartItem.getId())
.name(cartItem.getFoodie().getName())
.count(cartItem.getCount())
.totalPrice(cartItemTotalPrice(cartItem, inout))
.options(
cartItem.getCartOptions()
.stream()
.map(this::cartOptionToOptionDto)
.toList())
.build();
}

private OptionDto cartOptionToOptionDto(CartOption cartOption) {
return OptionDto.builder()
.idx(cartOption.getId())
.name(cartOption.getFoodieOption().getName())
.price(cartOption.getFoodieOption().getPrice())
.build();
}

private Long cartItemTotalPrice(CartItem cartItem, Long inout) {
Long optionsPriceSum = cartItem.getCartOptions()
.stream()
.mapToLong(cartOption -> cartOption.getFoodieOption().getPrice())
.sum();

Long totalPrice = optionsPriceSum + determinePrice(cartItem.getFoodie(), inout);
return totalPrice * cartItem.getCount();
}

}

0 comments on commit 6d0027a

Please sign in to comment.