public async Task <int> Handle(AddPlayerCommand request, CancellationToken cancellationToken) { request = request ?? throw new ArgumentNullException(nameof(request)); var desiredTeam = await this.teamsRepository .All() .Include(t => t.PlayerTeams) .SingleOrDefaultAsync(t => t.Id == request.TeamId, cancellationToken) ?? throw new NotFoundException(nameof(Team), request.TeamId); if (await CommonCheckHelper.CheckIfTeamIsFull(desiredTeam.Id, this.teamsRepository)) { throw new TeamIsFullException(desiredTeam.Name); } if (this.CheckIfPlayerParticipatesInATeamWithTheSameFormat(desiredTeam.TournamentFormatId, request.PlayerId)) { throw new PlayerCannotBeAMemeberOfMultipleTeamsWithTheSameFormatException(); } desiredTeam.PlayerTeams.Add(new PlayerTeam { PlayerId = request.PlayerId }); return(await this.teamsRepository.SaveChangesAsync(cancellationToken)); }
public async Task <int> Handle(InvitePlayerCommand request, CancellationToken cancellationToken) { request = request ?? throw new ArgumentNullException(nameof(request)); if (string.IsNullOrWhiteSpace(request.UserName)) { throw new InvalidSearchQueryException(); } var desiredPlayer = await this.playersRepository .AllAsNoTracking() .Include(p => p.PlayerTeams) .ThenInclude(pt => pt.Team) .Include(p => p.Claims) .SingleOrDefaultAsync(p => p.UserName == request.UserName, cancellationToken) ?? throw new PlayerDoesNotExistException(request.UserName); if (!await CommonCheckHelper.CheckIfPlayerHasLinkedSteamAccount(desiredPlayer.Id, this.playersRepository)) { throw new PlayerDoesNotHaveALinkedSteamAccountException(desiredPlayer.UserName); } if (desiredPlayer.Claims.Any(c => c.ClaimType == IS_VAC_BANNED_CLAIM_TYPE)) { throw new PlayerIsVacBannedException(desiredPlayer.UserName); } var desiredTeam = await this.teamsRepository .AllAsNoTracking() .Include(t => t.TournamentFormat) .SingleOrDefaultAsync(t => t.Id == request.TeamId, cancellationToken) ?? throw new NotFoundException(nameof(Team), request.TeamId); if (this.CheckIfUserIsPartOfATeamWithTheSameFormat(desiredPlayer, desiredTeam.TournamentFormatId)) { throw new PlayerCannotBeAMemeberOfMultipleTeamsWithTheSameFormatException(request.UserName); } if (await CommonCheckHelper.CheckIfTeamIsFull(desiredTeam.Id, this.teamsRepository)) { throw new TeamIsFullException(desiredTeam.Name); } if (await this.CheckIfUserAlreadyHasAnInviteForGivenTeam(request)) { throw new PlayerAlreadyHasPendingInvite(request.UserName); } var invite = new TeamInvite { PlayerId = desiredPlayer.Id, TeamId = desiredTeam.Id, TeamName = desiredTeam.Name, SenderUsername = this.userAccessor.Username }; await this.teamInvitesRepository.AddAsync(invite); var rowsAffected = await this.teamInvitesRepository.SaveChangesAsync(cancellationToken); await this.mediator.Publish(new PlayerInvitedNotification { SenderName = invite.SenderUsername, ReceiverId = desiredPlayer.Id, TeamName = desiredTeam.Name }); return(rowsAffected); }