public virtual Result AddBuyOrder(BuyOrderModel buyOrder) { if (!buyOrder.Specie.Id.HasValue) { return(Error("Specie.Id Required")); } var species = this.speciesRepository.FindById(buyOrder.Specie.Id.Value); if (species == null) { return(Error("Species not found")); } var speciesStock = this.stockRepository.FindStockBySpecieId(buyOrder.Specie.Id.Value); if (speciesStock == null) { return(Error("There aren't any stock for this species")); } if (speciesStock.Quantity < buyOrder.Quantity) { return(Error("Tere aren't enough stock to complete this operation")); } speciesStock.Quantity -= buyOrder.Quantity; this.stockRepository.Update(speciesStock); return(Ok(speciesStock)); }
public IActionResult CreateBuyOrder([FromBody] BuyOrderModel buyOrder) { return(Json(fishMarketService.AddBuyOrder(buyOrder))); }
public IActionResult BuyCoin(BuyOrderModel model) { using (var context = new MyAngularAppContext()) { var saleData = context.SaleOrder.Where(x => x.Rate <= model.Rate).ToList(); var totalCoin = model.Coin; var buyCoin = 0; var isCompleted = false; foreach (var item in saleData) { if (item.Coin <= (totalCoin - buyCoin)) { Transaction objTransaction = new Transaction(); objTransaction.UserId = 1; objTransaction.TransactionType = 1; objTransaction.SaleUserId = item.UserId; objTransaction.TransactionDateTime = DateTime.Now; objTransaction.Coin = item.Coin; objTransaction.Rate = model.Rate; context.Transaction.Add(objTransaction); context.SaveChanges(); buyCoin += (int)objTransaction.Coin; var saleCoin = context.SaleOrder.Find(item.OrderId); if (saleCoin != null) { var remaining_coin = saleCoin.Coin - objTransaction.Coin; if (remaining_coin <= 0) { context.SaleOrder.Remove(saleCoin); } else { saleCoin.Coin = remaining_coin; } context.SaveChanges(); } } else { Transaction objTransaction = new Transaction(); objTransaction.UserId = 1; objTransaction.TransactionType = 1; objTransaction.SaleUserId = item.UserId; objTransaction.TransactionDateTime = DateTime.Now; objTransaction.Coin = (totalCoin - buyCoin); objTransaction.Rate = item.Rate; context.Transaction.Add(objTransaction); context.SaveChanges(); buyCoin += (int)objTransaction.Coin; var saleCoin = context.SaleOrder.Find(item.OrderId); if (saleCoin != null) { var remaining_coin = saleCoin.Coin - objTransaction.Coin; if (remaining_coin <= 0) { context.SaleOrder.Remove(saleCoin); } else { saleCoin.Coin = remaining_coin; } context.SaveChanges(); } } if (totalCoin == buyCoin) { isCompleted = true; break; } } if (!isCompleted) { BuyOrder objBuyOrder = new BuyOrder(); objBuyOrder.Coin = (totalCoin - buyCoin); objBuyOrder.Rate = model.Rate; objBuyOrder.UserId = 1; context.BuyOrder.Add(objBuyOrder); context.SaveChanges(); } return(this.Ok(ApiResponse.SetResponse(ApiResponseStatus.Ok, "Order Placed Successfully ", null))); } }