Beispiel #1
0
        public async Task <ActionResult <MatchTeamDto> > CreateTeam([FromBody] MatchTeamOperationDto dto)
        {
            var match = await Context.Matches
                        .Where(o => o.Started == null)
                        .FirstOrDefaultAsync(o => o.Id == dto.MatchId);

            if (match == null)
            {
                return(BadRequest(new InvalidMatchException()));
            }

            var team = new MatchTeam()
            {
                MatchId = match.Id,
            };

            if (!string.IsNullOrEmpty(dto.Password))
            {
                team.SetPassword(dto.Password);
            }

            Context.Add(team);
            await Context.SaveChangesAsync();

            return(Ok(Mapper.Map <MatchTeamDto>(team)));
        }
Beispiel #2
0
        public async Task <ActionResult> LeaveTeam([FromBody] MatchTeamOperationDto dto)
        {
            var userId = User.Claims.GetUserId();

            if (userId == null)
            {
                return(Forbid());
            }

            var match = await Context.Matches
                        .Where(o => o.Started == null)
                        .FirstOrDefaultAsync(o => o.Id == dto.MatchId);

            if (match == null)
            {
                return(BadRequest(new InvalidMatchException()));
            }

            var registration = await Context.MatchRegistrations
                               .AsTracking()
                               .Include(o => o.Fighter)
                               .Where(o => o.Fighter.UserId == userId)
                               .Where(o => o.MatchId == match.Id)
                               .FirstOrDefaultAsync(o => o.FighterId == dto.FighterId);

            if (registration == null)
            {
                return(BadRequest(new InvalidMatchRegistrationException()));
            }

            if (registration.TeamId == dto.Id)
            {
                return(Ok());
            }

            registration.TeamId = null;
            await Context.SaveChangesAsync();

            return(Ok());
        }
Beispiel #3
0
        public async Task <ActionResult> JoinTeam([FromBody] MatchTeamOperationDto dto)
        {
            var userId = User.Claims.GetUserId();

            if (userId == null)
            {
                return(Forbid());
            }

            var match = await Context.Matches
                        .Where(o => o.Started == null)
                        .Include(o => o.Registrations)
                        .ThenInclude(o => o.Fighter)
                        .FirstOrDefaultAsync(o => o.Id == dto.MatchId);

            if (match == null)
            {
                return(BadRequest(new InvalidMatchException()));
            }

            if (match.Options.MaxFightersPerUser != null &&
                match.Registrations
                .Where(o => o.Fighter.UserId == userId && o.FighterId != dto.FighterId)
                .Count() >= match.Options.MaxFightersPerUser)
            {
                return(BadRequest(new MatchFighterLimitExceededException()));
            }

            var team = await Context.MatchTeams
                       .FirstOrDefaultAsync(o => o.Id == dto.Id);

            if (team == null)
            {
                return(BadRequest(new InvalidTeamException()));
            }

            if (!team.IsValidPassword(dto.Password))
            {
                return(BadRequest(new InvalidTeamPasswordException()));
            }

            var fighter = await Context.Fighters
                          .Where(o => o.UserId == userId)
                          .FirstOrDefaultAsync(o => o.Id == dto.FighterId);

            if (fighter == null)
            {
                return(BadRequest(new InvalidFighterException()));
            }

            var registration = await Context.MatchRegistrations
                               .AsTracking()
                               .Include(o => o.Fighter)
                               .Where(o => o.Fighter.UserId == userId)
                               .Where(o => o.MatchId == match.Id)
                               .FirstOrDefaultAsync(o => o.FighterId == dto.FighterId);

            if (registration == null)
            {
                registration = new MatchRegistration()
                {
                    MatchId   = match.Id,
                    FighterId = fighter.Id,
                    Date      = dateTimeProvider.Now,
                };
                Context.MatchRegistrations.Add(registration);
            }

            if (registration.TeamId == team.Id)
            {
                return(Ok());
            }

            registration.TeamId = team.Id;

            await Context.SaveChangesAsync();

            return(Ok());
        }