Beispiel #1
0
        public void CreateNextSeason(string previousSeasonId, IUnitOfWorkRepository repository)
        {
            Season previousSeason;
             using (var seasonRepository = _repositoryFactory.CreateSeasonRepository())
             {
            previousSeason = seasonRepository.GetOne(previousSeasonId);
             }

             if (previousSeason.SeasonStatus != SeasonStatus.Ended)
             {
            throw new ConflictException("Season must be ended before a new one can be created");
             }

             var newSeasonInfo = new NewSeasonInfo
             {
            Game = previousSeason.Game,
            SeasonNumber = previousSeason.GameOrder + 1
             };

             // Determine which teams promote and relegate.
             IEnumerable<Team> allTeamsSortedOnLeagueAndPosition;
             using (var teamRepository = _repositoryFactory.CreateTeamRepository())
             {
            allTeamsSortedOnLeagueAndPosition = teamRepository.GetTeamsByGame(previousSeason.GameId).OrderBy(x => x.CurrentLeagueCompetition.Order).ThenBy(x => x.CurrentLeaguePosition);
             }

             var teamsGroupedPerLeague = allTeamsSortedOnLeagueAndPosition.GroupBy(t => t.CurrentLeagueCompetitionId).Select(grp => grp.ToList()).ToList();
             var newLeagues = _leagueManager.PromoteAndRelegateTeams(teamsGroupedPerLeague);

             newSeasonInfo.TeamsLeague1 = newLeagues[0];
             newSeasonInfo.TeamsLeague2 = newLeagues[1];
             newSeasonInfo.TeamsLeague3 = newLeagues[2];
             newSeasonInfo.TeamsLeague4 = newLeagues[3];

             // Determine champion of the highest league.
             newSeasonInfo.NationalChampion = teamsGroupedPerLeague[0][0];

             // Determine cup winner.
             using (var matchRepository = new RepositoryFactory().CreateMatchRepository())
             {
            var cupFinal = matchRepository.GetNationalCupFinal(previousSeason.Id);
            newSeasonInfo.NationalCupWinner = cupFinal.GetWinner();
             }

             // Now all teams have been placed in the right leagues, so create match schedules for all competitions.
             var seasonAndCompetitionSchedules = CreateSeasonAndCompetitionSchedules(newSeasonInfo);

             // Insert the season and all competition schedules.
             InsertSeasonAndCompetitionSchedule(repository, seasonAndCompetitionSchedules);
        }