Skip to content

Commit

Permalink
[feat] 프론트를 위한 API 추가
Browse files Browse the repository at this point in the history
Co-authored-by: june-777 <[email protected]>
  • Loading branch information
kimhyun5u and june-777 committed Aug 26, 2024
1 parent 8f139d7 commit bbe55f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/camp/woowak/lab/cart/service/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public long getTotalPrice(CartTotalPriceCommand command) {
return totalPrice;
}

private Cart getCart(String customerId) {
public Cart getCart(String customerId) {
return cartRepository.findByCustomerId(customerId)
.orElse(cartRepository.save(new Cart(customerId)));
.orElseGet(() -> cartRepository.save(new Cart(customerId)));
}
}
12 changes: 12 additions & 0 deletions src/main/java/camp/woowak/lab/web/api/cart/CartApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import camp.woowak.lab.cart.domain.Cart;
import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.cart.service.CartService;
import camp.woowak.lab.cart.service.command.AddCartCommand;
import camp.woowak.lab.cart.service.command.CartTotalPriceCommand;
import camp.woowak.lab.web.authentication.LoginCustomer;
import camp.woowak.lab.web.authentication.annotation.AuthenticationPrincipal;
import camp.woowak.lab.web.dto.request.cart.AddCartRequest;
import camp.woowak.lab.web.dto.response.cart.AddCartResponse;
import camp.woowak.lab.web.dto.response.cart.CartResponse;
import camp.woowak.lab.web.dto.response.cart.CartTotalPriceResponse;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -26,6 +29,14 @@ public CartApiController(CartService cartService) {
this.cartService = cartService;
}

@GetMapping
public CartResponse getCart(@AuthenticationPrincipal LoginCustomer customer) {
Cart cart = cartService.getCart(customer.getId().toString());
long totalPrice = cartService.getTotalPrice(new CartTotalPriceCommand(customer.getId().toString()));
return new CartResponse(cart.getCustomerId(), cart.getCartItems().stream().mapToLong(CartItem::getAmount).sum(),
totalPrice);
}

@PostMapping
public AddCartResponse addCart(
@AuthenticationPrincipal LoginCustomer loginCustomer,
Expand All @@ -47,4 +58,5 @@ public CartTotalPriceResponse getCartTotalPrice(
log.info("Customer({})'s total price in cart is {}", loginCustomer.getId(), totalPrice);
return new CartTotalPriceResponse(totalPrice);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package camp.woowak.lab.web.dto.response.cart;

public record CartResponse(String customerId, Long totalAmount, Long totalPrice) {
}

0 comments on commit bbe55f1

Please sign in to comment.