Skip to content

Commit e512b32

Browse files
authored
장바구니 전체 선택 api 구현 (#384)
1 parent 726b80b commit e512b32

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/main/java/com/back/domain/cart/controller/CartController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ public ResponseEntity<RsData<CartResponseDto>> toggleSelection(
101101
return ResponseEntity.ok(RsData.of("200", "선택 상태가 변경되었습니다.", responseDto));
102102
}
103103

104+
@PutMapping("/toggle-all-selection")
105+
@Operation(summary = "장바구니 전체 선택 토글", description = "모든 장바구니 아이템의 선택 상태를 일괄 변경합니다.")
106+
public ResponseEntity<RsData<List<CartResponseDto>>> toggleAllSelection(
107+
@AuthenticationPrincipal CustomUserDetails customUserDetails,
108+
@RequestParam boolean isSelected) {
109+
110+
User user = customUserDetails.getUser();
111+
List<CartResponseDto> responseDtos = cartService.toggleAllSelection(user, isSelected);
112+
String message = isSelected ? "모든 장바구니 아이템이 선택되었습니다." : "모든 장바구니 아이템이 해제되었습니다.";
113+
return ResponseEntity.ok(RsData.of("200", message, responseDtos));
114+
}
115+
104116
@GetMapping("/selected")
105117
@Operation(
106118
summary = "선택된 장바구니 아이템 조회",

src/main/java/com/back/domain/cart/service/CartService.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,32 @@ public CartResponseDto toggleSelection(User user, Long cartId) {
219219
return CartResponseDto.from(updatedCart);
220220
}
221221

222+
/**
223+
* 장바구니 전체 선택 토글
224+
*/
225+
@Transactional
226+
public List<CartResponseDto> toggleAllSelection(User user, boolean isSelected) {
227+
// 1. 사용자의 모든 장바구니 아이템 조회 (상품/펀딩 fetch join)
228+
List<Cart> userCarts = cartRepository.findByUserWithProduct(user);
229+
230+
// 2. 모든 아이템의 선택 상태를 일괄 변경
231+
for (Cart cart : userCarts) {
232+
if (isSelected) {
233+
cart.select();
234+
} else {
235+
cart.unselect();
236+
}
237+
}
238+
239+
// 3. 일괄 저장
240+
List<Cart> updatedCarts = cartRepository.saveAll(userCarts);
241+
242+
// 4. 응답 DTO 변환
243+
return updatedCarts.stream()
244+
.map(CartResponseDto::from)
245+
.collect(Collectors.toList());
246+
}
247+
222248
/**
223249
* 선택된 장바구니 아이템 조회
224250
* @param user 사용자

src/test/java/com/back/domain/review/controller/ReviewControllerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ void getReviewStats_Success() throws Exception {
256256

257257
// When & Then
258258
mockMvc.perform(get("/api/reviews/stats")
259-
.param("productUuid", TEST_PRODUCT_UUID.toString()))
259+
.param("productUuid", TEST_PRODUCT_UUID.toString())
260+
.with(user(customUserDetails)))
260261
.andExpect(status().isOk());
261262
}
262263

0 commit comments

Comments
 (0)