Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void SaveLiveScoreData(LiveScoresData data)
- {
- var leaguesInDb = DbContext.SportLeagues.ToList();
- var dataByLeaguesInDb = data.Categories.Where(x => leaguesInDb.Select(s => s.Name).Contains(x.Name));
- foreach (var league in dataByLeaguesInDb)
- {
- var leagueEntity = leaguesInDb.FirstOrDefault(x => x.Name.Equals(league.Name));
- foreach (var match in league.Matches)
- {
- var localTeamEntity = DbContext.Teams.FirstOrDefault(x => x.ApiId == match.LocalTeam.Id);
- if (localTeamEntity == null)
- {
- var teamEntity = new Teams
- {
- ApiId = match.LocalTeam.Id,
- Name = match.LocalTeam.Name
- };
- DbContext.Teams.Add(teamEntity);
- DbContext.SaveChanges();
- localTeamEntity = teamEntity;
- }
- var visitorTeamEntity = DbContext.Teams.FirstOrDefault(x => x.ApiId == match.VisitorTeam.Id);
- if (visitorTeamEntity == null)
- {
- var teamEntity = new Teams
- {
- ApiId = match.VisitorTeam.Id,
- Name = match.VisitorTeam.Name
- };
- DbContext.Teams.Add(teamEntity);
- DbContext.SaveChanges();
- visitorTeamEntity = teamEntity;
- }
- var matchEntity = DbContext.Matches.FirstOrDefault(x => x.ApiId == match.Id);
- if (matchEntity == null)
- {
- DateTime.TryParseExact
- ($"{match.FormattedDate} {match.Time}",
- "dd.MM.yyyy HH:mm",
- CultureInfo.InvariantCulture,
- DateTimeStyles.None,
- out var matchDate
- );
- var matchToAdd = new Matches()
- {
- ApiId = match.Id,
- Date = matchDate,
- Status = match.Status,
- Timer = match.Timer,
- FixId = match.FixId,
- StaticId = match.StaticId,
- LeagueId = leagueEntity.Id,
- LocalTeamId = localTeamEntity.Id,
- VisitorTeamId = visitorTeamEntity.Id
- };
- if (char.IsDigit(Convert.ToChar(match.LocalTeam.Goals)))
- {
- matchToAdd.LocalTeamGoals = Convert.ToInt32(match.LocalTeam.Goals);
- }
- if (char.IsDigit(Convert.ToChar(match.VisitorTeam.Goals)))
- {
- matchToAdd.VisitorTeamGoals = Convert.ToInt32(match.VisitorTeam.Goals);
- }
- DbContext.Matches.Add(matchToAdd);
- DbContext.SaveChanges();
- matchEntity = matchToAdd;
- }
- else
- {
- if (!matchEntity.Status.Equals(match.Status))
- {
- matchEntity.Status = match.Status;
- }
- }
- foreach (var matchEvent in match.Events)
- {
- var eventEntity = new Events()
- {
- ApiEventId = matchEvent.EventId,
- MatchId = matchEntity.Id,
- Type = matchEvent.Type,
- Minute = matchEvent.Minute,
- TeamId = matchEvent.Team.Equals(_localteam) ? localTeamEntity.Id : visitorTeamEntity.Id,
- PlayerName = matchEvent.PlayerName,
- AssistPlayerName = matchEvent.AssistPlayerName
- };
- DbContext.Events.Add(eventEntity);
- }
- DbContext.SaveChanges();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement