Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid product quantity overflow #1113

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading