Skip to content

Commit

Permalink
Avoid product quantity overflow (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Dec 21, 2024
1 parent 230310c commit e42ceaa
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public async Task<AddToCartResult> AddToCart(long customerId, long productId, in
return addToCartResult;
}

if (quantity > int.MaxValue)
{
return CreateQuantityOverflowError();
}

var cartItem = await _cartItemRepository.Query().FirstOrDefaultAsync(x => x.ProductId == productId && x.CustomerId == customerId);

if (cartItem == null)
{
cartItem = new CartItem
Expand All @@ -63,13 +69,27 @@ public async Task<AddToCartResult> AddToCart(long customerId, long productId, in
}
else
{
if (cartItem.Quantity + quantity > int.MaxValue)
{
return CreateQuantityOverflowError();
}

cartItem.Quantity = cartItem.Quantity + quantity;
}

await _cartItemRepository.SaveChangesAsync();

addToCartResult.Success = true;

return addToCartResult;

AddToCartResult CreateQuantityOverflowError()
{
addToCartResult.ErrorMessage = _localizer["The quantity must be larger than zero"].Value;
addToCartResult.ErrorCode = "wrong-quantity";

return addToCartResult;
}
}

// TODO separate getting product thumbnail, varation options from here
Expand Down

0 comments on commit e42ceaa

Please sign in to comment.