0

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

3
  • 2
    You haven't shown the show method Commented Mar 11, 2018 at 15:37
  • I'm not sure what you want to improve here as both addProduct and removeProduct take something returned by the show method Commented Mar 11, 2018 at 16:02
  • ahh sorry guys, I added show method. And also add/remove methods returns ProductCartDto Commented Mar 11, 2018 at 16:28

1 Answer 1

1

IMO there is nothing wrong with your code. If you want to get rid of the show method call in those two methods, you can move that into the updateCartProducts method and replace the Function with a BiFunction that takes a ProductCartDto and a ProductBasicDto and produces a ProductCartDto.

public void addCartProduct(String cartId, long productId) {
    updateCartProducts(cartId, (productCartDto, productBasicDto) -> productCartDto.addProduct(productBasicDto));
}

public void removeCartProduct(String cartId, long productId) {
    updateCartProducts(cartId, (productCartDto, productBasicDto) -> productCartDto.removeProduct(productBasicDto));
}

private void updateCartProducts(String cartId, BiFunction<ProductCartDto, ProductBasicDto, ProductCartDto> processCart){
    //The code from show method
    ProductBasicDto productBasicDto = productRepository
            .findOptionalById(productId)
            .orElseThrow(() -> new NoResultException("Cannot find product ID: " + productId));

    productCartRepository.showCart(cartId)
            .map(processCartDto -> processCart.apply(processCartDto, productBasicDto))
            .map(productCartDto -> productCartRepository.updateCart(cartId, productCartDto))
            .orElseThrow(() -> new NoResultException("Cannot find cart ID: " + cartId));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.