aboutsummaryrefslogtreecommitdiff
path: root/Main
diff options
context:
space:
mode:
authorAryadev Chavali <aryadevchavali1@gmail.com>2019-02-08 12:13:49 +0000
committerAryadev Chavali <aryadevchavali1@gmail.com>2019-02-08 12:13:49 +0000
commita93a0a33e8d548c7b016997cb57c3351c8c37702 (patch)
tree91edeb8c2b6b0000cb3fcb5065b7299c901442a3 /Main
parent5432a563e537f130d60ca0474edefbfd9a4a94a4 (diff)
downloadnewsaggregator-a93a0a33e8d548c7b016997cb57c3351c8c37702.tar.gz
newsaggregator-a93a0a33e8d548c7b016997cb57c3351c8c37702.tar.bz2
newsaggregator-a93a0a33e8d548c7b016997cb57c3351c8c37702.zip
Added try catch in case table is empty
Diffstat (limited to 'Main')
-rw-r--r--Main/Program.cs19
1 files changed, 17 insertions, 2 deletions
diff --git a/Main/Program.cs b/Main/Program.cs
index 0159873..808de98 100644
--- a/Main/Program.cs
+++ b/Main/Program.cs
@@ -20,14 +20,29 @@ namespace Main {
PublishDate = 4
}
};
- string[] xmls = new [] {
+ string[] xmls = new[] {
await Download.DownloadXML("http://feeds.bbci.co.uk/news/rss.xml")
};
List<Article> articles = ParserService.ParseMultiple(xmls, configs).ToList();
Console.WriteLine(articles.Count);
using (var db = new DatabaseContext()) {
- articles.ForEach(async article => await db.Articles.AddAsync(article));
+ int start;
+ try {
+ start = db.Articles.Last().ArticleID;
+ } catch (Exception) {
+ start = 0;
+ }
+ var insertReady = articles.Select((article, index) => new Article() {
+ ArticleID = index + start,
+ Source = article.Source,
+ Title = article.Title,
+ Description = article.Description,
+ ArticleLink = article.ArticleLink,
+ ImageLink = article.ImageLink,
+ PublishDate = article.PublishDate
+ }).ToList();
+ insertReady.ForEach(async article => await db.Articles.AddAsync(article));
int records = await db.SaveChangesAsync();
Console.WriteLine($"Wrote {records} records!");
}