Skip to content

Commit

Permalink
Conditionally delete the cart when creating an order
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarincev committed Apr 21, 2020
1 parent 722c2f0 commit 9b0b4b4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions VirtoCommerce.Storefront/Controllers/Api/ApiCartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ public async Task<ActionResult<OrderCreatedInfo>> CreateOrderFromDefaultCart([Fr
//Need lock to prevent concurrent access to same cart
using (await AsyncLock.GetLockByKey(cartBuilder.Cart.GetCacheKey()).LockAsync())
{
return await CreateOrderFromCartAsync(cartBuilder, bankCardInfo);
return await CreateOrderFromCartAsync(cartBuilder, bankCardInfo, removeCart: true);
}
}

// POST: storefrontapi/cart/{name}/{type}/createorder
// POST: storefrontapi/cart/{name}/{type}/createorder?removeCart=true
[HttpPost("{name}/{type}/createorder")]
[ValidateAntiForgeryToken]
public async Task<ActionResult<OrderCreatedInfo>> CreateOrderFromNamedCart([FromRoute] string name, [FromRoute] string type, [FromBody] BankCardInfo bankCardInfo)
public async Task<ActionResult<OrderCreatedInfo>> CreateOrderFromNamedCart([FromRoute] string name, [FromRoute] string type, [FromBody] BankCardInfo bankCardInfo, [FromQuery]bool removeCart)
{
var cartBuilder = await LoadOrCreateCartAsync(Uri.UnescapeDataString(name), type);
if (cartBuilder.Cart.IsTransient())
Expand All @@ -383,11 +383,11 @@ public async Task<ActionResult<OrderCreatedInfo>> CreateOrderFromNamedCart([From
//Need lock to prevent concurrent access to same cart
using (await AsyncLock.GetLockByKey(cartBuilder.Cart.GetCacheKey()).LockAsync())
{
return await CreateOrderFromCartAsync(cartBuilder, bankCardInfo);
return await CreateOrderFromCartAsync(cartBuilder, bankCardInfo, removeCart);
}
}

private async Task<OrderCreatedInfo> CreateOrderFromCartAsync(ICartBuilder cartBuilder, BankCardInfo bankCardInfo)
private async Task<OrderCreatedInfo> CreateOrderFromCartAsync(ICartBuilder cartBuilder, BankCardInfo bankCardInfo, bool removeCart)
{
var orderDto = await _orderApi.CreateOrderFromCartAsync(cartBuilder.Cart.Id);
var order = orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage);
Expand All @@ -397,7 +397,7 @@ private async Task<OrderCreatedInfo> CreateOrderFromCartAsync(ICartBuilder cartB
//Raise domain event asynchronously
_publisher.Publish(new OrderPlacedEvent(WorkContext, orderDto.ToCustomerOrder(WorkContext.AllCurrencies, WorkContext.CurrentLanguage), cartBuilder.Cart)),
//Remove the cart asynchronously
cartBuilder.RemoveCartAsync()
removeCart ? cartBuilder.RemoveCartAsync() : Task.CompletedTask
};
//Process order asynchronously
var incomingPaymentDto = orderDto.InPayments?.FirstOrDefault();
Expand Down

0 comments on commit 9b0b4b4

Please sign in to comment.