This is the code for adding addon product to carts. Suppose the cart has a cake so a candle would be an addon. I see this addonCart.getItems().stream().findFirst().get()
repeated and want to use this once among other usages.
Also how do I remove this declaration of exception:
catch (UnsupportedOperationException e)
Adding addon products to cart
public String addAddonToCart(final Cart addonCart) {
log.info("Start : addAddonToCart method started");
final var cart = this.cartRepository.findById(addonCart.getId()).map(e -> e).orElseThrow(() -> {
throw new ResourceNotFoundException(new ErrorInfo(ErrorConstants.CART_NOT_FOUND));
});
final var items = cart.getItems().stream().map(item -> {
if (item.getItemId().equals(addonCart.getItems().stream().findFirst().get().getItemId())) {
if (Objects.isNull(item.getAddOns())) {
item.setAddOns(addonCart.getItems().stream().findFirst().get().getAddOns());
} else {
try {
item.getAddOns().addAll(addonCart.getItems().stream().findFirst().get().getAddOns());
} catch (UnsupportedOperationException e) {
}
}
}
return item;
}).collect(Collectors.toList());
cart.setItems(items);
this.cartRepository.save(cart);
log.info("End : addAddonToCart method ended");
return Constants.CART_ADDON_ADDED_SUCCESSFULLY;
}
Delete item from cart
/**
* Delete product from cart.
*
* @param itemId the item id
* @return the string
*/
public String deleteProductFromCart(final String itemId) {
log.info("Start : deleteProductFromCart method started");
final var cart = this.cartRepository.findByItemsItemId(itemId);
if (cart.isPresent()) {
final var itemList = cart.get().getItems().stream().filter(item -> {
return !item.getItemId().equals(itemId);
}).collect(Collectors.toList());
cart.get().setItems(itemList);
this.cartRepository.save(cart.get());
} else {
log.info("End : deleteProductFromCart method ended");
throw new ResourceNotFoundException(new ErrorInfo(ErrorConstants.PRODUCT_NOT_FOUND));
}
log.info("End : deleteProductFromCart method ended");
return Constants.CART_ITEM_DELETE_SUCCESSFULLY;
}
Delete addon from cart
public String deleteAddonFromCart(Cart addonCart) {
log.info("Start : deleteAddonFromCart method started");
final var itemId = addonCart.getItems().stream().findFirst().get().getItemId();
final var addonId = addonCart.getItems().stream().findFirst().get().getAddOns().stream().findFirst().get()
.getProductId();
final var cart = this.cartRepository.findByItemsItemId(itemId).map(e -> e).orElseThrow(() -> {
throw new ResourceNotFoundException(new ErrorInfo(ErrorConstants.CART_NOT_FOUND));
});
final var items = cart.getItems().stream().filter(item -> item.getItemId().equals(itemId)).map(item -> {
try {
item.getAddOns().removeIf(addon -> addon.getProductId().equals(addonId));
} catch (UnsupportedOperationException e) {
}
return item;
}).collect(Collectors.toList());
try {
cart.getItems().removeIf(item -> item.getItemId().equals(itemId));
cart.getItems().addAll(items);
} catch (UnsupportedOperationException e) {
}
this.cartRepository.save(cart);
log.info("Start : deleteAddonFromCart method ended");
return Constants.CART_ADDON_DELETED_SUCCESSFULLY;
}
Cart Entity
@NoArgsConstructor
@Document("cart")
@JsonInclude(JsonInclude.Include.NON_NULL)
@EqualsAndHashCode(callSuper = true)
public class Cart extends BaseEntity implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The id. */
@NotNull(message = "cartId can not be null", groups = { CartViews.AddonAddView.class })
@NotBlank(message = "cartId can not be blank", groups = { CartViews.AddonAddView.class })
@Id
@JsonView({ CartViews.AddonAddView.class })
private String id;
/** The domain id. */
private String domainId;
/** The checkout id. */
private String checkoutId;
/** The user IP. */
private String userIP;
/** The agents. */
@JsonProperty("agents")
private List<Agent> agents;
/** The customer party id. */
// @JsonView({ CartViews.AddItemDetailsView.class })
private String customerPartyId;
/** The guuid. */
// @JsonView({ CartViews.AddItemDetailsView.class })
private String guuid;
/** The sender. */
@JsonProperty("sender")
private Sender sender;
/** The channel id. */
@NotNull(message = "channelId can not be null", groups = { CartViews.AddItemDetailsView.class })
@NotBlank(message = "channelId can not be blank", groups = { CartViews.AddItemDetailsView.class })
@JsonView({ CartViews.AddItemDetailsView.class })
private String channelId;
/** The action. */
private String action;
/** The reason id. */
private String reasonId;
/** The sub reason id. */
private String subReasonId;
/** The comments. */
private String comments;
/** The cart total. */
private Float cartTotal;
/** The tax included in cart total. */
private Float taxIncludedInCartTotal;
/** The tax on cart total. */
private Float taxOnCartTotal;
/** The adjustment. */
@JsonProperty("adjustment")
private List<Adjustment> adjustment;
/** The status. */
private String status;
/** The items. */
@Valid
@NotNull(message = "items can not be null", groups = { CartViews.AddItemDetailsView.class,
CartViews.AddonDeleteView.class, CartViews.AddonAddView.class })
@JsonProperty("items")
@JsonView({ CartViews.AddItemDetailsView.class, CartViews.AddonDeleteView.class, CartViews.AddonAddView.class })
private List<Item> items;
}
Cart
. \$\endgroup\$