Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/search-engine-friendly-url #317

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/LinkDotNet.Blog.Domain/BlogPost.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations.Schema;
using System.Globalization;
using System.Linq;

namespace LinkDotNet.Blog.Domain;
Expand Down Expand Up @@ -35,6 +37,18 @@ private BlogPost()

public string TagsAsString => Tags is null ? string.Empty : string.Join(", ", Tags);

[NotMapped]
Kamyab7 marked this conversation as resolved.
Show resolved Hide resolved
public string SearchEngineFriendlyUrl => GenerateSearchEngineFriendlyUrl();
Kamyab7 marked this conversation as resolved.
Show resolved Hide resolved

private string GenerateSearchEngineFriendlyUrl()
Kamyab7 marked this conversation as resolved.
Show resolved Hide resolved
{
string SearchEngineFriendlyTitle = Title
Kamyab7 marked this conversation as resolved.
Show resolved Hide resolved
.Replace(' ', '-')
.ToLower(CultureInfo.CurrentCulture);

return $"{Id}/{SearchEngineFriendlyTitle}";
}

public static BlogPost Create(
string title,
string shortDescription,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using LinkDotNet.Blog.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using System;

namespace LinkDotNet.Blog.Infrastructure.Persistence.Sql.Mapping;

Expand All @@ -11,7 +14,8 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
builder.HasKey(c => c.Id);
builder.Property(c => c.Id)
.IsUnicode(false)
.ValueGeneratedOnAdd();
.ValueGeneratedOnAdd()
.HasValueGenerator<BlogPostIdGenerator>();
builder.Property(x => x.Title).HasMaxLength(256).IsRequired();
builder.Property(x => x.PreviewImageUrl).HasMaxLength(1024).IsRequired();
builder.Property(x => x.PreviewImageUrlFallback).HasMaxLength(1024);
Expand All @@ -26,3 +30,14 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
.IsDescending(false, true);
}
}


internal sealed class BlogPostIdGenerator : ValueGenerator<string>
Kamyab7 marked this conversation as resolved.
Show resolved Hide resolved
{
public override bool GeneratesTemporaryValues => false;

public override string Next(EntityEntry entry)
{
return Guid.NewGuid().ToString("N").Substring(0, 15);
}
}
7 changes: 4 additions & 3 deletions src/LinkDotNet.Blog.Web/Controller/RssFeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static XmlWriterSettings CreateXmlWriterSettings()

private static SyndicationItem CreateSyndicationItemFromBlogPost(string url, BlogPostRssInfo blogPost)
{
var blogPostUrl = url + $"/blogPost/{blogPost.Id}";
var blogPostUrl = url + $"/blogPost/{blogPost.SearchEngineFriendlyUrl}";
var shortDescription = MarkdownConverter.ToPlainString(blogPost.ShortDescription);
var item = new SyndicationItem(
blogPost.Title,
Expand Down Expand Up @@ -96,7 +96,7 @@ private static void AddCategories(Collection<SyndicationCategory> categories, Bl
private async Task<IEnumerable<SyndicationItem>> GetBlogPostItems(string url)
{
var blogPosts = await blogPostRepository.GetAllByProjectionAsync(
s => new BlogPostRssInfo(s.Id, s.Title, s.ShortDescription, s.UpdatedDate, s.PreviewImageUrl, s.Tags),
s => new BlogPostRssInfo(s.Id, s.Title, s.ShortDescription, s.UpdatedDate, s.PreviewImageUrl, s.Tags, s.SearchEngineFriendlyUrl),
f => f.IsPublished,
orderBy: post => post.UpdatedDate);
return blogPosts.Select(bp => CreateSyndicationItemFromBlogPost(url, bp));
Expand All @@ -108,5 +108,6 @@ private sealed record BlogPostRssInfo(
string ShortDescription,
DateTime UpdatedDate,
string PreviewImageUrl,
IEnumerable<string> Tags);
IEnumerable<string> Tags,
string SearchEngineFriendlyUrl);
}
8 changes: 4 additions & 4 deletions src/LinkDotNet.Blog.Web/Features/Archive/ArchivePage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
<ul class="ps-5">
@foreach (var blogPost in yearGroup.OrderByDescending(b => b.UpdatedDate))
{
<li class="pt-1"><a href="/blogPost/@blogPost.Id">@blogPost.Title</a></li>
<li class="pt-1"><a href="/blogPost/@blogPost.SearchEngineFriendlyUrl">@blogPost.Title</a></li>
}
</ul>
}
}
</div >
</div>

@code {
private IReadOnlyCollection<IGrouping<int, BlogPostPerYear>> blogPostsPerYear;
Expand All @@ -35,7 +35,7 @@
protected override async Task OnInitializedAsync()
{
var blogPosts = await Repository.GetAllByProjectionAsync(
p => new BlogPostPerYear(p.Id, p.Title, p.UpdatedDate),
p => new BlogPostPerYear(p.Id, p.SearchEngineFriendlyUrl, p.Title, p.UpdatedDate),
p => p.IsPublished);
blogPostCount = blogPosts.Count;
blogPostsPerYear = blogPosts
Expand All @@ -44,5 +44,5 @@
.ToImmutableArray();
}

private sealed record BlogPostPerYear(string Id, string Title, DateTime UpdatedDate);
private sealed record BlogPostPerYear(string Id, string SearchEngineFriendlyUrl, string Title, DateTime UpdatedDate);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using System.Net
@using System.Net
@using System.Text.RegularExpressions
@using System.Web
@using LinkDotNet.Blog.Domain
Expand Down Expand Up @@ -43,7 +43,7 @@
<h2></h2>
<p>@MarkdownConverter.ToMarkupString(BlogPost.ShortDescription)</p>
<p class="read-more">
<a href="/blogPost/@BlogPost.Id" aria-label="@BlogPost.Title">Read the whole article</a>
<a href="/blogPost/@BlogPost.SearchEngineFriendlyUrl" aria-label="@BlogPost.Title">Read the whole article</a>
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/blogPost/{blogPostId}"
@page "/blogPost/{blogPostId}/{searchEngineFriendlyTitle}"
@using Markdig
@using LinkDotNet.Blog.Domain
@using LinkDotNet.Blog.Infrastructure.Persistence
Expand Down Expand Up @@ -72,6 +72,9 @@ else
[Parameter]
public string BlogPostId { get; set; }

[Parameter]
public string SearchEngineFriendlyTitle { get; set; }
linkdotnet marked this conversation as resolved.
Show resolved Hide resolved

private string OgDataImage => BlogPost.PreviewImageUrlFallback ?? BlogPost.PreviewImageUrl;

private BlogPost BlogPost { get; set; }
Expand Down