From 035f442ba0afece776809ffaae45b2418ad16393 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sun, 28 Apr 2019 11:43:05 +0100 Subject: [PATCH] Set friendly date for blog posts --- .../VIewModels/BlogPostViewModelTests.cs | 34 +++++++++++++++++++ Website/ViewModels/BlogPostViewModel.cs | 26 ++++++++++++-- Website/Views/Blog/Entry.cshtml | 2 +- Website/Views/Blog/Page.cshtml | 2 +- Website/Views/Home/Index.cshtml | 2 +- 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/Website.Tests/VIewModels/BlogPostViewModelTests.cs b/Website.Tests/VIewModels/BlogPostViewModelTests.cs index f2f400e..d70022a 100644 --- a/Website.Tests/VIewModels/BlogPostViewModelTests.cs +++ b/Website.Tests/VIewModels/BlogPostViewModelTests.cs @@ -1,3 +1,4 @@ +using System; using FluentAssertions; using NSubstitute; using Website.Data.States; @@ -18,5 +19,38 @@ namespace Website.Tests.VIewModels var vm = new BlogPostViewModel(post); vm.Content.Should().Be("

header

"); } + + [Fact] + public void Timestamp_OnThe1st_IsFriendlyString() { + var state = new BlogPostState { + Post_Content = "", + Post_Timestamp = new DateTime(2018, 10, 01, 15, 1, 25) + }; + var post = new BlogPost(state); + var vm = new BlogPostViewModel(post); + vm.Timestamp.Should().Be("Monday the 1st of October 2018"); + } + + [Fact] + public void Timestamp_OnThe2nd_IsFriendlyString() { + var state = new BlogPostState { + Post_Content = "", + Post_Timestamp = new DateTime(2018, 10, 02, 15, 1, 25) + }; + var post = new BlogPost(state); + var vm = new BlogPostViewModel(post); + vm.Timestamp.Should().Be("Tuesday the 2nd of October 2018"); + } + + [Fact] + public void Timestamp_OnThe3rd_IsFriendlyString() { + var state = new BlogPostState { + Post_Content = "", + Post_Timestamp = new DateTime(2018, 10, 03, 15, 1, 25) + }; + var post = new BlogPost(state); + var vm = new BlogPostViewModel(post); + vm.Timestamp.Should().Be("Wednesday the 3rd of October 2018"); + } } } \ No newline at end of file diff --git a/Website/ViewModels/BlogPostViewModel.cs b/Website/ViewModels/BlogPostViewModel.cs index 8fccca2..14df1dc 100644 --- a/Website/ViewModels/BlogPostViewModel.cs +++ b/Website/ViewModels/BlogPostViewModel.cs @@ -10,12 +10,34 @@ namespace Website.ViewModels public BlogPostViewModel(BlogPost blogPost) { Title = blogPost.Title; - Timestamp = blogPost.Timestamp; + Timestamp = FormatTimestamp(blogPost.Timestamp); Url = blogPost.Url; Content = Markdown.ToHtml(blogPost.Content, GetPipeline()).Trim(); } + string GetDaySuffix(int day) { + switch (day) { + case 1: + case 21: + case 31: + return @"\s\t"; + case 2: + case 22: + return @"\n\d"; + case 3: + case 23: + return @"\r\d"; + default: + return @"\t\h"; + } + } + + private string FormatTimestamp(DateTime timestamp) { + var suffix = GetDaySuffix(timestamp.Day); + return timestamp.ToString($@"dddd \t\h\e d{suffix} \o\f MMMM yyyy"); + } + protected MarkdownPipeline GetPipeline()=>new MarkdownPipelineBuilder() //.UseAdvancedExtensions() .UseAutoLinks() @@ -25,7 +47,7 @@ namespace Website.ViewModels public string Title { get; private set; } public string Content { get; protected set; } - public DateTime Timestamp { get; private set; } + public string Timestamp { get; private set; } public string Url { get; private set; } } } \ No newline at end of file diff --git a/Website/Views/Blog/Entry.cshtml b/Website/Views/Blog/Entry.cshtml index 4812180..39227f2 100644 --- a/Website/Views/Blog/Entry.cshtml +++ b/Website/Views/Blog/Entry.cshtml @@ -6,4 +6,4 @@ @Html.Raw(Model.Content) -Posted on @Model.Timestamp.ToString() \ No newline at end of file +Posted on @Model.Timestamp \ No newline at end of file diff --git a/Website/Views/Blog/Page.cshtml b/Website/Views/Blog/Page.cshtml index 41b7ebc..f4df8b4 100644 --- a/Website/Views/Blog/Page.cshtml +++ b/Website/Views/Blog/Page.cshtml @@ -23,7 +23,7 @@

@post.Title

@Html.Raw(post.Content) - Posted on @post.Timestamp.ToString() + Posted on @post.Timestamp

Read more...

} \ No newline at end of file diff --git a/Website/Views/Home/Index.cshtml b/Website/Views/Home/Index.cshtml index fbd8ffd..a75f293 100644 --- a/Website/Views/Home/Index.cshtml +++ b/Website/Views/Home/Index.cshtml @@ -15,5 +15,5 @@

Latest Blog Post

@Model.Title

@Html.Raw(Model.Content) -Posted on @Model.Timestamp.ToString() +Posted on @Model.Timestamp

Read more...

\ No newline at end of file