더북(TheBook)

3. CartServiceImpl 클래스에 장바구니의 예외 처리를 위한 validateCart() 메서드를 추가합니다.

코드 15-12 CartServiceImpl.java

package com.springmvc.service;
...
import com.springmvc.exception.CartException;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartRepository cartRepository;
     ...

    public Cart validateCart(String cartId) {
        Cart cart = cartRepository.read(cartId);
        if (cart == null || cart.getCartItems().size() == 0) {
            throw new CartException(cartId);
        } 
        return cart;
    }
}

validateCart() 메서드는 장바구니 ID에 대한 장바구니 저장소 객체에서 장바구니 정보를 가져와 반환합니다. 장바구니 저장소 객체에 장바구니 ID가 없으면 예외 처리로 CartException() 메서드를 호출합니다.