add github provider

This commit is contained in:
Tommy Parnell
2015-10-07 19:19:23 -04:00
parent c9009b700f
commit b348bc56ad
13 changed files with 151 additions and 29 deletions

View File

@@ -9,16 +9,16 @@ namespace DotNetMashup.Web.Controllers
{
public class HomeController : Controller
{
private readonly BlogPostRepository factory;
private readonly Factory.RepositoryFactory factory;
public HomeController(BlogPostRepository factory)
public HomeController(Factory.RepositoryFactory factory)
{
this.factory = factory;
}
public IActionResult Index()
public async Task<IActionResult> Index()
{
var data = factory.GetData();
var data = await factory.GetData();
return View();
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetMashup.Web.Global;
using DotNetMashup.Web.Model;
using DotNetMashup.Web.Repositories;
using Microsoft.Framework.Caching.Memory;
namespace DotNetMashup.Web.Factory
{
public class RepositoryFactory
{
private readonly IMemoryCache cache;
private List<IRepository> Repos;
private const string cacheKey = "data";
public RepositoryFactory(IEnumerable<IBlogMetaData> data, ISiteSetting setting, IMemoryCache cache)
{
if(data == null)
{
throw new ArgumentNullException("data");
}
if(setting == null)
{
throw new ArgumentNullException("setting");
}
if(cache == null)
{
throw new ArgumentNullException("cache");
}
Repos = new List<IRepository>()
{
new GitHubRepository(),
new BlogPostRepository(data, setting)
};
this.cache = cache;
}
public async Task<List<IExternalData>> GetData()
{
var cachedData = this.cache.Get<List<IExternalData>>(cacheKey);
if(cachedData != null && cachedData.Count > 0) return cachedData;
var tasks = Repos.Select(a => a.GetData());
await Task.WhenAll(tasks);
var result = tasks.SelectMany(a => a.Result).OrderBy(a => a.PublishedDate).ToList();
cache.Set(cacheKey, result, new MemoryCacheEntryOptions { AbsoluteExpiration = DateTime.Now.AddHours(4) });
return result;
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DotNetMashup.Web.Model
{
public struct Author
{
public string Name { get; set; }
public string ImageUrl { get; set; }
public string AuthorUrl { get; set; }
public string Email { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DotNetMashup.Web.Model
{
/// <summary>
/// Simple class to implement the standard external interface
/// </summary>
public class BaseExternalData : IExternalData
{
public Author Author { get; set; }
public string Content { get; set; }
public string OriginalLink { get; set; }
public string Title { get; set; }
public DateTime PublishedDate { get; set; }
}
}

View File

@@ -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 : IMetaData
public class BlogMetaData : IBlogMetaData
{
public string FeedUrl { get; set; }
public string Author { get; set; }

View File

@@ -13,7 +13,6 @@ namespace DotNetMashup.Web.Model
public string Summary { get; set; }
public string Localink { get; set; }
public string OriginalLink { get; set; }
public string Author { get; set; }
public string AuthorEmail { get; set; }
public Author Author { get; set; }
}
}

View File

@@ -0,0 +1,6 @@
namespace DotNetMashup.Web.Model
{
public class GithubAnnouncement : BaseExternalData
{
}
}

View File

@@ -5,7 +5,7 @@ using System.Threading.Tasks;
namespace DotNetMashup.Web.Model
{
public interface IMetaData
public interface IBlogMetaData
{
string FeedUrl { get; set; }
string Author { get; set; }

View File

@@ -10,6 +10,7 @@ namespace DotNetMashup.Web.Model
string Title { get; set; }
string Content { get; set; }
string OriginalLink { get; set; }
string Author { get; set; }
Author Author { get; set; }
DateTime PublishedDate { get; set; }
}
}

View File

@@ -17,10 +17,10 @@ namespace DotNetMashup.Web.Repositories
private readonly ISiteSetting setting;
private readonly IMemoryCache cache;
private readonly IEnumerable<MetaData> _data;
private readonly IEnumerable<IBlogMetaData> _data;
private const string cacheKey = "blogposts";
public BlogPostRepository(IEnumerable<MetaData> data, IMemoryCache cache, ISiteSetting setting)
public BlogPostRepository(IEnumerable<IBlogMetaData> data, ISiteSetting setting)
{
this._data = data;
this.cache = cache;
@@ -37,8 +37,6 @@ namespace DotNetMashup.Web.Repositories
public async Task<IEnumerable<IExternalData>> GetData()
{
var cachedata = cache.Get<IEnumerable<IExternalData>>(cacheKey);
if(cachedata != null) return cachedata;
var syndicationFeeds = await GetSyndicationFeeds(_data);
var data = syndicationFeeds
@@ -91,21 +89,18 @@ namespace DotNetMashup.Web.Repositories
{
Title = x.Item.Title.Text,
Summary = truncatedSummary,
Author = authorname,
AuthorEmail = authoremail,
Author = new Author { Email = authoremail, Name = authorname },
Localink = locallink,
OriginalLink = originallink,
PublishedDate = x.Item.PublishDate.DateTime,
Content = content
};
})
.OrderByDescending(x => x.PublishedDate)
.ToList();
cache.Set(cacheKey, data.Cast<IExternalData>());
return data;
}
private async static Task<IEnumerable<KeyValuePair<string, SyndicationFeed>>> GetSyndicationFeeds(IEnumerable<IMetaData> metadataEntries)
private async static Task<IEnumerable<KeyValuePair<string, SyndicationFeed>>> GetSyndicationFeeds(IEnumerable<IBlogMetaData> metadataEntries)
{
var syndicationFeeds = new List<KeyValuePair<string, SyndicationFeed>>();
foreach(var metadata in metadataEntries)
@@ -122,15 +117,14 @@ namespace DotNetMashup.Web.Repositories
try
{
SyndicationFeed feed = null;
await Task.Run(() => {
await Task.Run(() =>
{
using(var reader = XmlReader.Create(url))
{
feed = SyndicationFeed.Load(reader);
}
});
if(feed != null)
{
feeds.Add(new KeyValuePair<string, SyndicationFeed>(id, feed));
@@ -152,7 +146,6 @@ namespace DotNetMashup.Web.Repositories
//Unable to load RSS feed
}
return feeds;
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetMashup.Web.Model;
using Octokit;
namespace DotNetMashup.Web.Repositories
{
public class GitHubRepository : IRepository
{
public string FactoryName
{
get
{
return "Github";
}
}
public async Task<IEnumerable<IExternalData>> GetData()
{
var client = new GitHubClient(new ProductHeaderValue("dotnetmashup"));
var issues = await client.Issue.GetAllForRepository("aspnet", "Announcements");
return issues.Select(a => new GithubAnnouncement
{
Author = new Model.Author { Name = string.IsNullOrWhiteSpace(a.User.Name) ? a.User.Name : a.User.Login, AuthorUrl = a.User.Url, ImageUrl = a.User.AvatarUrl, Email = a.User.Email },
Content = a.Body,
Title = a.Title,
OriginalLink = a.Url.AbsoluteUri
});
}
}
}

View File

@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.IO;
using DotNetMashup.Web.Repositories;
using DotNetMashup.Web.Factory;
using DotNetMashup.Web.Global;
using DotNetMashup.Web.Model;
using DotNetMashup.Web.Repositories;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Dnx.Runtime;
@@ -16,7 +17,7 @@ namespace DotNetMashup.Web
{
public class Startup
{
private IEnumerable<MetaData> _feedData = null;
private IEnumerable<BlogMetaData> _feedData = null;
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
@@ -25,7 +26,7 @@ namespace DotNetMashup.Web
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
_feedData = JsonConvert.DeserializeObject<IEnumerable<MetaData>>(File.ReadAllText(Path.Combine(appEnv.ApplicationBasePath, "blogfeed.json")));
_feedData = JsonConvert.DeserializeObject<IEnumerable<BlogMetaData>>(File.ReadAllText(Path.Combine(appEnv.ApplicationBasePath, "blogfeed.json")));
}
public IConfigurationRoot Configuration { get; set; }
@@ -42,7 +43,7 @@ namespace DotNetMashup.Web
return new MemoryCache(new MemoryCacheOptions());
});
services.AddInstance(_feedData);
services.AddSingleton<BlogPostRepository>();
services.AddSingleton<RepositoryFactory>();
// Add MVC services to the services container.
services.AddMvc();

View File

@@ -16,7 +16,8 @@
"Microsoft.Framework.Logging.Debug": "1.0.0-beta7",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta7",
"Newtonsoft.Json": "7.0.1",
"TweetinviAPI": "0.9.9.7"
"TweetinviAPI": "0.9.9.7",
"Octokit": "0.16.0"
},
"commands": {
@@ -24,7 +25,7 @@
},
"frameworks": {
"dnx46": {
"dnx451": {
"frameworkAssemblies": {
"System.ServiceModel": "4.0.0.0"
}