public async Task <Unit> Handle(AddToCartCommand request, CancellationToken cancellationToken) { var stock = await _context.Stocks .Include(x => x.Product) .ThenInclude(x => x.Photos) .FirstOrDefaultAsync(x => x.Id == request.StockId, cancellationToken); if (request.Quantity > stock.Quantity) { throw new RestException(HttpStatusCode.BadRequest, new { AddToCart = $"Not enough quantity in stock: {stock.Id}" }); } stock.Quantity -= request.Quantity; var stocksOnHold = await _context.StocksOnHold .Where(x => x.SessionId == _sessionService.GetId()) .ToListAsync(cancellationToken); if (stocksOnHold.Any(x => x.StockId == request.StockId)) { stocksOnHold.Find(x => x.StockId == request.StockId) !.Quantity += request.Quantity; } else { await _context.StocksOnHold.AddAsync(new StockOnHold { SessionId = _sessionService.GetId(), StockId = request.StockId, ProductName = stock.Product.Name, ProductSize = stock.ProductSize, ProductColor = stock.ProductColor, Price = stock.Price, Quantity = request.Quantity, PhotoUrl = stock.Product.Photos.First(x => x.IsMain).Url, ExpiryDate = DateTime.Now.AddMinutes(60) }, cancellationToken); } stocksOnHold.ForEach(x => x.ExpiryDate = DateTime.Now.AddMinutes(60)); var cartProduct = new CartProduct { StockId = stock.Id, ProductName = stock.Product.Name, ProductSize = stock.ProductSize, ProductColor = stock.ProductColor, Price = stock.Price, Quantity = request.Quantity, PhotoUrl = stock.Product.Photos.First(x => x.IsMain).Url }; _sessionService.AddToCart(cartProduct); var success = await _context.SaveChangesAsync(cancellationToken) > 0; if (success) { return(Unit.Value); } throw new Exception("Problem adding to cart"); }