コード例 #1
0
        public async Task <IActionResult> PlaceBet(BetForPlaceDto betForPlace)
        {
            int userId;

            Int32.TryParse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out userId);

            var match = await matchRepository.GetMatch(betForPlace.MatchId);

            float odds;

            if (match.Date < DateTime.Now)
            {
                return(BadRequest("You cannot bet on a past match."));
            }

            if (await betRepository.BetPlaced(userId, match.Id))
            {
                return(BadRequest("You already placed a bet on this match!"));
            }

            if (betForPlace.Prediction == 0)
            {
                odds = match.DrawOdds;
            }
            else if (betForPlace.Prediction == 1)
            {
                odds = match.TeamAOdds;
            }
            else if (betForPlace.Prediction == 2)
            {
                odds = match.TeamBOdds;
            }
            else
            {
                return(BadRequest("Something went wrong. Try again."));
            }

            var bet = new Bet
            {
                Match      = match,
                Prediction = betForPlace.Prediction,
                UserId     = userId,
                Odds       = odds
            };

            betRepository.Place(bet);

            if (await dataContext.Commit())
            {
                return(CreatedAtRoute("GetBet", new { id = bet.Id }, bet));
            }

            return(BadRequest("Could not place bet."));
        }