public static TeamResult Failed(Exception e)
        {
            var result = new TeamResult {
                Succeeded = false
            };

            result.Errors = new TeamError[] { new TeamError(e) };

            return(result);
        }
Esempio n. 2
0
        public async Task <TeamResult> CreateTeamAsync(ClaimsPrincipal signedInUser, DataTransferObjects.Team team)
        {
            // Validate team
            if (team == null)
            {
                throw new ArgumentNullException(nameof(team));
            }
            if (team.Roster == null)
            {
                throw new ArgumentNullException(nameof(team.Roster));
            }
            if (team.Roster.Count() < 5)
            {
                throw new InvalidOperationException("Teams must have at least 5 members.");
            }
            if (team.Owner == null)
            {
                throw new ArgumentNullException(nameof(team.Owner));
            }
            if (string.IsNullOrEmpty(team.Owner.Username))
            {
                throw new InvalidOperationException("Team owner invalid username");
            }
            if (string.IsNullOrEmpty(team.Name))
            {
                throw new ArgumentNullException(nameof(team.Name));
            }
            if (!Team.TeamNameRegex.IsMatch(team.Name))
            {
                throw new InvalidOperationException($"Team name {team.Name} is invalid");
            }

            // Make sure we have a default season
            var season = await _seasonManager.GetCurrentSeasonAsync();

            if (season == null)
            {
                throw new InvalidProgramException("Current season not found");
            }

            var entity = new Team {
                Name = team.Name, NormalizedName = team.Name.ToLower().Replace(' ', '-')
            };

            // Create team roster
            var roster = new List <TeamInvite>();

            foreach (var invitee in team.Roster)
            {
                // Validate invitee
                if (string.IsNullOrEmpty(invitee.Username) && string.IsNullOrEmpty(invitee.Tag))
                {
                    throw new InvalidOperationException("Team roster invalid");
                }

                if (invitee.Username != team.Owner.Username)
                {
                    Player player = null;

                    if (!string.IsNullOrEmpty(invitee.Username))
                    {
                        // If the player has a username, load the user
                        player = _db.Players
                                 .Include(x => x.User)
                                 .SingleOrDefault(x => x.User.UserName == invitee.Username);
                        if (player == null)
                        {
                            throw new InvalidProgramException($"Player entity for user {invitee.Username} not found.");
                        }
                    }
                    else
                    {
                        player = await _db.Players.FirstOrDefaultAsync(x => x.Tag == invitee.Tag);
                    }

                    if (player == null)
                    {
                        throw new InvalidProgramException($"Unable to find player");
                    }

                    var invite = new TeamInvite {
                        Player = player, Team = entity
                    };
                    roster.Add(invite);
                }
            }

            // Create team owner
            var ownerPlayer = _db.Players
                              .Include(x => x.User)
                              .SingleOrDefault(x => x.User.UserName == team.Owner.Username);

            if (ownerPlayer == null)
            {
                throw new InvalidProgramException($"Player entity for owner {team.Owner.Username} not found.");
            }
            if (ownerPlayer.User.Id != signedInUser.GetUserId())
            {
                throw new InvalidOperationException($"Team owner must be currently signed in user.");
            }

            var teamOwner = new TeamOwner {
                Player = ownerPlayer, Team = entity
            };

            // Begin transaction as this is where we begin save operations
            var transaction = await _db.Database.BeginTransactionAsync();

            var result = await _userManager.AddClaimAsync(ownerPlayer.User, new Claim($"{AuthorizationDefaults.ClaimTypeTeamOwner}:{entity.NormalizedName}", ownerPlayer.User.UserName));

            EnsureIdentitySucceeded(result);

            // Refresh the signin to add the owner cookie
            await _signInManager.RefreshSignInAsync(ownerPlayer.User);

            entity.Invitees = roster;
            entity.Owner    = teamOwner;

            _db.Add(teamOwner);
            _db.Add(entity);
            foreach (var invitee in roster)
            {
                _db.Add(invitee);
            }

            try
            {
                await _db.SaveChangesAsync();

                // Setup rank & image
                await _rankManager.CreateNewTeamRankingAsync(entity, season);

                await _imageManager.CreateDefaultImageForTeamAsync(entity);
            }
            catch (Exception e)
            {
                transaction.Rollback();

                return(TeamResult.Failed(e));
            }

            transaction.Commit();

            return(TeamResult.Success(entity));
        }