private async Task AddArticlesAndSourcesToDb(RootNewsServiceModel root) { if (root != null) { foreach (var article in root.Articles) { if (!this.db.Sources.Any(x => x.Id == article.Source.Id)) { db.Sources.Add(new Source { Id = article.Source.Id, Name = article.Source.Name, }); await db.SaveChangesAsync(); } db.Articles.Add(new Article { Author = article.Author, Title = article.Title, Description = article.Description, Url = article.Url, UrlToImage = article.UrlToImage, PublishedAt = article.PublishedAt, Content = article.Content, SourceId = article.Source.Id, }); await db.SaveChangesAsync(); } } }
public async Task UpdateNewsLocalDb() { await RemoveCurrentArticlesAndSources(); RootNewsServiceModel root = GetNewsRoot(); await AddArticlesAndSourcesToDb(root); }
private RootNewsServiceModel GetNewsRoot() { var responseString = GatherInfo(NewsApi.Url).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(responseString)) { RootNewsServiceModel rootNewsApi = JsonConvert.DeserializeObject <RootNewsServiceModel>(responseString); return(rootNewsApi); } return(null); }