From c9009b700f93300140be9b25b937c2c99ca1d91e Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Mon, 5 Oct 2015 20:45:47 -0400 Subject: [PATCH] async everything --- .../Controllers/HomeController.cs | 6 ++-- .../Extensions/StringHtmlExtension.cs | 11 +++--- .../Model/{IFeedData.cs => IMetaData.cs} | 2 +- src/DotNetMashup.Web/Model/MetaData.cs | 2 +- .../BlogPostRepository.cs} | 35 +++++++++++-------- .../IRepository.cs} | 6 ++-- .../Repositories/TwitterRepository.cs | 26 ++++++++++++++ src/DotNetMashup.Web/Startup.cs | 4 +-- .../ViewModel/MashupViewModel.cs | 14 ++++++++ src/DotNetMashup.Web/project.json | 5 +-- 10 files changed, 80 insertions(+), 31 deletions(-) rename src/DotNetMashup.Web/Model/{IFeedData.cs => IMetaData.cs} (91%) rename src/DotNetMashup.Web/{Factories/BlogPostFactory.cs => Repositories/BlogPostRepository.cs} (77%) rename src/DotNetMashup.Web/{Factories/IFactory.cs => Repositories/IRepository.cs} (59%) create mode 100644 src/DotNetMashup.Web/Repositories/TwitterRepository.cs create mode 100644 src/DotNetMashup.Web/ViewModel/MashupViewModel.cs diff --git a/src/DotNetMashup.Web/Controllers/HomeController.cs b/src/DotNetMashup.Web/Controllers/HomeController.cs index f46a7b1..af5e77b 100644 --- a/src/DotNetMashup.Web/Controllers/HomeController.cs +++ b/src/DotNetMashup.Web/Controllers/HomeController.cs @@ -2,16 +2,16 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using DotNetMashup.Web.Factories; +using DotNetMashup.Web.Repositories; using Microsoft.AspNet.Mvc; namespace DotNetMashup.Web.Controllers { public class HomeController : Controller { - private readonly BlogPostFactory factory; + private readonly BlogPostRepository factory; - public HomeController(BlogPostFactory factory) + public HomeController(BlogPostRepository factory) { this.factory = factory; } diff --git a/src/DotNetMashup.Web/Extensions/StringHtmlExtension.cs b/src/DotNetMashup.Web/Extensions/StringHtmlExtension.cs index 599a58e..869e10a 100644 --- a/src/DotNetMashup.Web/Extensions/StringHtmlExtension.cs +++ b/src/DotNetMashup.Web/Extensions/StringHtmlExtension.cs @@ -12,7 +12,9 @@ namespace DotNetMashup.Web.Extensions /// Truncates a string containing HTML to a number of text characters, keeping whole words. /// The result contains HTML and any tags left open are closed. /// - /// + /// todo: describe html parameter on TruncateHtml + /// todo: describe maxCharacters parameter on TruncateHtml + /// todo: describe trailingText parameter on TruncateHtml /// public static string TruncateHtml(this string html, int maxCharacters, string trailingText) { @@ -90,7 +92,8 @@ namespace DotNetMashup.Web.Extensions /// Truncates a string containing HTML to a number of text characters, keeping whole words. /// The result contains HTML and any tags left open are closed. /// - /// + /// todo: describe html parameter on TruncateHtml + /// todo: describe maxCharacters parameter on TruncateHtml /// public static string TruncateHtml(this string html, int maxCharacters) { @@ -100,7 +103,7 @@ namespace DotNetMashup.Web.Extensions /// /// Strips all HTML tags from a string /// - /// + /// todo: describe html parameter on StripHtml /// public static string StripHtml(this string html) { @@ -115,7 +118,6 @@ namespace DotNetMashup.Web.Extensions /// /// /// - /// /// public static string Truncate(this string text, int maxCharacters) { @@ -142,7 +144,6 @@ namespace DotNetMashup.Web.Extensions /// /// /// - /// /// public static string TruncateWords(this string text, int maxCharacters) { diff --git a/src/DotNetMashup.Web/Model/IFeedData.cs b/src/DotNetMashup.Web/Model/IMetaData.cs similarity index 91% rename from src/DotNetMashup.Web/Model/IFeedData.cs rename to src/DotNetMashup.Web/Model/IMetaData.cs index 537f6fc..5899247 100644 --- a/src/DotNetMashup.Web/Model/IFeedData.cs +++ b/src/DotNetMashup.Web/Model/IMetaData.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; namespace DotNetMashup.Web.Model { - public interface IFeedData + public interface IMetaData { string FeedUrl { get; set; } string Author { get; set; } diff --git a/src/DotNetMashup.Web/Model/MetaData.cs b/src/DotNetMashup.Web/Model/MetaData.cs index 1cdbea0..b1cbe17 100644 --- a/src/DotNetMashup.Web/Model/MetaData.cs +++ b/src/DotNetMashup.Web/Model/MetaData.cs @@ -1,7 +1,7 @@ namespace DotNetMashup.Web.Model { //stolen idea from: https://github.com/NancyFx/Nancy.Blog/blob/master/src/Nancy.Blog/Model/MetaData.cs - public class MetaData : IFeedData + public class MetaData : IMetaData { public string FeedUrl { get; set; } public string Author { get; set; } diff --git a/src/DotNetMashup.Web/Factories/BlogPostFactory.cs b/src/DotNetMashup.Web/Repositories/BlogPostRepository.cs similarity index 77% rename from src/DotNetMashup.Web/Factories/BlogPostFactory.cs rename to src/DotNetMashup.Web/Repositories/BlogPostRepository.cs index 6eca9e9..4a9a4e9 100644 --- a/src/DotNetMashup.Web/Factories/BlogPostFactory.cs +++ b/src/DotNetMashup.Web/Repositories/BlogPostRepository.cs @@ -10,9 +10,9 @@ using DotNetMashup.Web.Global; using DotNetMashup.Web.Model; using Microsoft.Framework.Caching.Memory; -namespace DotNetMashup.Web.Factories +namespace DotNetMashup.Web.Repositories { - public class BlogPostFactory : IFactory + public class BlogPostRepository : IRepository { private readonly ISiteSetting setting; @@ -20,7 +20,7 @@ namespace DotNetMashup.Web.Factories private readonly IEnumerable _data; private const string cacheKey = "blogposts"; - public BlogPostFactory(IEnumerable data, IMemoryCache cache, ISiteSetting setting) + public BlogPostRepository(IEnumerable data, IMemoryCache cache, ISiteSetting setting) { this._data = data; this.cache = cache; @@ -35,11 +35,11 @@ namespace DotNetMashup.Web.Factories } } - public IEnumerable GetData() + public async Task> GetData() { var cachedata = cache.Get>(cacheKey); if(cachedata != null) return cachedata; - var syndicationFeeds = GetSyndicationFeeds(_data); + var syndicationFeeds = await GetSyndicationFeeds(_data); var data = syndicationFeeds .SelectMany(pair => pair.Value.Items, (pair, item) => new { Id = pair.Key, Item = item }) @@ -105,35 +105,40 @@ namespace DotNetMashup.Web.Factories return data; } - private static IEnumerable> GetSyndicationFeeds(IEnumerable metadataEntries) + private async static Task>> GetSyndicationFeeds(IEnumerable metadataEntries) { var syndicationFeeds = new List>(); foreach(var metadata in metadataEntries) { - GetFeed(metadata.FeedUrl, metadata.Id, syndicationFeeds); + syndicationFeeds.AddRange(await GetFeed(metadata.FeedUrl, metadata.Id, syndicationFeeds)); } return syndicationFeeds; } - private static void GetFeed(string url, string id, List> syndicationFeeds) + private async static Task>> GetFeed(string url, string id, List> syndicationFeeds) { + var feeds = new List>(); try { SyndicationFeed feed = null; - using(var reader = XmlReader.Create(url)) - { - feed = SyndicationFeed.Load(reader); - } + await Task.Run(() => { + using(var reader = XmlReader.Create(url)) + { + feed = SyndicationFeed.Load(reader); + } + }); + + if(feed != null) { - syndicationFeeds.Add(new KeyValuePair(id, feed)); + feeds.Add(new KeyValuePair(id, feed)); if(feed.Links.Any(x => x.RelationshipType == "next")) { foreach(var pagingLink in feed.Links.Where(x => x.RelationshipType == "next")) { - GetFeed(pagingLink.Uri.AbsoluteUri, id, syndicationFeeds); + feeds.AddRange(await GetFeed(pagingLink.Uri.AbsoluteUri, id, syndicationFeeds)); } } } @@ -146,6 +151,8 @@ namespace DotNetMashup.Web.Factories { //Unable to load RSS feed } + return feeds; + } } } \ No newline at end of file diff --git a/src/DotNetMashup.Web/Factories/IFactory.cs b/src/DotNetMashup.Web/Repositories/IRepository.cs similarity index 59% rename from src/DotNetMashup.Web/Factories/IFactory.cs rename to src/DotNetMashup.Web/Repositories/IRepository.cs index e3baa36..b23b847 100644 --- a/src/DotNetMashup.Web/Factories/IFactory.cs +++ b/src/DotNetMashup.Web/Repositories/IRepository.cs @@ -4,12 +4,12 @@ using System.Linq; using System.Threading.Tasks; using DotNetMashup.Web.Model; -namespace DotNetMashup.Web.Factories +namespace DotNetMashup.Web.Repositories { - public interface IFactory + public interface IRepository { string FactoryName { get; } - IEnumerable GetData(); + Task> GetData(); } } \ No newline at end of file diff --git a/src/DotNetMashup.Web/Repositories/TwitterRepository.cs b/src/DotNetMashup.Web/Repositories/TwitterRepository.cs new file mode 100644 index 0000000..2d7bb34 --- /dev/null +++ b/src/DotNetMashup.Web/Repositories/TwitterRepository.cs @@ -0,0 +1,26 @@ +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Threading.Tasks; +//using DotNetMashup.Web.Model; +//using Tweetinvi; + +//namespace DotNetMashup.Web.Repositories +//{ +// public class TwitterRepository : IRepository +// { +// public string FactoryName +// { +// get +// { +// return "Twitter"; +// } +// } + +// public Task> GetData() +// { +// throw new NotImplementedException(); +// var matchingTweets = Search.SearchTweets(); +// } +// } +//} diff --git a/src/DotNetMashup.Web/Startup.cs b/src/DotNetMashup.Web/Startup.cs index e2941f0..cfc80c0 100644 --- a/src/DotNetMashup.Web/Startup.cs +++ b/src/DotNetMashup.Web/Startup.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.IO; -using DotNetMashup.Web.Factories; +using DotNetMashup.Web.Repositories; using DotNetMashup.Web.Global; using DotNetMashup.Web.Model; using Microsoft.AspNet.Builder; @@ -42,7 +42,7 @@ namespace DotNetMashup.Web return new MemoryCache(new MemoryCacheOptions()); }); services.AddInstance(_feedData); - services.AddSingleton(); + services.AddSingleton(); // Add MVC services to the services container. services.AddMvc(); diff --git a/src/DotNetMashup.Web/ViewModel/MashupViewModel.cs b/src/DotNetMashup.Web/ViewModel/MashupViewModel.cs new file mode 100644 index 0000000..22b4d33 --- /dev/null +++ b/src/DotNetMashup.Web/ViewModel/MashupViewModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DotNetMashup.Web.Model; + +namespace DotNetMashup.Web.ViewModel +{ + public class MashupViewModel + { + public string Header { get; set; } + public IEnumerable posts { get; set; } + } +} diff --git a/src/DotNetMashup.Web/project.json b/src/DotNetMashup.Web/project.json index eadeae2..4c7cd2a 100644 --- a/src/DotNetMashup.Web/project.json +++ b/src/DotNetMashup.Web/project.json @@ -15,7 +15,8 @@ "Microsoft.Framework.Logging.Console": "1.0.0-beta7", "Microsoft.Framework.Logging.Debug": "1.0.0-beta7", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta7", - "Newtonsoft.Json": "7.0.1" + "Newtonsoft.Json": "7.0.1", + "TweetinviAPI": "0.9.9.7" }, "commands": { @@ -23,7 +24,7 @@ }, "frameworks": { - "dnx451": { + "dnx46": { "frameworkAssemblies": { "System.ServiceModel": "4.0.0.0" }