Skip to content
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

[REFACTOR] 장바구니 수량 일괄 수정 도입 및 명세서 설명 추가 #149

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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",
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class SaveCartReq {
public class CartItemReq {
@Schema(description = "스티커 Id")
@NotNull
private int stickerId;


@Schema(description = "스티커 수량")
@NotNull
@Min(1)
private int count;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CartCountReq> cartCountReqs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

Expand All @@ -36,6 +37,7 @@ public void saveCartItem(int userId, SaveCartReq req) {

//todo : 배송 지역별 배송비 책정
@Override
@Transactional(readOnly = true)
public CartItemsRes getUserCartItems(int userId) {
List<Cart> carts = cartService.findByUserId(userId);
StickerOrderInfo stickerOrderInfo = stickerProvider.getStickerOrderInfoFromCart(carts);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ public interface CartService {

Optional<Cart> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,7 +33,7 @@ public Optional<Cart> 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));
}
Expand All @@ -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);
Expand Down