public async Task <Event> Post(Event @event) { _eventShareDbContext.Events.Add(@event); await _eventShareDbContext.SaveChangesAsync().ConfigureAwait(false); return(@event); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); try { var events = await _eventPoller.DoPollAsync(stoppingToken).ConfigureAwait(false); var existingEventTitles = await _eventShareDbContext.Events .Where(e => e.DateAndTime >= DateTime.Now) .Select(e => e.Title) .ToListAsync(stoppingToken) .ConfigureAwait(false); var newEvents = events .Where(e => !existingEventTitles.Any(t => t.Equals(e.Title, StringComparison.InvariantCultureIgnoreCase))) .ToList(); if (newEvents.Any()) { await _eventShareDbContext.Events.AddRangeAsync(newEvents, stoppingToken).ConfigureAwait(false); await _eventShareDbContext.SaveChangesAsync(stoppingToken).ConfigureAwait(false); } } catch (Exception ex) { _logger.LogError($"Polling round failed with {ex}"); } await Task.Delay(_workerPeriod, stoppingToken); } }
public async Task <IEnumerable <Event> > DoPollAsync(CancellationToken cancellationToken) { if (_currentPoll != PollResults.Count) { return(PollResults[_currentPoll++]); } _currentPoll = 0; var eventsToRemove = _eventShareDbContext.Events .Where(e => e.Title == "Fake Event #1" || e.Title == "Fake Event #2" || e.Title == "Fake Event #3" || e.Title == "Fake Event #4"); await eventsToRemove .ForEachAsync(e => { var entity = _eventShareDbContext.Find <Event>(e.Id); _eventShareDbContext.Entry(entity).State = EntityState.Deleted; }, cancellationToken) .ConfigureAwait(false); await _eventShareDbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(Enumerable.Empty <Event>()); }