private async Task FetchNewMatchesAsync(MatchesDbContext dbContext) { DateTime fetchSince; if (!await dbContext.Matches.AnyAsync()) { fetchSince = _matchesConfiguration.FetchSince; } else { fetchSince = await dbContext.Matches.MaxAsync(m => m.StartDateTime) + TimeSpan.FromDays(1); } var fetchTo = DateTime.Now + TimeSpan.FromDays(_matchesConfiguration.FetchAheadInDays); _logger.LogInformation($"Fetching new matches between {fetchSince:yyyy-MM-dd} and {fetchTo:yyyy-MM-dd}".AddTimestamp()); var matches = (await _apiClient.FetchMatchesBetweenAsync(fetchSince, fetchTo)).ToList(); foreach (var match in matches) { _logger.LogInformation($"New match:\n {JsonConvert.SerializeObject(match)}".AddTimestamp()); } dbContext.Matches.AddRange(matches); foreach (var match in matches) { _matchEventProducer.PublishNewMatch(match); } await dbContext.SaveChangesAsync(); }
private async Task JobAsync() { _logger.LogInformation("Executing job.".AddTimestamp()); using (var dbContext = new MatchesDbContext(_matchesConfiguration.ConnectionString)) { await FetchNewMatchesAsync(dbContext); DetachAll(dbContext); await UpdateMatchesAsync(dbContext); } }
private async Task UpdateMatchesAsync(MatchesDbContext dbContext) { if (await dbContext.Matches.AllAsync(Match.IsFinishedExpression)) { return; } var updateSince = await dbContext.Matches .Where(Match.IsNotFinishedExpression) .MinAsync(m => m.StartDateTime); var updateTo = DateTime.Now; if (updateTo < updateSince) { _logger.LogInformation("Skipping update.".AddTimestamp()); return; } _logger.LogInformation($"Fetching updates between {updateSince:yyyy-MM-dd} and {updateTo:yyyy-MM-dd}".AddTimestamp()); var matches = (await _apiClient.FetchMatchesBetweenAsync(updateSince, updateTo)).ToList(); foreach (var match in matches) { _logger.LogInformation($"Updated match:\n {JsonConvert.SerializeObject(match)}".AddTimestamp()); } dbContext.UpdateRange(matches); var matchIds = matches.Select(m => m.Id); var matchEntities = await dbContext.Matches.Where(m => matchIds.Contains(m.Id)).ToDictionaryAsync(m => m.Id, m => m); foreach (var match in matches) { var matchEntity = matchEntities[match.Id]; if (match.BlueScore != matchEntity.BlueScore || match.RedScore != matchEntity.RedScore) { _matchEventProducer.PublishMatchUpdate(match, matchEntity); } } await dbContext.SaveChangesAsync(); }
public MatchService() { _dbContext = new MatchesDbContext(); _converter = new MatchConverter(); }
public LeagueService() { _dbContext = new MatchesDbContext(); _converter = new LeagueConverter(); }
public MatchController(MatchesDbContext matchesDbContext, ILogger <MatchController> logger) { _matchesDbContext = matchesDbContext; _logger = logger; }
public TeamController(MatchesDbContext matchesDbContext) { _matchesDbContext = matchesDbContext; }
public TeamService() { _dbContext = new MatchesDbContext(); _converter = new TeamConverter(); }