8
8
using LazyCache ;
9
9
using Microsoft . Extensions . Logging ;
10
10
using Fritz . StreamTools . Services ;
11
+ using System . Collections . Generic ;
12
+ using System . Net . Http ;
13
+ using Newtonsoft . Json ;
14
+ using System . IO ;
11
15
12
16
namespace Fritz . StreamTools . Controllers
13
17
{
@@ -18,13 +22,15 @@ public GitHubController(
18
22
GitHubRepository repository ,
19
23
GithubyMcGithubFaceClient client ,
20
24
ILogger < GitHubController > logger ,
21
- IOptions < GitHubConfiguration > githubConfiguration )
25
+ IOptions < GitHubConfiguration > githubConfiguration ,
26
+ IHttpClientFactory httpClientFactory )
22
27
{
23
28
this . Cache = cache ;
24
29
this . Logger = logger ;
25
30
this . Client = client ;
26
31
_gitHubRepository = repository ;
27
32
_gitHubConfiguration = githubConfiguration . Value ;
33
+ _httpClient = httpClientFactory . CreateClient ( "DiscoverDotNet" ) ;
28
34
}
29
35
30
36
public IAppCache Cache { get ; }
@@ -35,6 +41,10 @@ public GitHubController(
35
41
36
42
private readonly GitHubConfiguration _gitHubConfiguration ;
37
43
44
+ private readonly HttpClient _httpClient ;
45
+
46
+ private static JsonSerializer _serializer = new JsonSerializer ( ) ;
47
+
38
48
public async Task < IActionResult > ContributorsInformation ( string repo , string userName , int count )
39
49
{
40
50
var outModel = await _gitHubRepository . GetRecentContributors ( _gitHubConfiguration . RepositoryCsv ) ;
@@ -49,14 +59,35 @@ public async Task<IActionResult> ContributorsInformation(string repo, string use
49
59
} ) ;
50
60
}
51
61
52
-
62
+ var newsPosts = await GetNewsFromDiscoverDotNet ( ) ;
63
+ var outViewModel = new TickerViewModel ( )
64
+ {
65
+ GitHubInformation = outModel . ToArray ( ) ,
66
+ News = newsPosts
67
+ } ;
53
68
54
69
ViewBag . Configuration = _gitHubConfiguration ;
55
70
56
- return View ( $ "contributor_{ _gitHubConfiguration . DisplayMode } ", outModel . ToArray ( ) ) ;
71
+ return View ( $ "contributor_{ _gitHubConfiguration . DisplayMode } ", outViewModel ) ;
57
72
58
73
}
59
74
75
+ private async Task < ( string source , string color , IEnumerable < BlogPostModel > blogPosts ) > GetNewsFromDiscoverDotNet ( )
76
+ {
77
+ IList < BlogPostModel > posts ;
78
+ using ( Stream response = await _httpClient . GetStreamAsync ( "https://discoverdot.net/data/news.json" ) )
79
+ {
80
+ using ( StreamReader reader = new StreamReader ( response ) )
81
+ {
82
+ using ( JsonTextReader json = new JsonTextReader ( reader ) )
83
+ {
84
+ posts = _serializer . Deserialize < IList < BlogPostModel > > ( json ) ;
85
+ }
86
+ }
87
+ }
88
+ return ( "Discover .NET" , "#551A8B" , posts . Take ( 5 ) . ToArray ( ) ) ;
89
+ }
90
+
60
91
public IActionResult Configuration ( )
61
92
{
62
93
return View ( _gitHubConfiguration ) ;
@@ -145,4 +176,21 @@ public async Task<IActionResult> VerifyRepository(string repositoryName, string
145
176
return Json ( true ) ;
146
177
}
147
178
}
179
+
180
+ public class TickerViewModel {
181
+
182
+ public IEnumerable < GitHubInformation > GitHubInformation { get ; set ; }
183
+
184
+ public ( string source , string color , IEnumerable < BlogPostModel > blogPosts ) News { get ; set ; }
185
+
186
+ }
187
+
188
+ public class BlogPostModel
189
+ {
190
+ public string Title { get ; set ; }
191
+ public string FeedTitle { get ; set ; }
192
+ public string Link { get ; set ; }
193
+ public DateTime Published { get ; set ; }
194
+ }
195
+
148
196
}
0 commit comments