public async Task <ActionResult> PostAnnouncementAsync([FromBody] AnnouncementRecord record) { record.Touch(); record.NewId(); await _announcementService.InsertOneAsync(record); await _eventMediator.PushSyncRequestAsync(); await _eventMediator.PushAnnouncementNotificationAsync(record); return(Ok()); }
public async Task ExecuteAsync() { _logger.LogDebug("Job started"); var response = string.Empty; using (var client = new HttpClient()) { var url = _configuration["source:url"]; _logger.LogDebug("Fetching data from {url}", url); response = await client.GetStringAsync(url); } if (response == "null") { _logger.LogDebug("Received null response"); return; } var records = JsonConvert.DeserializeObject <JObject[]>(response); var unixReference = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var mapping = records.Select(j => new { Record = new AnnouncementRecord() { ExternalReference = j["id"].Value <string>(), Area = j["news"]["type"].Value <string>().UppercaseFirst(), Author = j["news"]?["department"]?.Value <string>().UppercaseFirst() ?? "Eurofurence", Title = j["news"]["title"].Value <string>(), Content = j["news"]["message"].Value <string>().RemoveMarkdown(), ValidFromDateTimeUtc = unixReference.AddSeconds(j["date"].Value <double>()).ToUniversalTime(), ValidUntilDateTimeUtc = unixReference .AddSeconds(j["news"]["valid_until"].Value <double>()).ToUniversalTime(), }, Type = j["news"]["type"].Value <string>() }).ToList(); foreach (var item in mapping) { if (new[] { "new", "reschedule" }.Contains(item.Type)) { item.Record.ValidUntilDateTimeUtc = item.Record.ValidFromDateTimeUtc.AddHours(48); } } var existingRecords = await _announcementService.FindAllAsync(); var patch = new PatchDefinition <AnnouncementRecord, AnnouncementRecord>((source, list) => list.SingleOrDefault(a => a.ExternalReference == source.ExternalReference)); patch .Map(s => s.ExternalReference, t => t.ExternalReference) .Map(s => s.Area, t => t.Area) .Map(s => s.Author, t => t.Author) .Map(s => s.Title, t => t.Title) .Map(s => s.Content, t => t.Content) .Map(s => s.ValidUntilDateTimeUtc, t => t.ValidUntilDateTimeUtc) .Map(s => s.ValidFromDateTimeUtc, t => t.ValidFromDateTimeUtc); var diff = patch.Patch(mapping.Select(a => a.Record), existingRecords) .Where(a => !string.IsNullOrEmpty(a.Entity.ExternalReference) && a.Action != ActionEnum.NotModified) .ToList(); _logger.LogDebug("Diff results in {count} new/modified records", diff.Count); if (diff.Count == 0) { return; } _logger.LogInformation("Processing {count} new/modified records", diff.Count); await _announcementService.ApplyPatchOperationAsync(diff); await _pushEventMediator.PushSyncRequestAsync(); foreach (var record in diff.Where(a => a.Action == ActionEnum.Add)) { _logger.LogInformation("Sending push notification for announcement {id} ({title})", record.Entity.Id, record.Entity.Title); await _pushEventMediator.PushAnnouncementNotificationAsync(record.Entity); } _logger.LogDebug("Job finished"); }