public async Task <IActionResult> updateScore(int id, ScoreForUpdateDto score)
        {
            var match = await matchRepository.GetMatch(id);

            if (match == null)
            {
                return(BadRequest("Match does not exist."));
            }

            if (match.Date >= DateTime.Now)
            {
                return(BadRequest("You cannot assign score to a future match."));
            }

            var scoreToUpdate = new Score
            {
                MatchId    = id,
                TeamAGoals = score.TeamAGoals,
                TeamBGoals = score.TeamBGoals
            };


            var result = 0;

            if (score.TeamAGoals > score.TeamBGoals)
            {
                result = 1;
            }
            else if (score.TeamAGoals < score.TeamBGoals)
            {
                result = 2;
            }

            match.Score = scoreToUpdate;

            var bets = betRepository.GetBetsByMatch(id);

            foreach (var bet in bets)
            {
                var user = await userRepository.GetUser(bet.UserId);

                if (bet.Prediction == result)
                {
                    user.Score += bet.Odds;
                }
                else
                {
                    user.Score -= 1;
                }
            }


            if (await dataContext.Commit())
            {
                return(NoContent());
            }

            throw new Exception($"Updating score of match {id} failed on save.");
        }
Beispiel #2
0
        public async Task <IActionResult> GetBetsByMatch(int matchId)
        {
            var bets = betRepository.GetBetsByMatch(matchId);

            return(Ok(bets));
        }