Skip to content

Commit

Permalink
feat: 장바구니 조회 기능 구현 (#43)
Browse files Browse the repository at this point in the history
* feat: 장바구니 추가 기능 구현

* feat: 장바구니 조회 기능 구현
  • Loading branch information
LEEJaeHyeok97 authored Nov 19, 2024
1 parent bede955 commit c6f9bd4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.jangburich.domain.menu.domain.repository.MenuRepository;
import com.jangburich.domain.order.domain.Cart;
import com.jangburich.domain.order.domain.repository.CartRepository;
import com.jangburich.domain.order.domain.repository.OrdersRepository;
import com.jangburich.domain.order.dto.request.AddCartRequest;
import com.jangburich.domain.order.dto.response.CartResponse;
import com.jangburich.domain.order.dto.response.CartResponse.GetCartItemsResponse;
import com.jangburich.domain.store.domain.Store;
import com.jangburich.domain.store.domain.repository.StoreRepository;
import com.jangburich.domain.user.domain.User;
import com.jangburich.domain.user.domain.repository.UserRepository;
import com.jangburich.global.payload.Message;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -65,4 +67,29 @@ public Message addCart(String userProviderId, AddCartRequest addCartRequest) {
.message("장바구니에 상품을 추가했습니다.")
.build();
}

public CartResponse getCartItems(String userProviderId) {
User user = userRepository.findByProviderId(userProviderId)
.orElseThrow(() -> new NullPointerException());

List<Cart> carts = cartRepository.findAllByUser(user);

if (carts.isEmpty()) {
return CartResponse.of(List.of(), 0);
}

List<GetCartItemsResponse> cartItems = carts.stream()
.map(cart -> GetCartItemsResponse.of(
cart.getMenu().getName(),
cart.getMenu().getDescription(),
cart.getQuantity(),
cart.getMenu().getPrice()
))
.toList();

int discountAmount = 0;
CartResponse cartResponse = CartResponse.of(cartItems, discountAmount);

return cartResponse;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.jangburich.domain.order.domain.repository;

import com.jangburich.domain.order.domain.Cart;
import com.jangburich.domain.user.domain.User;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -11,4 +13,6 @@
public interface CartRepository extends JpaRepository<Cart, Long> {
@Query("SELECT c FROM Cart c WHERE c.user.userId = :userId AND c.menu.id = :menuId")
Optional<Cart> findByUserIdAndMenuId(@Param("userId") Long userId, @Param("menuId") Long menuId);

List<Cart> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jangburich.domain.order.dto.response;

import java.util.List;

public record CartResponse(
List<GetCartItemsResponse> cartItems,
Integer totalAmount,
Integer discountAmount,
Integer finalAmount
) {
public static CartResponse of(List<GetCartItemsResponse> cartItems, Integer discountAmount) {
int totalAmount = cartItems.stream()
.mapToInt(item -> item.menuPrice * item.quantity)
.sum();

discountAmount = 0;

int finalAmount = totalAmount - discountAmount;

return new CartResponse(cartItems, totalAmount, discountAmount, finalAmount);
}

public record GetCartItemsResponse(
String menuName,
String menuDescription,
Integer quantity,
Integer menuPrice
) {
public static GetCartItemsResponse of(String menuName, String menuDescription, Integer quantity, Integer menuPrice) {
return new GetCartItemsResponse(menuName, menuDescription, quantity, menuPrice);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.jangburich.domain.order.application.OrderService;
import com.jangburich.domain.order.dto.request.AddCartRequest;
import com.jangburich.domain.order.dto.response.CartResponse;
import com.jangburich.global.payload.Message;
import com.jangburich.global.payload.ResponseCustom;
import com.jangburich.utils.parser.AuthenticationParser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -23,11 +25,19 @@ public class OrderController {
private final OrderService orderService;

@Operation(summary = "장바구니 담기", description = "장바구니에 물건과 수량을 담습니다.")
@PostMapping("/cart")
@PostMapping("/carts")
public ResponseCustom<Message> addCart(
Authentication authentication,
@RequestBody AddCartRequest addCartRequest
) {
return ResponseCustom.OK(orderService.addCart(AuthenticationParser.parseUserId(authentication), addCartRequest));
}

@Operation(summary = "장바구니 조회", description = "장바구니에 담은 상품을 조회합니다.")
@GetMapping("/carts")
public ResponseCustom<CartResponse> getCartItems(
Authentication authentication
) {
return ResponseCustom.OK(orderService.getCartItems(AuthenticationParser.parseUserId(authentication)));
}
}

0 comments on commit c6f9bd4

Please sign in to comment.