public async Task InstantSell(Sell instantSell, int productId, decimal coinSell) { //Add the sell to Sell Table and save it await SellSave(instantSell); //Calculate realtime from bid(offer) table var moneyEarned = await RealTimePrice(productId, coinSell); //Find current product price var product = await db.Products.FindAsync(productId); //Add to cartSell table the new sell CartSell newCartSell = new CartSell { SellId = instantSell.SellId, ProductId = productId, Quantity = coinSell }; await CartSellSave(newCartSell); //Reduse the quantity of this product from portfolio var productNewQuantity = db.PortFolio.FirstOrDefault(x => x.ProductId == productId && x.UserPortofolioId == instantSell.UserSellId); productNewQuantity.CoinsQuantity -= newCartSell.Quantity; db.PortFolio.Update(productNewQuantity); //update the table await db.SaveChangesAsync(); //Increase the Wallet amount by moneyEarn in parameters //Find Client Wallet and reduse it var clientUser = await _userManager.FindByIdAsync(instantSell.UserSellId); clientUser.Wallet += moneyEarned; //Save the changes await _userManager.UpdateAsync(clientUser); }
//Save to CartSell Table public async Task CartSellSave(CartSell cartSell) { db.CartSell.Add(cartSell); await db.SaveChangesAsync(); }