Skip to content

Commit 200e781

Browse files
committed
refactor: Moved reading time into BlogPost
1 parent 90e0d8d commit 200e781

File tree

5 files changed

+15
-23
lines changed

5 files changed

+15
-23
lines changed

src/LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ private BlogPost()
3838

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

41+
public int ReadingTimeInMinutes { get; private set; }
42+
4143
public string Slug => GenerateSlug();
4244

4345
private string GenerateSlug()
@@ -56,13 +58,13 @@ private string GenerateSlug()
5658

5759
var slug = Title.ToLower(CultureInfo.CurrentCulture);
5860

59-
// Remove all special characters from the string.
61+
// Remove all special characters from the string.
6062
slug = MatchIfSpecialCharactersExist().Replace(slug, "");
6163

62-
// Remove all additional spaces in favour of just one.
64+
// Remove all additional spaces in favour of just one.
6365
slug= MatchIfAdditionalSpacesExist().Replace(slug," ").Trim();
6466

65-
// Replace all spaces with the hyphen.
67+
// Replace all spaces with the hyphen.
6668
slug= MatchIfSpaceExist().Replace(slug, "-");
6769

6870
return slug;
@@ -115,6 +117,7 @@ public static BlogPost Create(
115117
PreviewImageUrlFallback = previewImageUrlFallback,
116118
IsPublished = isPublished,
117119
Tags = tags?.Select(t => t.Trim()).ToImmutableArray(),
120+
ReadingTimeInMinutes = ReadingTimeCalculator.CalculateReadingTime(content),
118121
};
119122

120123
return blogPost;
@@ -144,5 +147,6 @@ public void Update(BlogPost from)
144147
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
145148
IsPublished = from.IsPublished;
146149
Tags = from.Tags;
150+
ReadingTimeInMinutes = from.ReadingTimeInMinutes;
147151
}
148152
}

src/LinkDotNet.Blog.Web/Features/Services/ReadingTimeCalculator.cs renamed to src/LinkDotNet.Blog.Domain/ReadingTimeCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Text.RegularExpressions;
33

4-
namespace LinkDotNet.Blog.Web.Features.Services;
4+
namespace LinkDotNet.Blog.Domain;
55

66
public static partial class ReadingTimeCalculator
77
{

src/LinkDotNet.Blog.Web/Features/Components/ShortBlogPost.razor

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
@using LinkDotNet.Blog.Web.Features.Services
66
@using Microsoft.Extensions.Caching.Memory
77

8-
@inject IMemoryCache MemoryCache
98
<article>
109
<div class="blog-card @AltCssClass">
1110
<div class="meta">
@@ -24,7 +23,7 @@
2423
<li class="draft">Draft</li>
2524
}
2625
<li class="date me-4"><span>@BlogPost.UpdatedDate.ToString("dd/MM/yyyy")</span></li>
27-
@if (BlogPost.Tags != null && BlogPost.Tags.Any())
26+
@if (BlogPost.Tags.Any())
2827
{
2928
<li class="tags me-4">
3029
<ul>
@@ -35,7 +34,7 @@
3534
</ul>
3635
</li>
3736
}
38-
<li class="read-time me-4">@readingTime min</li>
37+
<li class="read-time me-4">@BlogPost.ReadingTimeInMinutes min</li>
3938
</ul>
4039
</div>
4140
<div class="description">
@@ -50,8 +49,6 @@
5049
</article>
5150

5251
@code {
53-
private int readingTime;
54-
5552
[Parameter]
5653
public BlogPost BlogPost { get; set; }
5754

@@ -83,14 +80,4 @@
8380

8481
return base.SetParametersAsync(ParameterView.Empty);
8582
}
86-
87-
protected override void OnInitialized()
88-
{
89-
var key = "reading-time-" + BlogPost.Id;
90-
readingTime = MemoryCache.GetOrCreate(key, entry =>
91-
{
92-
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
93-
return ReadingTimeCalculator.CalculateReadingTime(BlogPost.Content);
94-
});
95-
}
9683
}

tests/LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ public void ShouldUpdateBlogPost()
1212
{
1313
var blogPostToUpdate = new BlogPostBuilder().Build();
1414
blogPostToUpdate.Id = "random-id";
15-
var blogPost = BlogPost.Create("Title", "Desc", "Content", "Url", true, previewImageUrlFallback: "Url2");
15+
var blogPost = BlogPost.Create("Title", "Desc", "Other Content", "Url", true, previewImageUrlFallback: "Url2");
1616
blogPost.Id = "something else";
1717

1818
blogPostToUpdate.Update(blogPost);
1919

2020
blogPostToUpdate.Title.Should().Be("Title");
2121
blogPostToUpdate.ShortDescription.Should().Be("Desc");
22-
blogPostToUpdate.Content.Should().Be("Content");
22+
blogPostToUpdate.Content.Should().Be("Other Content");
2323
blogPostToUpdate.PreviewImageUrl.Should().Be("Url");
2424
blogPostToUpdate.PreviewImageUrlFallback.Should().Be("Url2");
2525
blogPostToUpdate.IsPublished.Should().BeTrue();
2626
blogPostToUpdate.Tags.Should().BeNullOrEmpty();
2727
blogPostToUpdate.Slug.Should().NotBeNull();
28+
blogPostToUpdate.ReadingTimeInMinutes.Should().Be(1);
2829
}
2930

3031
[Theory]

tests/LinkDotNet.Blog.UnitTests/Web/Features/Services/ReadingTimeCalculatorTests.cs renamed to tests/LinkDotNet.Blog.UnitTests/Domain/ReadingTimeCalculatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Linq;
2-
using LinkDotNet.Blog.Web.Features.Services;
2+
using LinkDotNet.Blog.Domain;
33

4-
namespace LinkDotNet.Blog.UnitTests.Web.Features.Services;
4+
namespace LinkDotNet.Blog.UnitTests.Domain;
55

66
public class ReadingTimeCalculatorTests
77
{

0 commit comments

Comments
 (0)