11using Microsoft . SyndicationFeed ;
2- using System . Xml ;
32using Microsoft . SyndicationFeed . Atom ;
43using Pilgaard . Blog . Features . BlogPost ;
54using Pilgaard . Blog . Features . SEO ;
5+ using System . Xml ;
66
77namespace Pilgaard . Blog . Features . Feed ;
88
99public static class FeedApi
1010{
1111 public static WebApplication MapRssFeed ( this WebApplication app )
1212 {
13- app . MapGet ( "/feed.xml" , async ( HttpContext context ) =>
13+ app . MapGet ( FeedConstants . FeedRoute , async ( HttpContext context ) =>
1414 {
1515 context . Response . ContentType = "application/xml" ;
1616
@@ -24,40 +24,9 @@ public static WebApplication MapRssFeed(this WebApplication app)
2424
2525 var writer = new AtomFeedWriter ( xmlWriter ) ;
2626
27- await writer . WriteTitle ( DefaultMetadata . Title ) ;
28-
29- var lastUpdated = BlogPostData
30- . AllBlogPostSeries
31- . SelectMany ( blogPostSeries => blogPostSeries . BlogPosts . Select ( blogPost => blogPost . LastUpdated ) )
32- . Max ( ) ;
27+ await WriteHeader ( writer ) ;
3328
34- await writer . WriteUpdated ( lastUpdated ) ;
35- await writer . Write ( new SyndicationLink ( new Uri ( FeedConstants . BlogUrl ) ) ) ;
36-
37- foreach ( var blogPostSeries in BlogPostData . AllBlogPostSeries )
38- {
39- foreach ( var blogPost in blogPostSeries . BlogPosts )
40- {
41- var link = $ "{ FeedConstants . BlogUrl } /{ blogPostSeries . GetRelativePath ( blogPost ) } ";
42- var item = new SyndicationItem
43- {
44- Id = link ,
45- Title = blogPost . Title ,
46- Description = blogPost . Description ,
47- Published = blogPost . PublishDate ,
48- LastUpdated = blogPost . LastUpdated ,
49- } ;
50-
51- item . AddLink ( new SyndicationLink ( new Uri ( link ) ) ) ;
52- item . AddContributor ( new SyndicationPerson ( "Niels Pilgaard Grøndahl" , "[email protected] " ) ) ; 53- foreach ( var blogPostTag in blogPost . Tags )
54- {
55- item . AddCategory ( new SyndicationCategory ( blogPostTag ) ) ;
56- }
57-
58- await writer . Write ( item ) ;
59- }
60- }
29+ await WriteBody ( writer ) ;
6130
6231 // This closes the <content> element
6332 await xmlWriter . WriteEndElementAsync ( ) ;
@@ -69,4 +38,47 @@ public static WebApplication MapRssFeed(this WebApplication app)
6938
7039 return app ;
7140 }
41+
42+ private static async Task WriteBody ( ISyndicationFeedWriter writer )
43+ {
44+ foreach ( var blogPostSeries in BlogPostData . AllBlogPostSeries )
45+ {
46+ foreach ( var blogPost in blogPostSeries . BlogPosts )
47+ {
48+ var link = $ "{ FeedConstants . BlogUrl } /{ blogPostSeries . GetRelativePath ( blogPost ) } ";
49+ var item = new SyndicationItem
50+ {
51+ Id = link ,
52+ Title = blogPost . Title ,
53+ Description = blogPost . Description ,
54+ Published = blogPost . PublishDate ,
55+ LastUpdated = blogPost . LastUpdated ,
56+ } ;
57+
58+ item . AddLink ( new SyndicationLink ( new Uri ( link ) ) ) ;
59+ item . AddContributor ( new SyndicationPerson ( "Niels Pilgaard Grøndahl" , "[email protected] " ) ) ; 60+ foreach ( var blogPostTag in blogPost . Tags )
61+ {
62+ item . AddCategory ( new SyndicationCategory ( blogPostTag ) ) ;
63+ }
64+
65+ await writer . Write ( item ) ;
66+ }
67+ }
68+ }
69+
70+ private static async Task WriteHeader ( AtomFeedWriter writer )
71+ {
72+ await writer . WriteTitle ( DefaultMetadata . Title ) ;
73+
74+ var lastUpdated = BlogPostData
75+ . AllBlogPostSeries
76+ . SelectMany ( blogPostSeries => blogPostSeries . BlogPosts . Select ( blogPost => blogPost . LastUpdated ) )
77+ . Max ( ) ;
78+
79+ await writer . WriteUpdated ( lastUpdated ) ;
80+ await writer . WriteId ( FeedConstants . FeedUrl ) ;
81+ await writer . Write ( new SyndicationLink ( new Uri ( FeedConstants . FeedUrl ) , "self" ) ) ;
82+ await writer . Write ( new SyndicationLink ( new Uri ( FeedConstants . BlogUrl ) , "alternate" ) ) ;
83+ }
7284}
0 commit comments