diff --git a/server/src/main/java/org/tattour/server/domain/cart/controller/CartController.java b/server/src/main/java/org/tattour/server/domain/cart/controller/CartController.java index 7e4f401..1f80049 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/controller/CartController.java +++ b/server/src/main/java/org/tattour/server/domain/cart/controller/CartController.java @@ -20,7 +20,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.tattour.server.domain.cart.controller.dto.request.SaveCartReq; +import org.tattour.server.domain.cart.controller.dto.request.CartItemReq; +import org.tattour.server.domain.cart.controller.dto.request.UpdateCartCountReq; import org.tattour.server.domain.cart.controller.dto.response.CartItemsRes; import org.tattour.server.domain.cart.facade.CartFacade; import org.tattour.server.global.config.annotations.UserId; @@ -64,9 +65,9 @@ public class CartController { @PostMapping public ResponseEntity saveCartItem( @Parameter(hidden = true) @UserId Integer userId, - @RequestBody @Valid SaveCartReq saveCartReq + @RequestBody @Valid CartItemReq cartItemReq ) { - cartFacade.saveCartItem(userId, saveCartReq); + cartFacade.saveCartItem(userId, cartItemReq); return BaseResponse.success(SuccessType.CREATE_SUCCESS); } @@ -92,7 +93,7 @@ public ResponseEntity getUserCartItem( return BaseResponse.success(SuccessType.GET_SUCCESS, cartFacade.getUserCartItems(userId)); } - @Operation(summary = "장바구니 수량 1개 더하기") + @Operation(summary = "장바구니 수량 일괄 수정") @ApiResponses(value = { @ApiResponse( responseCode = "200", @@ -111,11 +112,11 @@ public ResponseEntity getUserCartItem( description = "알 수 없는 서버 에러가 발생했습니다.", content = @Content(schema = @Schema(implementation = FailResponse.class))) }) - @PatchMapping("/{cartId}") - public ResponseEntity addCartCount( + @PatchMapping + public ResponseEntity updateCartCount( @Parameter(hidden = true) @UserId Integer userId, - @PathVariable Integer cartId) { - cartFacade.increaseCartCount(userId, cartId); + @RequestBody @Valid UpdateCartCountReq req) { + cartFacade.updateCartsCount(userId, req); return BaseResponse.success(SuccessType.UPDATE_SUCCESS); } @@ -141,7 +142,7 @@ public ResponseEntity addCartCount( @DeleteMapping("/{cartId}") public ResponseEntity deleteCartCount( @Parameter(hidden = true) @UserId Integer userId, - @PathVariable Integer cartId) { + @Parameter(description = "장바구니 Id") @PathVariable Integer cartId) { cartFacade.deleteCartItem(userId, cartId); return BaseResponse.success(SuccessType.DELETE_SUCCESS); } diff --git a/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/CartCountReq.java b/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/CartCountReq.java new file mode 100644 index 0000000..1ece005 --- /dev/null +++ b/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/CartCountReq.java @@ -0,0 +1,17 @@ +package org.tattour.server.domain.cart.controller.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import javax.validation.constraints.NotNull; +import lombok.Getter; + +@Schema(description = "장바구니 수량 수정 Request") +@Getter +public class CartCountReq { + @Schema(description = "장바구니 Id") + @NotNull(message = "장바구니 id는 null일 수 없습니다.") + private int cartId; + + @Schema(description = "장바구니 수량") + @NotNull(message = "장바구니 수량 null일 수 없습니다.") + private int count; +} diff --git a/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/SaveCartReq.java b/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/CartItemReq.java similarity index 65% rename from server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/SaveCartReq.java rename to server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/CartItemReq.java index 4b6e130..8333245 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/SaveCartReq.java +++ b/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/CartItemReq.java @@ -1,5 +1,6 @@ package org.tattour.server.domain.cart.controller.dto.request; +import io.swagger.v3.oas.annotations.media.Schema; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import lombok.Getter; @@ -7,10 +8,12 @@ @Getter @NoArgsConstructor -public class SaveCartReq { +public class CartItemReq { + @Schema(description = "스티커 Id") @NotNull private int stickerId; - + + @Schema(description = "스티커 수량") @NotNull @Min(1) private int count; diff --git a/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/UpdateCartCountReq.java b/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/UpdateCartCountReq.java new file mode 100644 index 0000000..e11f0ad --- /dev/null +++ b/server/src/main/java/org/tattour/server/domain/cart/controller/dto/request/UpdateCartCountReq.java @@ -0,0 +1,12 @@ +package org.tattour.server.domain.cart.controller.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import java.util.List; +import lombok.Getter; + +@Schema(description = "장바구니 수량 일괄 수정 Request") +@Getter +public class UpdateCartCountReq { + @Schema(description = "장바구니 수량 수정 Request 목록") + private List cartCountReqs; +} diff --git a/server/src/main/java/org/tattour/server/domain/cart/domain/Cart.java b/server/src/main/java/org/tattour/server/domain/cart/domain/Cart.java index 489684c..a55cc83 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/domain/Cart.java +++ b/server/src/main/java/org/tattour/server/domain/cart/domain/Cart.java @@ -40,7 +40,7 @@ public void addCount(int count) { this.count += count; } - public void increaseCount() { - this.count += COUNT_INCREMENT; + public void updateCount(int count) { + this.count = count; } } diff --git a/server/src/main/java/org/tattour/server/domain/cart/facade/CartFacade.java b/server/src/main/java/org/tattour/server/domain/cart/facade/CartFacade.java index a553d06..f593766 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/facade/CartFacade.java +++ b/server/src/main/java/org/tattour/server/domain/cart/facade/CartFacade.java @@ -1,14 +1,15 @@ package org.tattour.server.domain.cart.facade; -import org.tattour.server.domain.cart.controller.dto.request.SaveCartReq; +import org.tattour.server.domain.cart.controller.dto.request.CartItemReq; +import org.tattour.server.domain.cart.controller.dto.request.UpdateCartCountReq; import org.tattour.server.domain.cart.controller.dto.response.CartItemsRes; public interface CartFacade { - void saveCartItem(int userId, SaveCartReq req); + void saveCartItem(int userId, CartItemReq req); CartItemsRes getUserCartItems(int userId); - void increaseCartCount(int userId, int cartId); + void updateCartsCount(int userId, UpdateCartCountReq req); void deleteCartItem(int userId, int cartId); } diff --git a/server/src/main/java/org/tattour/server/domain/cart/facade/impl/CartFacadeImpl.java b/server/src/main/java/org/tattour/server/domain/cart/facade/impl/CartFacadeImpl.java index 970d543..7478057 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/facade/impl/CartFacadeImpl.java +++ b/server/src/main/java/org/tattour/server/domain/cart/facade/impl/CartFacadeImpl.java @@ -6,7 +6,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.tattour.server.domain.cart.controller.dto.request.SaveCartReq; +import org.tattour.server.domain.cart.controller.dto.request.CartItemReq; +import org.tattour.server.domain.cart.controller.dto.request.UpdateCartCountReq; import org.tattour.server.domain.cart.controller.dto.response.CartItemsRes; import org.tattour.server.domain.cart.domain.Cart; import org.tattour.server.domain.cart.facade.CartFacade; @@ -27,7 +28,7 @@ public class CartFacadeImpl implements CartFacade { @Override @Transactional - public void saveCartItem(int userId, SaveCartReq req) { + public void saveCartItem(int userId, CartItemReq req) { User user = userService.readUserById(userId); Sticker sticker = stickerProvider.getById(req.getStickerId()); @@ -36,6 +37,7 @@ public void saveCartItem(int userId, SaveCartReq req) { //todo : 배송 지역별 배송비 책정 @Override + @Transactional(readOnly = true) public CartItemsRes getUserCartItems(int userId) { List carts = cartService.findByUserId(userId); StickerOrderInfo stickerOrderInfo = stickerProvider.getStickerOrderInfoFromCart(carts); @@ -46,14 +48,18 @@ public CartItemsRes getUserCartItems(int userId) { } @Override - public void increaseCartCount(int userId, int cartId) { - Cart cart = cartService.findByIdAndUserId(userId, cartId); - cartService.increaseCartCount(cart); + @Transactional + public void updateCartsCount(int userId, UpdateCartCountReq updateCartCountReq) { + updateCartCountReq.getCartCountReqs() + .forEach(req -> cartService + .findByIdAndUserId(req.getCartId(), userId) + .updateCount(req.getCount())); } @Override + @Transactional public void deleteCartItem(int userId, int cartId) { - Cart cart = cartService.findByIdAndUserId(userId, cartId); + Cart cart = cartService.findByIdAndUserId(cartId, userId); cartService.delete(cart); } } \ No newline at end of file diff --git a/server/src/main/java/org/tattour/server/domain/cart/service/CartService.java b/server/src/main/java/org/tattour/server/domain/cart/service/CartService.java index 2777b75..cd1dcd3 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/service/CartService.java +++ b/server/src/main/java/org/tattour/server/domain/cart/service/CartService.java @@ -13,14 +13,12 @@ public interface CartService { Optional findByUserAndSticker(User user, Sticker sticker); - Cart findByIdAndUserId(int userId, int cartId); + Cart findByIdAndUserId(int id, int userId); void mergeOrAddToCart(User user, Sticker sticker, int count); Cart createNewCart(User user, Sticker sticker, int count); - void increaseCartCount(Cart cart); - void delete(Cart cart); void deleteAllByUserId(User user); diff --git a/server/src/main/java/org/tattour/server/domain/cart/service/impl/CartServiceImpl.java b/server/src/main/java/org/tattour/server/domain/cart/service/impl/CartServiceImpl.java index 7f0f729..654fcde 100644 --- a/server/src/main/java/org/tattour/server/domain/cart/service/impl/CartServiceImpl.java +++ b/server/src/main/java/org/tattour/server/domain/cart/service/impl/CartServiceImpl.java @@ -4,7 +4,6 @@ import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import org.tattour.server.domain.cart.domain.Cart; import org.tattour.server.domain.cart.repository.CartRepositoryImpl; import org.tattour.server.domain.cart.service.CartService; @@ -34,7 +33,7 @@ public Optional findByUserAndSticker(User user, Sticker sticker) { } @Override - public Cart findByIdAndUserId(int userId, int id) { + public Cart findByIdAndUserId(int id, int userId) { return cartRepositoryImpl.findByIdAndUser_Id(id, userId) .orElseThrow(() -> new BusinessException(ErrorType.NOT_FOUND_CART_EXCEPTION)); } @@ -59,13 +58,6 @@ public Cart createNewCart(User user, Sticker sticker, int count) { .build(); } - @Override - @Transactional - public void increaseCartCount(Cart cart) { - cart.increaseCount(); - cartRepositoryImpl.save(cart); - } - @Override public void delete(Cart cart) { cartRepositoryImpl.delete(cart);