Skip to content

Commit

Permalink
Merge pull request #22 from marinesnow34/cart
Browse files Browse the repository at this point in the history
Feat: 음료 수량 변경 구현
  • Loading branch information
marinesnow34 authored Nov 17, 2023
2 parents 937fa5b + fe0c7d9 commit adb1bc1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Getter
Expand All @@ -28,6 +29,7 @@
@AllArgsConstructor
@Slf4j
@Builder
@Setter
public class CartItem extends BaseTimeEntity {

@Id
Expand All @@ -39,7 +41,7 @@ public class CartItem extends BaseTimeEntity {
@Column(nullable = false)
private Long count;

// 장바구니 메뉴 연관관계 매핑
// 장바구니 메뉴 연관관계 매핑"
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "foodie_idx")
private Foodie foodie;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum ExceptionCode {
FOODY_NOT_IN_STORE(400, "Foody does not exists in store."),
INVALID_OPTION_COUNT(400, "Invalid option count."),
INVALID_OPTION(400, "Invalid option."),
OPTION_NOT_FOUND(404, "Option does not exists.");
OPTION_NOT_FOUND(404, "Option does not exists."),
CART_ITEM_NOT_FOUND(404, "Cart item does not exists.");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -14,6 +15,8 @@
import com.readyvery.readyverydemo.security.jwt.dto.CustomUserDetails;
import com.readyvery.readyverydemo.src.order.dto.CartAddReq;
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
import com.readyvery.readyverydemo.src.order.dto.CartEditReq;
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;

import lombok.RequiredArgsConstructor;
Expand All @@ -39,4 +42,11 @@ public ResponseEntity<CartAddRes> addCart(@AuthenticationPrincipal CustomUserDet
CartAddRes cartAddRes = orderService.addCart(userDetails, cartAddReq);
return new ResponseEntity<>(cartAddRes, HttpStatus.OK);
}

@PutMapping("/cart")
public ResponseEntity<CartEidtRes> updateCart(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody CartEditReq cartEditReq) {
CartEidtRes cartEditRes = orderService.editCart(userDetails, cartEditReq);
return new ResponseEntity<>(cartEditRes, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.readyvery.readyverydemo.security.jwt.dto.CustomUserDetails;
import com.readyvery.readyverydemo.src.order.dto.CartAddReq;
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
import com.readyvery.readyverydemo.src.order.dto.CartEditReq;
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;

public interface OrderService {
FoodyDetailRes getFoody(Long storeId, Long foodyId, Long inout);

CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq);

CartEidtRes editCart(CustomUserDetails userDetails, CartEditReq cartEditReq);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.readyvery.readyverydemo.security.jwt.dto.CustomUserDetails;
import com.readyvery.readyverydemo.src.order.dto.CartAddReq;
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
import com.readyvery.readyverydemo.src.order.dto.CartEditReq;
import com.readyvery.readyverydemo.src.order.dto.CartEidtRes;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.OrderMapper;

Expand Down Expand Up @@ -73,6 +75,34 @@ public CartAddRes addCart(CustomUserDetails userDetails, CartAddReq cartAddReq)
return orderMapper.cartToCartAddRes(cartItem);
}

@Override
public CartEidtRes editCart(CustomUserDetails userDetails, CartEditReq cartEditReq) {
CartItem cartItem = getCartItem(cartEditReq.getIdx());

verifyCartItem(cartItem, userDetails);

editCartItem(cartItem, cartEditReq);
cartItemRepository.save(cartItem);
return orderMapper.cartToCartEditRes(cartItem);
}

private void editCartItem(CartItem cartItem, CartEditReq cartEditReq) {
cartItem.setCount(cartEditReq.getCount());
}

private void verifyCartItem(CartItem cartItem, CustomUserDetails userDetails) {
boolean isCartItemOfUser = cartItem.getCart().getUserInfo().getId().equals(userDetails.getId());
if (!isCartItemOfUser) {
throw new BusinessLogicException(ExceptionCode.CART_ITEM_NOT_FOUND);
}
}

private CartItem getCartItem(Long idx) {
return cartItemRepository.findById(idx).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.CART_ITEM_NOT_FOUND)
);
}

private CartOption makeCartOption(CartItem cartItem, Long option) {
FoodieOption foodieOption = getFoodieOption(option);
return CartOption.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Getter;

@Getter
public class CartEditReq {
private Long idx; //cartItem idx
private Long count;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CartEidtRes {
private Long idx;
private Long count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ public CartAddRes cartToCartAddRes(CartItem cartItem) {
.cartItemId(cartItem.getId())
.build();
}

public CartEidtRes cartToCartEditRes(CartItem cartItem) {
return CartEidtRes.builder()
.idx(cartItem.getId())
.count(cartItem.getCount())
.build();
}
}

0 comments on commit adb1bc1

Please sign in to comment.