Skip to content

Commit daecaec

Browse files
committed
Added infrastructure for sitemap generator
1 parent 61182aa commit daecaec

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@page "/Sitemap"
2+
@using LinkDotNet.Blog.Web.Shared.Services.Sitemap
3+
@inject ISitemapService sitemapService
4+
5+
<div class="row">
6+
<button class="btn btn-primary" @onclick="CreateSitemap">Create Sitemap</button>
7+
8+
@if (urlSet != null)
9+
{
10+
<table class="table table-striped table-hover">
11+
<tbody>
12+
<tr>
13+
<th>Url</th>
14+
<th>Last Changed</th>
15+
</tr>
16+
@foreach (var url in urlSet.Urls)
17+
{
18+
<tr>
19+
<td>@url.Location</td>
20+
<td>@url.LastModified</td>
21+
</tr>
22+
}
23+
</tbody>
24+
</table>
25+
}
26+
</div>
27+
28+
@code {
29+
private UrlSet urlSet;
30+
31+
private async Task CreateSitemap()
32+
{
33+
urlSet = await sitemapService.CreateSitemapAsync();
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace LinkDotNet.Blog.Web.Shared.Services.Sitemap
4+
{
5+
public interface ISitemapService
6+
{
7+
Task<UrlSet> CreateSitemapAsync();
8+
}
9+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using LinkDotNet.Blog.Domain;
6+
using LinkDotNet.Blog.Infrastructure.Persistence;
7+
using Microsoft.AspNetCore.Components;
8+
9+
namespace LinkDotNet.Blog.Web.Shared.Services.Sitemap
10+
{
11+
public class SitemapService : ISitemapService
12+
{
13+
private readonly IRepository<BlogPost> repository;
14+
private readonly NavigationManager navigationManager;
15+
16+
public SitemapService(
17+
IRepository<BlogPost> repository,
18+
NavigationManager navigationManager)
19+
{
20+
this.repository = repository;
21+
this.navigationManager = navigationManager;
22+
}
23+
24+
public async Task<UrlSet> CreateSitemapAsync()
25+
{
26+
const string sitemapNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9";
27+
var urlSet = new UrlSet
28+
{
29+
Namespace = sitemapNamespace,
30+
};
31+
32+
var blogPosts = (await repository.GetAllAsync(f => f.IsPublished, b => b.UpdatedDate)).ToList();
33+
34+
urlSet.Urls.Add(new Url { Location = navigationManager.BaseUri });
35+
urlSet.Urls.AddRange(CreateUrlsForBlogPosts(blogPosts));
36+
urlSet.Urls.AddRange(CreateUrlsForTags(blogPosts));
37+
38+
return urlSet;
39+
}
40+
41+
private IEnumerable<Url> CreateUrlsForBlogPosts(IEnumerable<BlogPost> blogPosts)
42+
{
43+
return blogPosts.Select(b => new Url
44+
{
45+
Location = $"{navigationManager.BaseUri}blogPosts/{b.Id}",
46+
LastModified = b.UpdatedDate.ToString("yyyy-MM-dd"),
47+
}).ToList();
48+
}
49+
50+
private IEnumerable<Url> CreateUrlsForTags(IEnumerable<BlogPost> blogPosts)
51+
{
52+
return blogPosts
53+
.SelectMany(b => b.Tags)
54+
.Select(t => t.Content)
55+
.Distinct()
56+
.Select(t => new Url
57+
{
58+
Location = $"{navigationManager.BaseUri}searchByTag/{Uri.EscapeDataString(t)}",
59+
});
60+
}
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Xml.Serialization;
2+
3+
namespace LinkDotNet.Blog.Web.Shared.Services.Sitemap
4+
{
5+
[XmlRoot(ElementName="url")]
6+
public class Url
7+
{
8+
[XmlElement(ElementName="loc")]
9+
public string Location { get; set; }
10+
11+
[XmlElement(ElementName="lastmod")]
12+
public string LastModified { get; set; }
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Xml.Serialization;
3+
4+
namespace LinkDotNet.Blog.Web.Shared.Services.Sitemap
5+
{
6+
[XmlRoot(ElementName="urlset")]
7+
public class UrlSet
8+
{
9+
[XmlElement(ElementName = "url")]
10+
public List<Url> Urls { get; set; } = new();
11+
12+
[XmlAttribute(AttributeName="xmlns")]
13+
public string Namespace { get; set; }
14+
}
15+
}

0 commit comments

Comments
 (0)