public async Task <IActionResult> PlaceLayOffer([FromBody] List <OfferDto> offer) { double laibelty = 0.00; foreach (var item in offer) { laibelty = laibelty + item.Stake * item.Odds; } if (await _OfferService.CheckLaiaabilty(laibelty, offer[0].UserId) == false) { return(BadRequest("you don't have enough cash to place this bet")); } else { await _accountService.TakeFromBalance(offer[0].UserId, laibelty); } List <List <Bet> > bettoreturn = new List <List <Bet> >(); foreach (var item in offer) { item.MarketName = item.MarketName.Replace("%2F", "/"); var Backexsist = await _OfferService.CheckExsitingBackbets(item.Odds, item.MarketName, item.EventId, item.RunnerName); if (Backexsist != null) { var exsist = await _OfferService.GetBackOffer(Backexsist); if (exsist.Liquidity != 0) { if (exsist.Liquidity == item.Stake || exsist.Liquidity > item.Stake) { await _OfferService.TakeFromBackLiquidity(exsist.Id, item.Stake); var matchedbet = _mapper.Map <Bet>(item); var betinvolved = await _OfferService.ManageInvolvedBackBets(exsist.Id, matchedbet); matchedbet.Status = "closed"; matchedbet.RemainingStake = 0; matchedbet.InStake = item.Stake; matchedbet.Lalaibelty = laibelty; matchedbet.Profit = item.Stake; var bettoadd = await _OfferService.AddToBets(matchedbet); await _BetCSer.addtoCounter(matchedbet.EventId); await _OfferService.AddToBackMatchedbet(exsist.Id, bettoadd); betinvolved.Add(bettoadd); SendNotification(betinvolved, "MatchingBet"); bettoreturn.Add(betinvolved); } else if (exsist.Liquidity < item.Stake) { var remainingStake = item.Stake - exsist.Liquidity; var matchedbet = _mapper.Map <Bet>(item); var betinvolved = await _OfferService.ManageInvolvedLowBackBets(exsist.Id, matchedbet); matchedbet.InStake = exsist.Liquidity; matchedbet.RemainingStake = 0; matchedbet.Status = "closed"; matchedbet.Lalaibelty = exsist.Liquidity * item.Odds; matchedbet.Profit = exsist.Liquidity; var bettoadd = await _OfferService.AddToBets(matchedbet); await _BetCSer.addtoCounter(matchedbet.EventId); await _OfferService.AddToBackMatchedbet(exsist.Id, bettoadd); var x = _mapper.Map <LayOffer>(item); x.Liquidity = remainingStake; var newlayack = await _OfferService.AddLayOffer(x); var c = _mapper.Map <Bet>(x); var added = await _OfferService.AddToBets(c); await _BetCSer.addtoCounter(added.EventId); var betLayinvoled = _mapper.Map <InvolvedLayBets>(added); await _OfferService.AddToInvolvedLayBets(newlayack.Id, betLayinvoled); betinvolved.Add(bettoadd); SendNotification(betinvolved, "MatchingBet"); bettoreturn.Add(betinvolved); } } } var Layexsist = await _OfferService.CheckExsitingLaybets(item.Odds, item.MarketName, item.EventId, item.RunnerName); if (Layexsist != null) { List <Bet> laybets = new List <Bet>(); await _OfferService.AddToLayLiquidity(Layexsist, item.Stake); var laykbet = _mapper.Map <Bet>(item); laybets.Add(laykbet); var added = await _OfferService.AddToBets(laykbet); await _BetCSer.addtoCounter(added.EventId); var involved = _mapper.Map <InvolvedLayBets>(added); involved.Status = "open"; await _OfferService.AddToInvolvedLayBets(Layexsist, involved); SendNotification(laybets, "MatchingBet"); bettoreturn.Add(laybets); } if (Layexsist == null && Backexsist == null) { List <Bet> newlaybet = new List <Bet>(); var betoffer = _mapper.Map <LayOffer>(item); var Layoffer = await _OfferService.AddLayOffer(betoffer); var bet = _mapper.Map <Bet>(betoffer); newlaybet.Add(bet); var added2 = await _OfferService.AddToBets(bet); await _BetCSer.addtoCounter(added2.EventId); var betlayinvoled = _mapper.Map <InvolvedLayBets>(added2); await _OfferService.AddToInvolvedLayBets(Layoffer.Id, betlayinvoled); SendNotification(newlaybet, "MatchingBet"); bettoreturn.Add(newlaybet); } } return(Ok(bettoreturn)); }