public async Task <EfModels.PlayerMain> AddMainToProfileAsync(int playerId, 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 playerTask = this.context.StoredPlayers .SingleOrDefaultAsync(x => x.Id == playerId); await Task.WhenAll(profileTask, playerTask); var profile = profileTask.Result; var player = playerTask.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 (!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 newPlayer = new EfModels.PlayerMain() { ProfileId = profileId, PlayerId = playerId }; profile.PlayerMains.Add(newPlayer); await this.context.SaveChangesAsync(); await context.Entry(newPlayer).Reference(x => x.Player).LoadAsync(); await context.Entry(newPlayer.Player).Reference(x => x.Realm).LoadAsync(); await context.Entry(newPlayer.Player.Realm).Reference(x => x.Region).LoadAsync(); return(newPlayer); }
private PlayerMain MapPlayerMain(EfModels.PlayerMain efMain, bool isOfficer) { if (efMain == null) { return(null); } return(new PlayerMain() { Id = efMain.Id, Notes = efMain.Notes, OfficerNotes = isOfficer ? efMain.OfficerNotes : string.Empty, Player = this.mapper.Map <StoredPlayer>(efMain.Player), Alts = efMain.Alts.Select(x => MapPlayerAlt(x)) }); }