You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Assume that a user, let us call it USER_A, has a product P with a quantity x in its stored shoping cart.
Immagine that USER_A logged out from the session and went to the cart page. The current shopping list will be empty as expected.
Now, the user decided to add another product P with quantity z then he rememberd to log in. The current implementation will override the quantity of product P to z quantity.
Suggestion:
I belive that the quantity of product P should be the sum of (z+x). To achive that I implemented the StoredCartItems as below.
Code:
public async Task<ServiceResponse<List<CartProductResponse>>> StoreCartItems(List<CartItem> cartItems)
{
cartItems.ForEach(cartItem => cartItem.UserId = GetUserId());
// Get Stored Cart Items for the User
var cartItemsFromDB = await _context.CartItems
.Where(ci => ci.UserId == GetUserId())
.ToListAsync();
// If the locally stored cartItem is equal to the DB stored items then update quantity
// otherwise add the cartItem to the DB
foreach (var cartItem in cartItems)
{
var sameItem = cartItemsFromDB.Find(dbItem => dbItem.ProductId == cartItem.ProductId
&& dbItem.ProductTypeId == cartItem.ProductTypeId);
if (sameItem != null){
sameItem.Quantity += cartItem.Quantity;
// Update Existing item quantity
_context.CartItems.Update(sameItem);
}
else
{
// Add the new item to the DB
_context.CartItems.Add(cartItem);
}
}
await _context.SaveChangesAsync();
return await GetDbCartProducts();
}
The text was updated successfully, but these errors were encountered:
abouchrit
changed the title
Bug in StoreCartItems Method in the Server's CartService.cs
@patrickgod Bug in StoreCartItems Method in the Server's CartService.cs
Jun 17, 2023
Issue:
Assume that a user, let us call it USER_A, has a product P with a quantity x in its stored shoping cart.
Immagine that USER_A logged out from the session and went to the cart page. The current shopping list will be empty as expected.
Now, the user decided to add another product P with quantity z then he rememberd to log in. The current implementation will override the quantity of product P to z quantity.
Suggestion:
I belive that the quantity of product P should be the sum of (z+x). To achive that I implemented the StoredCartItems as below.
Code:
The text was updated successfully, but these errors were encountered: