I have been working on developing am API which basically adds/updates all the products from a wishlist to the user's cart.
The requirements of the API are as follows:
The logic that I had implemented deals with a 2 nested for loop, one loops the wishlist entries and the other loops the cart entries if the cart isn't empty. This logic works well for adding a product but I am having trouble updating the products.
When I hit the API on an empty cart I see no issue all the products were added perfectly, but when I try to hit the API again the only one item of the cart is getting updated while the others stay the same, this is happening cause when the index of the wishlist is incrementing to check for the next product the previously updated products which were updated to the sum of the both quantities are now overridden by the addToCart()
method.
like if there are 2 same products on the wishlist and the cart namely Product A and Product B on the two for loops
There will be four conditions
sl cart wishlist operation
1 A A update
2 A B add
3 B A add
4 B B update
For case 1 the product gets updated at first but the is getting overridden by the addToCart method in the second iteration.
How do I solve this?
Suggestions to other type of implementation is also welcome but I can't change most of the pre-existing methods such as AddToCart()
or UpdateCart()
.
The implementation
public void addAllToCart(String wishListId)
{
//code for finding the wishlist by ID saves the wishlist on wishlist of type WishListModel
//code for getting session cart saves on cart of type CartModel
if (CollectionUtils.isNotEmpty(wishlist.getEntries()))
{
for (WishListEntryModel wishListEntry : wishlist.getEntries())
{
checkInCartEntry(wishListEntry, cart.getEntries());
}
}
}
private void checkInCartEntry(WishListEntryModel wishListEntry, List<AbstractOrderEntryModel> cartEntries)
{
if (CollectionUtils.isEmpty(cartEntries))
{
addToCart(wishListEntry);
}
else
{
for (AbstractOrderEntryModel cartEntry : cartEntries)
{
if (wishListEntry.getProduct().getCode().equalsIgnoreCase(cartEntry.getProduct().getCode()))
{
updateCartEntry(cartEntry.getEntryNumber, wishlist.getQuantity+cartEntry.getQuantity);
}
else
{
addToCart(wishListEntry);
}
}
}
}
Thanks for all the comments and answers.
I solved this issue by separating adding, and updating tasks in the methods.
Nested loops which I was using earlier as correctly pointed out by @Mak in their answer never break thus causing the addToCart()
method to override the manipulations made earlier.
but instead of using a map, I used a more simple solution and did the following:
addToCart()
method and overwrite all the cart entries with the wishlist entryThe code is below
public void addAllToCart(String wishListId) {
WishListModel wishList = findByWishlistId(wishListId)
CartModel cart = cartService.getSessionCart();
CartModel tempCart = copyOf(cart);
if(CollectionUtils.isNotEmpty(wishList.getEntries())) {
for (WishListEntryModel wishListEntry : wishlist.getEntries()) {
cartFacade.addToCart(wishListEntry);
}
if(Objects.nonNull(tempCart)){
for (AbstractOrderEntryModel tempCartEntry : tempCart.getEntries()) {
checkInCartEntry(tempCartEntry, cart.getEntries());
}
}
}
}
private void checkInCartEntry(AbstractOrderEntryModel tempCartEntry, List<AbstractOrderEntryModel> cartEntries) throws CommerceCartModificationException {
for (AbstractOrderEntryModel cartEntry : cartEntries) {
if(!tempCartEntry.getProduct().getCode().isEmpty() && !cartEntry.getProduct().getCode().isEmpty() && (tempCartEntry.getProduct().getCode().equalsIgnoreCase(cartEntry.getProduct().getCode()))) {
cartFacade.updateCartEntry(cartEntry.getEntryNumber(), cartEntry.getQuantity());
}
}
}