Skip to content

Commit

Permalink
addToCart: better error mesage
Browse files Browse the repository at this point in the history
  • Loading branch information
thiennn committed Oct 27, 2021
1 parent 2bce252 commit 342a830
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IActionResult> AddToCart([FromBody] AddToCartModel model)
}
else
{
return Ok(new { Error = true, Message = result.Error });
return Ok(result);
}
}

Expand All @@ -64,7 +64,7 @@ public async Task<IActionResult> AddToCartResult(long productId)
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetActiveCartDetails(currentUser.Id);

var model = new AddToCartResult(_currencyService)
var model = new AddToCartResultVm(_currencyService)
{
CartItemCount = cart.Items.Count,
CartAmount = cart.SubTotal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels.AddToCartResult
@model SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels.AddToCartResultVm

<div class="modal-header">
<h5 class="modal-title" id="myModalLabel">@Localizer["The product has been added to your cart"]</h5>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ public async Task<Cart> GetActiveCart(long customerId, long createdById)
.Where(x => x.CustomerId == customerId && x.CreatedById == createdById && x.IsActive).FirstOrDefaultAsync();
}

public async Task<Result> AddToCart(long customerId, long productId, int quantity)
public async Task<AddToCartResult> AddToCart(long customerId, long productId, int quantity)
{
return await AddToCart(customerId, customerId, productId, quantity);
}

public async Task<Result> AddToCart(long customerId, long createdById, long productId, int quantity)
public async Task<AddToCartResult> AddToCart(long customerId, long createdById, long productId, int quantity)
{
if(quantity <= 0)
var addToCartResult = new AddToCartResult { Success = false };

if (quantity <= 0)
{
return Result.Fail(_localizer["The quantity must be larger than zero"].Value);
addToCartResult.ErrorMessage = _localizer["The quantity must be larger than zero"].Value;
addToCartResult.ErrorCode = "wrong-quantity";
return addToCartResult;
}
var cart = await GetActiveCart(customerId, createdById);
if (cart == null)
Expand All @@ -79,7 +83,9 @@ public async Task<Result> AddToCart(long customerId, long createdById, long prod
{
if (cart.LockedOnCheckout)
{
return Result.Fail(_localizer["Cart is locked for checkout. Please complete the checkout first."].Value);
addToCartResult.ErrorMessage = _localizer["Cart is locked for checkout. Please complete the checkout first."].Value;
addToCartResult.ErrorCode = "cart-locked";
return addToCartResult;
}

cart = await _cartRepository.Query().Include(x => x.Items).FirstOrDefaultAsync(x => x.Id == cart.Id);
Expand All @@ -105,7 +111,8 @@ public async Task<Result> AddToCart(long customerId, long createdById, long prod

await _cartRepository.SaveChangesAsync();

return Result.Ok();
addToCartResult.Success = true;
return addToCartResult;
}

public async Task<CartVm> GetActiveCartDetails(long customerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace SimplCommerce.Module.ShoppingCart.Services
{
public interface ICartService
{
Task<Result> AddToCart(long customerId, long productId, int quantity);
Task<AddToCartResult> AddToCart(long customerId, long productId, int quantity);

Task<Result> AddToCart(long customerId, long createdById, long productId, int quantity);
Task<AddToCartResult> AddToCart(long customerId, long createdById, long productId, int quantity);

IQueryable<Cart> Query();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ $(function () {
data: JSON.stringify({ productId: Number(productId), quantity: Number(quantity) }),
contentType: "application/json"
}).done(function (data) {
if (data.error) {
$('#shopModal').find('.modal-content').html(cartLockedError);
if (data.success === false) {
if (data.errorCode === "cart-locked") {
$('#shopModal').find('.modal-content').html(cartLockedError);
} else {
$('#shopModal').find('.modal-content').html(generalError).find('.modal-body').text(data.errorMessage);
}
} else {
$('#shopModal').find('.modal-content').html(data);
$('.cart-badge .badge').text($('#shopModal').find('.cart-item-count').text());
Expand Down

0 comments on commit 342a830

Please sign in to comment.