public void SaveJobResult(AdJobResult jobResult) { _grabberContext.AdDownloadJobs.First(adj => adj.Id == jobResult.DownloadJob.Id).ProcessedAt = DateTime.Now; _grabberContext.SaveChanges(); Entry entry; try { entry = _znakerContext.Entries.First( e => e.IdOnSource == jobResult.DownloadJob.AdId && e.SourceId == jobResult.DownloadJob.SourceType); } catch (InvalidOperationException) { entry = new Entry { Text = jobResult.Text, SourceId = jobResult.DownloadJob.SourceType, CreatedOn = DateTime.Now, IdOnSource = jobResult.DownloadJob.AdId }; _znakerContext.Add(entry); } if (jobResult.Contacts == null || jobResult.Contacts.Count == 0) { _znakerContext.SaveChanges(); return; } var contacts = jobResult.Contacts.Select(k => { var contact = _znakerContext.Contacts.FirstOrDefault(c => c.ContactType == k.Key && c.Identity == k.Value); if (null != contact) { contact.UpdatedOn = DateTime.Now; return(contact); } contact = new Contact { ContactType = k.Key, Identity = k.Value, CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now }; _znakerContext.Contacts.Add(contact); return(contact); }).ToList(); var entryContacts = contacts.Select(c => new EntryContact { Contact = c, Entry = entry }).ToList(); _znakerContext.AddRange(entryContacts); _znakerContext.SaveChanges(); }
public AdJobResult Grab(AdDownloadJob job) { var downloadResult = new AdJobResult { DownloadJob = job }; var adResponse = _client.GetAsync(_config.GetAdvertDataUrl(job.AdId)).Result; if (adResponse.IsSuccessStatusCode) { downloadResult.Text = ExtractAdTextFromJsonString(adResponse.Content.ReadAsStringAsync().Result); } else { return(downloadResult); } var contactsResponse = _client.GetAsync(_config.GetAdvertContactUrl(job.AdId)).Result; if (contactsResponse.IsSuccessStatusCode) { downloadResult.Contacts = ExtractAdContactsFromJsonString(contactsResponse.Content.ReadAsStringAsync().Result); } return(downloadResult); }