Exemplo n.º 1
0
        public object Post(MoveTeamToLeagueRequest request)
        {
            var team = Db.SingleById <Team>(request.TeamId);

            var oldLeagueId = team.LeagueId.GetValueOrDefault();

            using (var transaction = Db.BeginTransaction())
            {
                Db.Delete <Match>(sql =>
                                  sql.GuestTeamId == team.Id || sql.HomeTeamId == team.Id && sql.LeagueId == team.LeagueId);

                var targetLeague        = Db.SingleById <League>(request.Id);
                var teamsInTargetLeague = Db.Select <Team>(sql => sql.LeagueId == request.Id);

                var newMatches = MatchFactory.CreateMatchesForMovedTeam(team, teamsInTargetLeague, targetLeague);

                foreach (var match in newMatches)
                {
                    match.LeagueId = targetLeague.Id;
                    Db.Insert(match, true);
                }

                Db.Update <Team>(new { LeagueId = request.Id }, p => p.Id == request.TeamId);
                Db.Update <TableEntry>(new { LeagueId = request.Id },
                                       p => p.TeamId == request.TeamId && p.LeagueId == oldLeagueId);

                StandingsCalculator.Calculate(Db, oldLeagueId);
                StandingsCalculator.Calculate(Db, request.Id);

                transaction.Commit();
            }

            return(null);
        }
Exemplo n.º 2
0
        public object Put(EnterMatchResultRequest request)
        {
            ValidateResult(request);

            using (var transaction = this.Db.BeginTransaction())
            {
                var match = this.Db.LoadSingleById <Match>(request.Id);

                match.Throw404NotFoundIfNull("Match not found");

                this.EnsureUserMayEnterMatchResult(match);

                if (match.PlayDate == null)
                {
                    this.Db.Update <Match>(new { PlayDate = DateTime.UtcNow }, p => p.Id == request.Id);
                }

                this.Db.Update <Match>(new { request.HomeTeamScore, request.GuestTeamScore, ResultDate = DateTime.UtcNow }, p => p.Id == request.Id);

                var dto = this.Get(new GetMatchByIdRequest {
                    Id = request.Id
                });

                StandingsCalculator.Calculate(this.Db, match.LeagueId);

                transaction.Commit();

                return(dto);
            }
        }
        public void Calculate_ShouldReturnTheScoreOfTheGameAsString(int scorePlayerA, int scorePlayerB, string expectedResult)
        {
            var testee = new StandingsCalculator();

            var result = testee.Calculate(scorePlayerA, scorePlayerB);

            result.Should().Be(expectedResult);
        }
Exemplo n.º 4
0
        public void Get(SetMatchesNotPlayedRequest request)
        {
            var matchIds = this.Db.Select(Db.From <MatchView>().Where(p => p.CompetitionId == request.CompetitionId && p.PlayDate == null && !p.HomeTeamIsForfaitOut && !p.GuestTeamIsForfaitOut)).Select(x => x.Id);

            Db.Update <Match>(new { IsNotPlayedMatch = true }, x => matchIds.Contains(x.Id));

            var competition = Db.LoadSingleById <Competition>(request.CompetitionId);

            foreach (var league in competition.Leagues)
            {
                StandingsCalculator.Calculate(Db, league.Id);
            }
        }
Exemplo n.º 5
0
        public object Post(UpdateTeamRequest request)
        {
            var originalTeam = Db.SingleById<Team>(request.Id);
            var shouldUpdateStandings = originalTeam.IsForfaitOut != request.IsForfaitOut;
            originalTeam.PopulateWith(request);
            Db.Save(originalTeam);

            if (shouldUpdateStandings && originalTeam.LeagueId.HasValue)
            {
                StandingsCalculator.Calculate(Db, originalTeam.LeagueId.Value);
            }

            return Db.SingleById<Team>(request.Id).ConvertTo<TeamDto>();
        }
Exemplo n.º 6
0
        public MatchViewDto Post(ResetMatchRequest request)
        {
            var matchToReset = Db.SingleById <Match>(request.Id);

            matchToReset.PlayDate         = null;
            matchToReset.RegisterDate     = null;
            matchToReset.ResultDate       = null;
            matchToReset.HomeTeamScore    = null;
            matchToReset.GuestTeamScore   = null;
            matchToReset.TableId          = null;
            matchToReset.IsNotPlayedMatch = false;
            Db.Save(matchToReset);

            StandingsCalculator.Calculate(this.Db, matchToReset.LeagueId);

            return(Db.SingleById <MatchView>(request.Id).ConvertTo <MatchViewDto>());
        }
Exemplo n.º 7
0
        public object Delete(DeleteMatchResultRequest request)
        {
            var match = Db.LoadSingleById <Match>(request.MatchId);

            EnsureUserMayEnterMatchResult(match);

            match.HomeTeamScore  = null;
            match.GuestTeamScore = null;
            match.ResultDate     = null;

            Db.Update(match);

            StandingsCalculator.Calculate(this.Db, match.LeagueId);

            return(Get(new GetMatchByIdRequest {
                Id = request.MatchId
            }));
        }
Exemplo n.º 8
0
        public void Post(CreateTeamsAndMatchesRequest request)
        {
            using (var transaction = this.Db.OpenTransaction(IsolationLevel.ReadCommitted))
            {
                long competitionId = request.CompetitionId;

                var teamInscriptions = this.Db.Select(Db.From <TeamInscription>().Where(p => p.CompetitionId == competitionId && p.AssignedLeagueId != null));
                var teams            = teamInscriptions.CreateTeams();

                teams.ForEach(team => team.Id = (int)this.Db.Insert(team, true));

                var matches = MatchFactory.CreateLeagueMatches(teams, leagueId => Db.SingleById <League>(leagueId).LeagueMatchCreationMode).ToList();

                this.Db.InsertAll(matches);

                var leagueIds = matches.Select(s => s.LeagueId).Distinct().ToList();
                leagueIds.ForEach(leagueId => StandingsCalculator.Calculate(this.Db, leagueId));

                transaction.Commit();
            }
        }
Exemplo n.º 9
0
        public object Delete(DeleteTeamRequest request)
        {
            var team     = this.Db.SingleById <Team>(request.Id);
            var leagueId = team.LeagueId.GetValueOrDefault();

            team.Throw404NotFoundIfNull("Team existiert nicht!");

            using (var transaction = this.Db.BeginTransaction(IsolationLevel.RepeatableRead))
            {
                this.Db.Delete <TableEntry>(sql => sql.TeamId == team.Id);

                if (this.Db.Count <Match>(p => (p.HomeTeamId == team.Id || p.GuestTeamId == team.Id) && p.CupId != 0) > 0)
                {
                    // team already has cup matches -> we do not delete cup matches and therefore we
                    // have to keep the team, but remove league assignment and delete the league matches
                    team.LeagueId = null;
                    this.Db.Update(team);
                    this.Db.Delete <Match>(p => (p.HomeTeamId == team.Id || p.GuestTeamId == team.Id) && p.LeagueId > 0);

                    MatchFactory.ForfaitOpenCupMatchesForTeam(team.Id, this.Db);
                }
                else
                {
                    this.Db.Delete <Match>(sql => sql.GuestTeamId == team.Id || sql.HomeTeamId == team.Id);
                    this.Db.Delete <Team>(sql => sql.Id == team.Id);
                }


                if (leagueId != default(int))
                {
                    StandingsCalculator.Calculate(this.Db, leagueId);
                }

                transaction.Commit();
            }

            return(null);
        }
Exemplo n.º 10
0
        public MatchViewDto Put(UpdateMatchRequest request)
        {
            var matchToUpdate = Db.SingleById <Match>(request.Id);

            matchToUpdate.PopulateWith(request);

            if (matchToUpdate.HasResult && !matchToUpdate.ResultDate.HasValue)
            {
                matchToUpdate.ResultDate = DateTime.Now;
            }

            if (matchToUpdate.PlayDate.HasValue && !matchToUpdate.RegisterDate.HasValue)
            {
                matchToUpdate.ResultDate = matchToUpdate.PlayDate;
            }

            matchToUpdate.IsNotPlayedMatch = false;

            Db.Save(matchToUpdate);

            StandingsCalculator.Calculate(this.Db, matchToUpdate.LeagueId);

            return(Db.SingleById <MatchView>(request.Id).ConvertTo <MatchViewDto>());
        }