示例#1
0
        private PlayerAlt MapPlayerAlt(EfModels.PlayerAlt efAlt)
        {
            if (efAlt == null)
            {
                return(null);
            }

            return(new PlayerAlt()
            {
                Id = efAlt.Id,
                Player = this.mapper.Map <StoredPlayer>(efAlt.Player)
            });
        }
示例#2
0
        public async Task <EfModels.PlayerAlt> AddAltToMainAsync(int playerId, int mainId, int profileId)
        {
            var profileTask = this.context.GuildProfile
                              .Include(x => x.PlayerMains)
                              .Include(x => x.PlayerAlts)
                              .Include(x => x.Players)
                              .SingleOrDefaultAsync(x => x.Id == profileId);

            var mainTask = this.context.PlayerMains
                           .SingleOrDefaultAsync(x => x.Id == mainId);

            var playerTask = this.context.StoredPlayers
                             .SingleOrDefaultAsync(x => x.Id == playerId);

            await Task.WhenAll(profileTask, mainTask, playerTask);

            var profile = profileTask.Result;
            var player  = playerTask.Result;
            var main    = mainTask.Result;

            if (profile == null)
            {
                throw new UserReportableError($"Profile {profileId} not found.", (int)HttpStatusCode.BadRequest);
            }

            if (player == null)
            {
                throw new UserReportableError($"Player {playerId} not found.", (int)HttpStatusCode.BadRequest);
            }

            if (main == null)
            {
                throw new UserReportableError($"Main {mainId} not found.", (int)HttpStatusCode.BadRequest);
            }

            if (!profile.Players.Any(x => x.Id == playerId))
            {
                throw new UserReportableError($"Player {playerId} does not belong to this profile.", (int)HttpStatusCode.BadRequest);
            }

            if (profile.PlayerMains.Any(x => x.PlayerId == playerId) || profile.PlayerAlts.Any(x => x.PlayerId == playerId))
            {
                throw new UserReportableError($"Player {playerId} is already assigned!.", (int)HttpStatusCode.BadRequest);
            }

            var newAlt = new EfModels.PlayerAlt()
            {
                ProfileId    = profileId,
                PlayerId     = playerId,
                PlayerMainId = mainId
            };

            this.context.PlayerAlts.Add(newAlt);

            await this.context.SaveChangesAsync();

            await context.Entry(newAlt).Reference(x => x.Player).LoadAsync();

            await context.Entry(newAlt.Player).Reference(x => x.Realm).LoadAsync();

            await context.Entry(newAlt.Player.Realm).Reference(x => x.Region).LoadAsync();

            return(newAlt);
        }