Пример #1
0
        public async Task <IActionResult> PutTournament([FromRoute] int id, [FromBody] Tournament tournament)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tournament.Id)
            {
                return(BadRequest());
            }

            _context.Entry(tournament).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TournamentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Complete([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var round = await _context.Rounds
                        .Include(r => r.Matches)
                        .ThenInclude(m => m.NextWinnersMatch)
                        .Include(r => r.Matches)
                        .ThenInclude(m => m.NextLosersMatch)
                        .SingleOrDefaultAsync(r => r.Id == id);

            if (round == null)
            {
                return(BadRequest());
            }

            round.IsComplete = true;

            var rounds = new List <Round>();

            rounds.Add(round);

            UpdateMatches(round.Matches);

            if (round.RoundType == RoundType.GrandFinals)
            {
                var lastMatch = round.Matches.OrderBy(m => m.MatchNumber).FirstOrDefault();

                if (lastMatch.Winner == Slot.Two)
                {
                    var resetRound = new Round
                    {
                        RoundType    = RoundType.Reset,
                        Matches      = new List <Match>(),
                        RoundNumber  = round.RoundNumber + 1,
                        TournamentId = round.TournamentId,
                        Name         = "Grand Finals Reset"
                    };

                    var resetMatch = new Match {
                        MatchNumber     = lastMatch.MatchNumber + 1,
                        CompetitorOneId = lastMatch.CompetitorOneId,
                        CompetitorTwoId = lastMatch.CompetitorTwoId
                    };
                    lastMatch.NextWinnersMatch     = resetMatch;
                    lastMatch.NextWinnersMatchSlot = Slot.One;
                    lastMatch.NextLosersMatch      = resetMatch;
                    lastMatch.NextLosersMatchSlot  = Slot.Two;
                    _context.Entry(lastMatch).CurrentValues.SetValues(lastMatch);

                    resetRound.Matches.Add(resetMatch);

                    _context.Add(resetRound);
                    rounds.Add(resetRound);
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RoundExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(rounds));
        }