public IActionResult StartTournament(int id)
        {
            var tournament    = _tournamentRepository.GetTournamentById(id);
            var tournamentDto = _mapper.Map <TournamentReadDto>(tournament);
            var usersDto      = _mapper.Map <List <UserReadDto> >(_userRepository.GetUsersByTournament(id));
            var byeUser       = _mapper.Map <UserReadDto>(_userRepository.GetByePlayer());

            TournamentCourse tournamentCourse = new TournamentCourse(usersDto, tournamentDto, byeUser);

            var matches = tournamentCourse.GetMatchesInCurrentRound()
                          .Select(match => _mapper.Map <Match>(match)).ToList();

            for (int i = 0; i < matches.Count; i++)
            {
                matches[i].Tournament = tournament;
                _repository.DetachLocal(matches[i], matches[i].Id);
                _repository.SaveMatch(matches[i]);
                _repository.SaveChanges();
                tournamentCourse.UpdateIds(i, matches[i].Id);
            }

            return(View("/Views/CourseTournament/CourseTournament.cshtml", tournamentCourse));
        }
        public IActionResult GetResultsTournament([FromForm] TournamentCourse tournamentCourse)
        {
            var               tournamentId  = int.Parse(Request.Form["TournamentId"]);
            Tournament        tournament    = _tournamentRepository.GetTournamentById(tournamentId);
            TournamentReadDto tournamentDto = _mapper.Map <TournamentReadDto>(tournament);

            if (tournamentCourse.Matches == null) //from /ongoingAll
            {
                tournamentCourse.TournamentDto = tournamentDto;
                tournamentCourse.Matches       = _repository.GetMatchesByTournamentId(tournamentId)
                                                 .Select(match => _mapper.Map <MatchDto>(match)).ToList();
                tournamentCourse.updateOngoing();

                return(View("/Views/CourseTournament/CourseTournament.cshtml", tournamentCourse));
            }
            int round           = 0;
            int numberOfMatches = tournamentCourse.Matches.Count;
            int currentRound    = _repository.GetTournamentRound(tournamentId);

            for (int i = 0; i < numberOfMatches; i++)
            {
                int id = int.Parse(Request.Form["MatchDto[" + i + "].Id"]);

                Match match = _repository.GetMatchById(id);
                if (currentRound == match.Round)
                {
                    if (match.Player1.Id == -1)
                    {
                        match.Winner = Winner.Two;
                    }
                    else if (match.Player2.Id == -1)
                    {
                        match.Winner = Winner.One;
                    }
                    else
                    {
                        match.Winner = tournamentCourse.Matches[i].Winner;
                    }

                    match.Result = tournamentCourse.Matches[i].Result;


                    _repository.SaveChanges(); //update finished matches
                    round = match.Round;       //last round
                }


                MatchDto matchDto = _mapper.Map <MatchDto>(match);
                matchDto.TournamentDto = tournamentDto;
                tournamentCourse.Matches.Add(matchDto);
            }

            tournamentCourse.UpdateOthers(tournamentDto, round);
            if (!tournamentCourse.IsFinished)
            {
                var matches = tournamentCourse.GetMatchesInCurrentRound()
                              .Select(match => _mapper.Map <Match>(match)).ToList();

                foreach (var match in matches)
                {
                    match.Tournament = tournament;
                    _repository.DetachLocal(match, match.Id);
                    _repository.SaveMatch(match);
                    _repository.SaveChanges();
                    MatchDto matchDto = _mapper.Map <MatchDto>(match);
                    tournamentCourse.Matches.Add(matchDto);
                }

                tournamentCourse.Matches.RemoveAll(m => m.Id == 0);
            }
            else
            {
                tournament.Completed = true;
                _tournamentRepository.SaveChanges();
                return(RedirectToAction(nameof(CompletedTournamentDetail), new { id = tournamentDto.Id }));
            }

            return(View("/Views/CourseTournament/CourseTournament.cshtml", tournamentCourse));
        }