Beispiel #1
0
        private FootballTeam UpdateHomeTeamPoints(UpdatePlayedGameDto game, bool gameDeletion, FootballTeam homeTeam)
        {
            if (game.Result == GameResult.Won)
            {
                if (!gameDeletion)
                {
                    homeTeam.Points += WINNING_POINTS;
                }
                else
                {
                    homeTeam.Points -= WINNING_POINTS;
                }
            }
            else if (game.Result == GameResult.Draw)
            {
                if (!gameDeletion)
                {
                    homeTeam.Points += DRAW_POINTS;
                }
                else
                {
                    homeTeam.Points -= DRAW_POINTS;
                }
            }

            return(homeTeam);
        }
Beispiel #2
0
        private FootballTeam UpdateAwayTeamPoints(UpdatePlayedGameDto game, bool gameDeletion, FootballTeam awayTeam)
        {
            if (game.Result == GameResult.Lost)
            {
                if (!gameDeletion)
                {
                    awayTeam.Points += WINNING_POINTS;
                }
                else
                {
                    awayTeam.Points -= WINNING_POINTS;
                }
            }
            else if (game.Result == GameResult.Draw)
            {
                if (!gameDeletion)
                {
                    awayTeam.Points += DRAW_POINTS;
                }
                else
                {
                    awayTeam.Points -= DRAW_POINTS;
                }
            }

            return(awayTeam);
        }
Beispiel #3
0
        public Result DeleteGame(int gameId)
        {
            Result result = new Result();

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    PlayedGameDto gameDto = this.gamesRepository.Find(gameId);
                    if (gameDto == null)
                    {
                        return(result.SetError($"There is no game with #{ gameId }!"));
                    }

                    this.gamesRepository.Delete(gameDto.ID);

                    UpdatePlayedGameDto deletedGame = Mapper.Map <PlayedGameDto, UpdatePlayedGameDto>(gameDto);
                    Result updateTeamPointsResult   = this.UpdateTeamsPoints(deletedGame, gameDeletion: true);
                    if (updateTeamPointsResult.IsError)
                    {
                        return(updateTeamPointsResult);
                    }

                    scope.Complete();
                }

                return(result.SetSuccess("Game deleted."));
            }
            catch (Exception ex)
            {
                return(result.SetError(ex.Message));
            }
        }
Beispiel #4
0
        public Result <PlayedGameDto> AddPlayedGame(AddPlayedGameDto gameDto)
        {
            Result <PlayedGameDto> result = new Result <PlayedGameDto>();

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    gameDto.DatePlayed = this.dateTimeProvider.GetCurrentDateTime();
                    PlayedGameDto newGameDto = this.gamesRepository.AddGame(gameDto);

                    UpdatePlayedGameDto addedGame = Mapper.Map <PlayedGameDto, UpdatePlayedGameDto>(newGameDto);
                    Result updatePointsResult     = this.UpdateTeamsPoints(addedGame, gameDeletion: false);
                    if (updatePointsResult.IsError)
                    {
                        result.SetError(updatePointsResult.Message);

                        return(result);
                    }

                    scope.Complete();

                    return(result.SetData(newGameDto));
                }
            }
            catch (Exception ex)
            {
                result.SetError(ex.Message);

                return(result);
            }
        }
Beispiel #5
0
        private Result UpdateTeamsPoints(UpdatePlayedGameDto game, bool gameDeletion)
        {
            Result result = this.UpdateHomeTeamWinOrDraw(game, gameDeletion);

            result = this.UpdateAwayTeamWinOrDraw(game, gameDeletion);

            return(result);
        }
Beispiel #6
0
 public IActionResult UpdateScore(UpdatePlayedGameDto model)
 {
     if (ModelState.IsValid)
     {
         var playedGame = _playedGamesService.GetById(model.Id);
         var winnerTeam = _playedGamesService.UpdateScore(playedGame, model.HomeTeamScore.Value, model.AwayTeamScore.Value);
         _playedGamesService.AddWinnerToNextGame(playedGame, winnerTeam);
     }
     else
     {
         model = _mapper.Map <UpdatePlayedGameDto>(_playedGamesService.GetAllById(model.Id));
         return(View(model));
     }
     return(RedirectToAction("Index", new { tournamentId = model.PlayedGamesRound.TournamentId }));
 }
Beispiel #7
0
        public ActionResult UpdatePlayedGame(UpdatePlayedGameViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            UpdatePlayedGameDto gameToUpdate = Mapper.Map <UpdatePlayedGameViewModel, UpdatePlayedGameDto>(viewModel);
            Result result = this.service.UpdateGame(gameToUpdate);

            if (result.IsError)
            {
                ModelState.AddModelError("", $"Error: { result.Message }");
            }

            return(RedirectToAction("PlayedGames", "PlayedGame"));
        }
Beispiel #8
0
        public Result UpdateGame(UpdatePlayedGameDto newGameDto)
        {
            Result result = new Result();

            try
            {
                PlayedGame oldPlayedGame = this.gamesRepository.FindRough(newGameDto.ID);
                if (newGameDto == null)
                {
                    return(result.SetError($"There is no game with #{ newGameDto.ID }!"));
                }

                using (var scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    UpdatePlayedGameDto oldGameDto = Mapper.Map <PlayedGame, UpdatePlayedGameDto>(oldPlayedGame);
                    Result updatePointsResult      = this.UpdateTeamsPoints(oldGameDto, gameDeletion: true);
                    if (updatePointsResult.IsError)
                    {
                        result.SetError(updatePointsResult.Message);

                        return(result);
                    }

                    oldPlayedGame.Result = newGameDto.Result;
                    this.gamesRepository.Update(oldPlayedGame);

                    updatePointsResult = this.UpdateTeamsPoints(newGameDto, gameDeletion: false);
                    if (updatePointsResult.IsError)
                    {
                        result.SetError(updatePointsResult.Message);

                        return(result);
                    }

                    scope.Complete();
                }

                return(result.SetSuccess("Game updated successfully."));
            }
            catch (Exception ex)
            {
                return(result.SetError(ex.Message));
            }
        }
Beispiel #9
0
        private Result UpdateAwayTeamWinOrDraw(UpdatePlayedGameDto game, bool gameDeletion)
        {
            Result result = new Result();

            if (game.Result == GameResult.Lost ||
                game.Result == GameResult.Draw)
            {
                FootballTeam awayTeam = this.teamsRepository.FindRough(game.AwayTeamId);
                if (awayTeam == null)
                {
                    return(result.SetError($"There is no team with id #{ game.AwayTeamId }!"));
                }

                awayTeam = this.UpdateAwayTeamPoints(game, gameDeletion, awayTeam);

                this.teamsRepository.Update(awayTeam);
            }

            return(result.SetSuccess("Team points updated."));
        }