Exemple #1
0
        public object Post(CreateCupRoundRequest request)
        {
            using (var transaction = Db.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                var cup = Db.SingleById <Cup>(request.Id);

                var matchesCurrentRound =
                    Db.LoadSelect(Db.From <Match>().Where(p => p.CupId == cup.Id && p.CupRound == cup.CurrentRound));
                var guestTeamIds = matchesCurrentRound.Select(s => s.GuestTeamId).ToList();
                var homeTeamIds  = matchesCurrentRound.Select(s => s.HomeTeamId).ToList();
                var teamIds      = guestTeamIds.Concat(homeTeamIds).Distinct().ToList();
                var teams        = Db.SelectByIds <Team>(teamIds);
                var leagueIds    = teams.Select(s => s.LeagueId).Distinct().ToList();
                var leagues      = Db.SelectByIds <League>(leagueIds);

                var newRoundMatches = MatchFactory.CreateNextCupRound(matchesCurrentRound, teams, leagues, Db);

                Db.Update <Cup>(new { CurrentRound = cup.CurrentRound + 1 }, p => p.Id == cup.Id);

                foreach (var match in newRoundMatches)
                {
                    // we can not use InsertAll as it opens a new transaction -> nested transaction are not supported
                    Db.Insert(match);
                }

                transaction.Commit();
            }

            return(null);
        }