コード例 #1
0
        public async Task <IActionResult> PlaceBackOffer([FromBody] List <OfferDto> offer)
        {
            double laibelty = 0.00;

            foreach (var item in offer)
            {
                laibelty = laibelty + item.Stake;
            }
            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 layexsist = await _OfferService.CheckExsitingLaybets(item.Odds, item.MarketName, item.EventId, item.RunnerName);

                if (layexsist != null)
                {
                    var exsist = await _OfferService.GetLayOffer(layexsist);

                    if (exsist.Liquidity != 0)
                    {
                        if (exsist.Liquidity == item.Stake || exsist.Liquidity > item.Stake)
                        {
                            await _OfferService.TakeFromLayLiquidity(exsist.Id, item.Stake);

                            var matchedbet  = _mapper.Map <Bet>(item);
                            var betinvolved = await _OfferService.ManageInvolvedLayBets(exsist.Id, matchedbet);

                            matchedbet.Status         = "Closed";
                            matchedbet.RemainingStake = 0;
                            matchedbet.InStake        = item.Stake;
                            matchedbet.Lalaibelty     = laibelty;
                            matchedbet.Profit         = item.Stake * item.Odds;
                            var bettoadd = await _OfferService.AddToBets(matchedbet);

                            await _BetCSer.addtoCounter(bettoadd.EventId);

                            await _OfferService.AddToLayMatchedbet(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.ManageInvolvedLowLayBets(exsist.Id, matchedbet);

                            matchedbet.InStake        = exsist.Liquidity;
                            matchedbet.RemainingStake = 0;
                            matchedbet.Status         = "Closed";
                            matchedbet.Lalaibelty     = exsist.Liquidity;
                            matchedbet.Profit         = exsist.Liquidity * item.Odds;
                            var bettoadd = await _OfferService.AddToBets(matchedbet);

                            await _BetCSer.addtoCounter(bettoadd.EventId);

                            await _OfferService.AddToLayMatchedbet(exsist.Id, bettoadd);

                            var x = _mapper.Map <BackOffer>(item);
                            x.Liquidity = remainingStake;
                            var newBack = await _OfferService.AddBackOffer(x);

                            var c     = _mapper.Map <Bet>(x);
                            var added = await _OfferService.AddToBets(c);

                            await _BetCSer.addtoCounter(added.EventId);

                            var betbackinvoled = _mapper.Map <InvolvedBackBets>(added);
                            await _OfferService.AddToInvolvedBackBets(newBack.Id, betbackinvoled);

                            betinvolved.Add(matchedbet);
                            SendNotification(betinvolved, "MatchingBet");
                            bettoreturn.Add(betinvolved);
                        }
                    }
                }
                var backexsist = await _OfferService.CheckExsitingBackbets(item.Odds, item.MarketName, item.EventId, item.RunnerName);

                if (backexsist != null)
                {
                    List <Bet> backbets = new List <Bet>();
                    await _OfferService.AddToBackLiquidity(backexsist, item.Stake);

                    var backbet = _mapper.Map <Bet>(item);
                    backbets.Add(backbet);
                    var added = await _OfferService.AddToBets(backbet);

                    await _BetCSer.addtoCounter(added.EventId);

                    var involved = _mapper.Map <InvolvedBackBets>(added);
                    involved.Status = "open";
                    await _OfferService.AddToInvolvedBackBets(backexsist, involved);

                    SendNotification(backbets, "New Bet Accepted and waiting for matching");
                    bettoreturn.Add(backbets);
                }
                if (layexsist == null && backexsist == null)
                {
                    List <Bet> newbackbet = new List <Bet>();
                    var        betoffer   = _mapper.Map <BackOffer>(item);
                    var        backoffer  = await _OfferService.AddBackOffer(betoffer);

                    var bet = _mapper.Map <Bet>(betoffer);
                    newbackbet.Add(bet);
                    var added2 = await _OfferService.AddToBets(bet);

                    await _BetCSer.addtoCounter(added2.EventId);

                    var betlayinvoled = _mapper.Map <InvolvedBackBets>(added2);
                    await _OfferService.AddToInvolvedBackBets(backoffer.Id, betlayinvoled);

                    SendNotification(newbackbet, "New Bet Accepted and waiting for matching");
                    bettoreturn.Add(newbackbet);
                }
            }

            return(Ok(bettoreturn));
        }