public bool BuyDeal(BuyDealModel buyDealModel) { using (CovidCoinContext covidCoinEntities = new CovidCoinContext()) { var dealBought = covidCoinEntities.UserDeals.Where(x => x.userId == buyDealModel.userId && x.dealId == buyDealModel.userId && x.dealType.Equals("buy") && x.isActive == true).FirstOrDefault(); if (dealBought != null) { return(false); } else { var user = covidCoinEntities.Users.Where(x => x.Id == buyDealModel.userId && x.IsActive == true).FirstOrDefault(); user.LeverageBalance = user.LeverageBalance - buyDealModel.dealPrice * buyDealModel.dealShares; covidCoinEntities.Entry(user).State = EntityState.Modified; UserDeal deal = new UserDeal(); deal.dealId = buyDealModel.dealId; deal.userId = buyDealModel.userId; deal.dealShares = buyDealModel.dealShares; deal.dealPrice = buyDealModel.dealPrice; deal.dealType = "buy"; deal.isActive = true; deal.createdAt = DateTime.Now; covidCoinEntities.UserDeals.Add(deal); covidCoinEntities.SaveChanges(); return(true); } } }
public IHttpActionResult UserBuyDeal([FromBody] BuyDealModel buyDealModel) { return(Json(new DealService().BuyDeal(buyDealModel))); }