-
Notifications
You must be signed in to change notification settings - Fork 0
Cart crud기능 구현 #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cart crud기능 구현 #53
Changes from all commits
721a1cd
0958d01
1fcfcfa
c729bfe
4b25172
7034172
088bb6f
2ae2fb9
dbafc4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.campustable.be.domain.cart.controller; | ||
|
|
||
|
|
||
| import com.campustable.be.domain.cart.dto.CartRequest; | ||
| import com.campustable.be.domain.cart.dto.CartResponse; | ||
| import com.campustable.be.domain.cart.service.CartService; | ||
| import com.campustable.be.global.aop.LogMonitoringInvocation; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/cart") | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class CartController implements CartControllerDocs{ | ||
|
|
||
| private final CartService cartService; | ||
|
|
||
| @LogMonitoringInvocation | ||
| @Override | ||
| @PostMapping("/items") | ||
| public ResponseEntity<CartResponse> addOrUpdateItems(@RequestBody CartRequest request) { | ||
| CartResponse response = cartService.updateCartItem(request.getMenuId(), request.getQuantity()); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @LogMonitoringInvocation | ||
| @Override | ||
| @GetMapping("") | ||
| public ResponseEntity<CartResponse> getCart(){ | ||
|
|
||
| CartResponse response = cartService.getCart(); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
|
|
||
| @LogMonitoringInvocation | ||
| @Override | ||
| @DeleteMapping("/{cartId}") | ||
| public void deleteCart(@PathVariable Long cartId) { | ||
| cartService.deleteCart(cartId); | ||
| } | ||
|
|
||
| @LogMonitoringInvocation | ||
| @Override | ||
| @DeleteMapping("/items/{cartItemId}") | ||
| public void deleteCartItem(@PathVariable Long cartItemId) { | ||
| cartService.deleteCartItem(cartItemId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.campustable.be.domain.cart.controller; | ||
|
|
||
| import com.campustable.be.domain.cart.dto.CartRequest; | ||
| import com.campustable.be.domain.cart.dto.CartResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
|
|
||
| public interface CartControllerDocs { | ||
|
|
||
| @Operation( | ||
| summary = "장바구니 메뉴 추가 또는 수량 변경", | ||
| description = """ | ||
| ### 요청 파라미터 | ||
| - `menu_id` (Long, required): 장바구니에 담을 메뉴 ID | ||
| - `quantity` (int, required): 담을 수량 | ||
| - 1 이상: 해당 수량으로 장바구니에 추가 또는 수정 | ||
| - 0: 해당 메뉴를 장바구니에서 삭제 | ||
|
|
||
| ### 응답 데이터 | ||
| - `cartId` (Long): 장바구니 ID | ||
| - `items` (List<CartItemDto>): 장바구니에 담긴 메뉴 목록 | ||
| - `totalPrice` (int): 장바구니 전체 금액 | ||
|
|
||
| ### 특징 | ||
| - 수량을 0으로 전달하면 해당 메뉴는 삭제됩니다. | ||
| - **장바구니가 비워지면 장바구니 엔티티가 자동 삭제**됩니다. (이 경우 응답에 `cartId`가 포함되지 않을 수 있습니다.) | ||
|
|
||
| ### 예외 처리 | ||
| - `MENU_NOT_FOUND` (404): 메뉴 없음 | ||
| - `USER_NOT_FOUND` (404): 유저 없음 | ||
| """ | ||
| ) | ||
| ResponseEntity<CartResponse> addOrUpdateItems( | ||
| @RequestBody CartRequest request | ||
| ); | ||
|
|
||
| @Operation( | ||
| summary = "장바구니 조회", | ||
| description = """ | ||
| ### 특징 | ||
| - JWT 인증 정보로 현재 로그인한 사용자의 장바구니를 조회합니다. | ||
| - 장바구니가 없는 경우 빈 배열(`[]`)과 `totalPrice: 0`을 반환합니다. | ||
|
|
||
| ### 예외 처리 | ||
| - `USER_NOT_FOUND` (404): 유저 없음 | ||
| - `ACCESS_DENIED` (403): 인증 실패 | ||
| """ | ||
| ) | ||
| ResponseEntity<CartResponse> getCart(); | ||
|
|
||
| @Operation( | ||
| summary = "장바구니 전체 삭제", | ||
| description = """ | ||
| ### 요청 파라미터 | ||
| - `cartId` (Long): 삭제할 장바구니 ID | ||
|
|
||
| ### 상세 로직 | ||
| - 해당 장바구니 ID가 현재 로그인한 사용자의 것인지 검증합니다. | ||
| - 사용자와 장바구니의 **연관관계를 명시적으로 끊은 후 삭제**를 진행합니다. | ||
| - 장바구니 삭제 시 하위의 모든 아이템도 함께 삭제됩니다. | ||
|
|
||
| ### 예외 처리 | ||
| - `CART_NOT_FOUND` (404): 장바구니 없음 | ||
| - `ACCESS_DENIED` (403): **본인의 장바구니가 아닌 경우** | ||
| """ | ||
| ) | ||
| void deleteCart(@PathVariable Long cartId); | ||
|
|
||
| @Operation( | ||
| summary = "장바구니 특정 메뉴 삭제", | ||
| description = """ | ||
| ### 요청 파라미터 | ||
| - `cartItemId` (Long): 삭제할 장바구니 아이템 ID | ||
|
|
||
| ### 상세 로직 | ||
| 1. 해당 `cartItemId`가 현재 로그인한 사용자의 장바구니에 속해 있는지 검증합니다. | ||
| 2. 아이템을 삭제한 후, **장바구니에 남은 아이템이 없는지 확인**합니다. | ||
| 3. **남은 아이템이 하나도 없다면 장바구니 엔티티도 함께 삭제** 처리합니다. | ||
|
|
||
| ### 예외 처리 | ||
| - `CART_ITEM_NOT_FOUND` (404): 아이템 없음 | ||
| - `ACCESS_DENIED` (403): **본인의 장바구니 아이템이 아닌 경우** | ||
| """ | ||
| ) | ||
| void deleteCartItem(@PathVariable Long cartItemId); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.campustable.be.domain.cart.dto; | ||
|
|
||
|
|
||
| import com.campustable.be.domain.cart.entity.CartItem; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class CartItemDto { | ||
| private String menuName; | ||
| private int quantity; | ||
| private int price; | ||
| private String menuUrl; | ||
| private Long cartItemId; | ||
|
|
||
| public CartItemDto(CartItem entity) { | ||
| this.menuName = entity.getMenu().getMenuName(); | ||
| this.price = entity.getMenu().getPrice(); | ||
| this.quantity = entity.getQuantity(); | ||
| this.menuUrl = entity.getMenu().getMenuUrl(); | ||
| this.cartItemId = entity.getCartItemId(); | ||
| } | ||
|
Comment on lines
+17
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -name "CartItem.java" -type fRepository: CampusTable/campus-table-be Length of output: 138 🏁 Script executed: cat -n ./src/main/java/com/campustable/be/domain/cart/entity/CartItem.javaRepository: CampusTable/campus-table-be Length of output: 1326 🏁 Script executed: # Check for CartItem creation and service layer usage
rg -n "new CartItem\|CartItem\(" --type=java -A 3 | head -50Repository: CampusTable/campus-table-be Length of output: 53 🏁 Script executed: # Search for CartItemDto and service methods that use it
rg -n "CartItemDto\|toDto" --type=java -B 2 -A 3 | head -80Repository: CampusTable/campus-table-be Length of output: 53 🏁 Script executed: # Find CartItemDto class
find . -name "CartItemDto.java" -type fRepository: CampusTable/campus-table-be Length of output: 138 🏁 Script executed: cat -n ./src/main/java/com/campustable/be/domain/cart/dto/CartItemDto.javaRepository: CampusTable/campus-table-be Length of output: 836 🏁 Script executed: # Check if there's a ForeignKey constraint or database schema
find . -name "*.sql" -o -name "*schema*" -type f | head -20Repository: CampusTable/campus-table-be Length of output: 53 🏁 Script executed: # Search for all references to CartItemDto instantiation
rg -n "new CartItemDto\|CartItemDto(" --type=java -B 3 -A 2Repository: CampusTable/campus-table-be Length of output: 179 🏁 Script executed: # Search for CartItemDto instantiation
rg -n "CartItemDto" --type=java -B 3 -A 2Repository: CampusTable/campus-table-be Length of output: 4399 🏁 Script executed: cat -n ./src/main/java/com/campustable/be/domain/cart/service/CartService.java | head -80Repository: CampusTable/campus-table-be Length of output: 3456 🏁 Script executed: # Check if there are any FK constraints or database validation
rg -n "ForeignKey|nullable|unique|JoinColumn" --type=java | grep -i "cart\|menu"Repository: CampusTable/campus-table-be Length of output: 956 🏁 Script executed: rg -n "@JoinColumn\|@ManyToOne" --type=java -A 1 | grep -A 1 "CartItem"Repository: CampusTable/campus-table-be Length of output: 53 🏁 Script executed: rg -n "menu" --type=java ./src/main/java/com/campustable/be/domain/cart/entity/CartItem.javaRepository: CampusTable/campus-table-be Length of output: 216 🏁 Script executed: # Check if there are any other places where CartItem is created
rg -n "new CartItem" --type=java -B 3 -A 3Repository: CampusTable/campus-table-be Length of output: 814
CartItem의 menu 필드에 명시적인 @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Menu menu;현재 CartItemDto 생성은 트랜잭션 내에서 이루어지므로 LazyInitializationException은 발생하지 않습니다. 🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.campustable.be.domain.cart.dto; | ||
|
|
||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class CartRequest { | ||
|
|
||
| private Long menuId; | ||
| private int quantity; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.campustable.be.domain.cart.dto; | ||
|
|
||
| import java.util.List; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Builder | ||
| public class CartResponse { | ||
| private List<CartItemDto> items; | ||
| private int totalPrice; | ||
| private Long cartId; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.campustable.be.domain.cart.entity; | ||
|
|
||
| import com.campustable.be.domain.user.entity.User; | ||
| import jakarta.persistence.CascadeType; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.OneToMany; | ||
| import jakarta.persistence.OneToOne; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class Cart { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "cart_id") | ||
| private Long cartId; | ||
|
|
||
| @OneToOne | ||
| @JoinColumn(name = "user_id") | ||
| private User user; | ||
|
|
||
| @OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, orphanRemoval = true) | ||
| private List<CartItem> cartItems = new ArrayList<>(); | ||
|
|
||
| public Cart(User user) { | ||
| this.user = user; | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.campustable.be.domain.cart.entity; | ||
|
|
||
| import com.campustable.be.domain.menu.entity.Menu; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.validation.constraints.Min; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| public class CartItem { | ||
|
|
||
| public CartItem(Cart cart, Menu menu){ | ||
| this.cart = cart; | ||
| this.menu = menu; | ||
| } | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name= "cart_item_id") | ||
| private Long cartItemId; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| private Menu menu; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| private Cart cart; | ||
|
|
||
| @Min(value = 0, message = "수량은 0 이상이어야 합니다") | ||
| private int quantity; | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.campustable.be.domain.cart.repository; | ||
|
|
||
| import com.campustable.be.domain.cart.entity.Cart; | ||
| import com.campustable.be.domain.cart.entity.CartItem; | ||
| import com.campustable.be.domain.menu.entity.Menu; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface CartItemRepository extends JpaRepository<CartItem, Long> { | ||
|
|
||
| Optional<CartItem> findByCartAndMenu(Cart cart, Menu menu); | ||
|
|
||
|
|
||
| List<CartItem> findByCart(Cart cart); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.campustable.be.domain.cart.repository; | ||
|
|
||
| import com.campustable.be.domain.cart.entity.Cart; | ||
| import com.campustable.be.domain.user.entity.User; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface CartRepository extends JpaRepository<Cart, Long> { | ||
|
|
||
| Optional<Cart> findByUser(User user); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API 문서의 파라미터 이름을 실제 JSON 필드명과 일치시키세요.
문서에서
menu_id(snake_case)로 표기되어 있지만,CartRequestDTO의 실제 필드명은menuId(camelCase)입니다. Spring Boot의 기본 JSON 직렬화는 camelCase를 사용하므로, API 사용자는menuId로 요청해야 합니다.문서의 정확성을 위해 파라미터 이름을
menuId로 수정하세요.🔎 제안하는 수정
🤖 Prompt for AI Agents