public async Task SellOrderAsync(string userId, ApiSellOrder info) { var changesNotes = new List <ApiProdCountChange>(); await _semaphoreSlim.WaitAsync(); try { await using (var context = _di.GetRequiredService <ApplicationDbContext>()) { await using (var transaction = await context.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted)) { var date = DateTime.UtcNow; var order = _mapper.Map <Order>(info); order.OpenDate = date; order.Status = OrderStatus.Open; order.ResponsibleUserId = userId; order.Transactions = new List <ProductAction>(); foreach (var productOrder in info.ProductOrders) { var from = getStockAndVerify(productOrder.IdProduct, productOrder, context); changesNotes.Add(new ApiProdCountChange { ProductId = productOrder.IdProduct, WarehouseId = productOrder.FromId, NewCount = from.Quantity, OldCount = from.Quantity + productOrder.Quantity }); var prodOrder = new ProductAction { Date = date, ProductId = productOrder.IdProduct, Quantity = -productOrder.Quantity, WarehouseId = productOrder.FromId, Description = productOrder.Description, Price = productOrder.Price, BuyPrice = from.Product.RecommendedBuyPrice, UserId = userId, Operation = OperationDescription.Sold }; order.Transactions.Add(prodOrder); } context.Orders.Add(order); await context.SaveChangesAsync(); await transaction.CommitAsync(); } } await Informer.ProductsCountChangedAsync(changesNotes).ConfigureAwait(false); } finally { _semaphoreSlim.Release(); } }
public async Task <ApiResponseBase> Sell([FromBody] ApiSellOrder model, [FromServices] TrackerHub hub) { if (!ModelState.IsValid) { return(ModelState.ToApiBaseResponse()); } var user = await GetCurrentUserAsync(); await _houseRepo.SellOrderAsync(user?.Id, model).ConfigureAwait(false); return(new ApiResponseBase()); }