I'm trying to pass a Function to the method. Problem is that I cannot make it work without repeating the same argument.
public void addCartProduct(String cartId, long productId) {
updateCartProducts(cartId, productCartDto -> productCartDto.addProduct(show(productId)));
}
public void removeCartProduct(String cartId, long productId) {
updateCartProducts(cartId, productCartDto -> productCartDto.removeProduct(show(productId)));
}
private void updateCartProducts(String cartId, Function<ProductCartDto,ProductCartDto> processCart){
productCartRepository.showCart(cartId)
.map(processCart)
.map(productCartDto -> productCartRepository.updateCart(cartId, productCartDto))
.orElseThrow(() -> new NoResultException("Cannot find cart ID: " + cartId));
}
EDIT: add show method
public ProductBasicDto show(Long productId) {
return productRepository
.findOptionalById(productId)
.orElseThrow(() -> new NoResultException("Cannot find product ID: " + productId));
}
Here method show() is redundant. Is there any possibility to make updateCartProducts responsible for this method? I cannot make any changes in existing classes. The difference is that first one adds product and the second removes
showmethod