public async Task <bool> UpdateAlternateAccountsAsync(Guid summonerId, IEnumerable <AlternateAccountView> viewList) { var newList = viewList.ToList(); var existingAlternateAccounts = (await _alternateAccountRepository.ReadAllForSummonerAsync(summonerId)).ToList(); var kvp = new Dictionary <AlternateAccountView, AlternateAccountEntity>(); foreach (var entity in existingAlternateAccounts) { var mapped = _alternateAccountMapper.Map(entity); kvp.Add(mapped, entity); } var createList = new List <AlternateAccountEntity>(); var updateList = new List <AlternateAccountEntity>(); var deleteList = new List <AlternateAccountEntity>(); foreach (var newAltAccount in newList) { //create if (!kvp.TryGetValue(newAltAccount, out var oldEntity)) { var newEntity = _alternateAccountMapper.Map(newAltAccount); newEntity.Id = Guid.NewGuid(); newEntity.SummonerId = summonerId; createList.Add(newEntity); } //update else { if (oldEntity.AlternateName != newAltAccount.AlternateName || oldEntity.OpGGUrlLink != newAltAccount.OpGgUrlLink) { oldEntity.AlternateName = newAltAccount.AlternateName; oldEntity.OpGGUrlLink = newAltAccount.OpGgUrlLink; updateList.Add(oldEntity); } } } //delete foreach (var oldAltAccount in kvp) { if (!newList.Contains(oldAltAccount.Key)) { deleteList.Add(oldAltAccount.Value); } } var createTask = _alternateAccountRepository.CreateAsync(createList); var updateTask = _alternateAccountRepository.UpdateAsync(updateList); var deleteTask = _alternateAccountRepository.DeleteAsync(deleteList); return(await createTask && await updateTask && await deleteTask); }
public async Task <PlayerProfileView> GetPlayerProfileAsync(Guid userId) { var seasonsTask = _seasonInfoRepository.GetAllSeasonsAsync(); var summonerTask = _summonerInfoRepository.ReadOneByUserIdAsync(userId); var achievementsTask = _achievementRepository.GetAchievementsForUserAsync(userId); var summoner = await summonerTask; if (summoner == null) { var message = $"User {userId} was not found."; _logger.LogError(message); throw new SummonerInfoException($"User {userId} was not found."); } var achievements = await achievementsTask; var alternateAccountsTask = _alternateAccountRepository.ReadAllForSummonerAsync(summoner.Id); var teamPlayer = (await _teamPlayerRepository.GetAllRostersForPlayerAsync(summoner.Id)).ToList(); var seasons = (await seasonsTask).OrderBy(x => x.SeasonStartDate).ToList(); var alternateAccounts = await alternateAccountsTask; var altAccountsMapped = _alternateAccountMapper.Map(alternateAccounts); var achievementViews = new List <AchievementView>(); foreach (var achievement in achievements) { var achievementView = new AchievementView(achievement); var seasonAchieved = seasons.FirstOrDefault(x => x.SeasonStartDate <achievement.AchievedDate && (x.SeasonEndDate ?? DateTime.MaxValue)> achievement.AchievedDate); achievementView.Season = seasonAchieved != null ? seasonAchieved.SeasonName : "PreSeason"; achievementViews.Add(achievementView); } var playerStatsDictionary = new Dictionary <int, PlayerStatsView>(); var matchDetails = await _matchDetailRepository.GetMatchDetailsForPlayerAsync(new List <Guid> { summoner.Id }); var statIds = matchDetails.Values.SelectMany(x => x.Select(y => y.PlayerStatsId)); var allPlayerStats = await _playerStatsRepository.GetStatsAsync(statIds); var teamname = "None"; if (teamPlayer.Any()) { var latestSeason = seasons.Last(); var team = await _teamRosterRepository.GetTeamAsync(latestSeason.Id, teamPlayer.Select(x => x.TeamRosterId)); if (team != null) { teamname = team.TeamName; } } foreach (var seasonKvp in allPlayerStats) { var season = seasons.First(x => x.Id == seasonKvp.Key.SeasonId); var seasonStats = allPlayerStats.SelectMany(x => x.Value.Where(y => y.SeasonInfoId == season.Id)); var mappedStats = _playerStatsMapper.MapForSeason(seasonStats, 0); if (int.TryParse(season.SeasonName.Split(" ").Last(), out var seasonNum)) { playerStatsDictionary.Add(seasonNum, mappedStats); } } var view = new PlayerProfileView { PlayerName = summoner.SummonerName, Rank = _tierDivisionMapper.Map(summoner.Tier_DivisionId), TeamName = string.IsNullOrEmpty(teamname) ? "None" : teamname, Achievements = achievementViews, PlayerStats = playerStatsDictionary, AlternateAccountViews = altAccountsMapped }; if (summoner.PreviousSeasonRankId != null) { view.PreviousSeasonRank = _tierDivisionMapper.Map(summoner.PreviousSeasonRankId.Value); } return(view); }