public void UpdateCompetitors(int competitionId, List <CompetitorCreationInfoDTO> competitors) { var existingCompetitors = DbContext.Competitors.Where(x => x.IdCompetition == competitionId).ToList(); var newCompetitors = new List <CompetitorCreationInfoDTO>(); var competitorsToRemove = new List <Competitor>(); foreach (var existingCompetitor in existingCompetitors) { if (!competitors.Any(x => x.Id == existingCompetitor.Id)) { competitorsToRemove.Add(existingCompetitor); } else { var competitorInfo = new CompetitionInfo(competitors.FirstOrDefault(x => x.Id == existingCompetitor.Id)); existingCompetitor.CompetitionInfo = competitorInfo.SerializeObject(); competitors.RemoveAt(competitors.FindIndex(x => x.Id == existingCompetitor.Id)); } } DbContext.Competitors.RemoveRange(competitorsToRemove); InsertNewCompetitors(DbContext.Competitions.First(x => x.Id == competitionId), competitors); }
public Dictionary <int, Competitor> InsertNewCompetitors(Competition competition, List <CompetitorCreationInfoDTO> competitors) { Dictionary <int, Competitor> newCompetitors = new Dictionary <int, Competitor>(); foreach (var competitor in competitors) { var player = new Player(); player.DisplayName = competitor.Name; DbContext.Players.Add(player); var competitorInfo = new CompetitionInfo(competitor); var dbCompetitor = new Competitor() { Player = player, Competition = competition, CompetitionInfo = competitorInfo.SerializeObject() }; DbContext.Competitors.Add(dbCompetitor); newCompetitors.Add(competitor.Id, dbCompetitor); } return(newCompetitors); }