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.");
        }
Esempio n. 2
0
        public IActionResult PartiallyUpdateScoreForUser(Guid userId, Guid id,
                                                         [FromBody] JsonPatchDocument <ScoreForUpdateDto> patchDoc)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            if (!_libraryRepository.UserExists(userId))
            {
                return(NotFound());
            }

            var scoreForUserFromRepo = _libraryRepository.GetScoreForUser(userId, id);

            if (scoreForUserFromRepo == null)
            {
                var scoreDto = new ScoreForUpdateDto();
                patchDoc.ApplyTo(scoreDto);

                var scoreToAdd = Mapper.Map <Score>(scoreDto);
                scoreToAdd.Id = id;

                _libraryRepository.AddScoreForUser(userId, scoreToAdd);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Upserting score {id} for author {userId} failed on save");
                }

                var scoreToReturn = Mapper.Map <ScoreDto>(scoreToAdd);
                return(CreatedAtRoute("GetScoreForUser",
                                      new { userId = userId, id = scoreToReturn.Id },
                                      scoreToReturn));
            }

            var scoreToPatch = Mapper.Map <ScoreForUpdateDto>(scoreForUserFromRepo);

            patchDoc.ApplyTo(scoreToPatch);

            //add validation

            Mapper.Map(scoreToPatch, scoreForUserFromRepo);

            _libraryRepository.UpdateScoreForUser(scoreForUserFromRepo);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Patching score {id} for user {userId} failed on save");
            }

            return(NoContent());
        }
Esempio n. 3
0
        public IActionResult UpdateScoreForUser(Guid userId, Guid id,
                                                [FromBody] ScoreForUpdateDto score)
        {
            if (score == null)
            {
                return(BadRequest());
            }

            //Check if the user exists
            if (!_libraryRepository.UserExists(userId))
            {
                return(NotFound());
            }

            //Check if the score exists
            var scoreForUserFromRepo = _libraryRepository.GetScoreForUser(userId, id);

            if (scoreForUserFromRepo == null)
            {
                //Map the score from the requset body to score entity
                var scoreToAdd = Mapper.Map <Score>(score);
                scoreToAdd.Id = id;

                _libraryRepository.AddScoreForUser(userId, scoreToAdd);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Upserting score {id} for user {userId} failed on save");
                }

                var scoreToReturn = Mapper.Map <ScoreDto>(scoreToAdd);

                return(CreatedAtRoute("GetScoreForUser",
                                      new { userId = userId, id = scoreToReturn.Id },
                                      scoreToReturn));
            }

            //map
            //apply update
            //map back to entity
            //all this is taken care by the automapper
            Mapper.Map(score, scoreForUserFromRepo);
            //Now the entity contains the updated info

            _libraryRepository.UpdateScoreForUser(scoreForUserFromRepo);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Updating score {id} for user {userId} failed on save.");
            }

            return(NoContent());
        }