Skip to content

Commit a0f84fe

Browse files
committed
Option to not update UpdateDate (fixes #20)
1 parent 70ea414 commit a0f84fe

File tree

7 files changed

+78
-10
lines changed

7 files changed

+78
-10
lines changed

LinkDotNet.Blog.IntegrationTests/Infrastructure/Persistence/Sql/SqlRepositoryTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class SqlRepositoryTests : SqlDatabaseTestBase
1313
[Fact]
1414
public async Task ShouldLoadBlogPost()
1515
{
16-
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, new[] { "Tag 1", "Tag 2" });
16+
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, tags: new[] { "Tag 1", "Tag 2" });
1717
await DbContext.BlogPosts.AddAsync(blogPost);
1818
await DbContext.SaveChangesAsync();
1919

@@ -33,7 +33,7 @@ public async Task ShouldLoadBlogPost()
3333
[Fact]
3434
public async Task ShouldSaveBlogPost()
3535
{
36-
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, new[] { "Tag 1", "Tag 2" });
36+
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, tags: new[] { "Tag 1", "Tag 2" });
3737

3838
await BlogPostRepository.StoreAsync(blogPost);
3939

@@ -52,7 +52,7 @@ public async Task ShouldSaveBlogPost()
5252
[Fact]
5353
public async Task ShouldGetAllBlogPosts()
5454
{
55-
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, new[] { "Tag 1", "Tag 2" });
55+
var blogPost = BlogPost.Create("Title", "Subtitle", "Content", "url", true, tags: new[] { "Tag 1", "Tag 2" });
5656
await DbContext.BlogPosts.AddAsync(blogPost);
5757
await DbContext.SaveChangesAsync();
5858

LinkDotNet.Blog.TestUtilities/BlogPostBuilder.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LinkDotNet.Domain;
1+
using System;
2+
using LinkDotNet.Domain;
23

34
namespace LinkDotNet.Blog.TestUtilities
45
{
@@ -11,6 +12,7 @@ public class BlogPostBuilder
1112
private bool isPublished = true;
1213
private string[] tags;
1314
private int likes;
15+
private DateTime? updateDate;
1416

1517
public BlogPostBuilder WithTitle(string title)
1618
{
@@ -54,9 +56,15 @@ public BlogPostBuilder WithLikes(int likes)
5456
return this;
5557
}
5658

59+
public BlogPostBuilder WithUpdatedDate(DateTime updateDate)
60+
{
61+
this.updateDate = updateDate;
62+
return this;
63+
}
64+
5765
public BlogPost Build()
5866
{
59-
var blogPost = BlogPost.Create(title, shortDescription, content, url, isPublished, tags);
67+
var blogPost = BlogPost.Create(title, shortDescription, content, url, isPublished, updateDate, tags);
6068
blogPost.Likes = likes;
6169
return blogPost;
6270
}

LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using FluentAssertions;
34
using LinkDotNet.Blog.TestUtilities;
45
using LinkDotNet.Domain;
@@ -29,10 +30,20 @@ public void ShouldUpdateBlogPost()
2930
[Fact]
3031
public void ShouldTrimWhitespacesFromTags()
3132
{
32-
var blogPost = BlogPost.Create("Title", "Sub", "Content", "Preview", false, new[] { " Tag 1", " Tag 2 " });
33+
var blogPost = BlogPost.Create("Title", "Sub", "Content", "Preview", false, tags: new[] { " Tag 1", " Tag 2 ", });
3334

3435
blogPost.Tags.Select(t => t.Content).Should().Contain("Tag 1");
3536
blogPost.Tags.Select(t => t.Content).Should().Contain("Tag 2");
3637
}
38+
39+
[Fact]
40+
public void ShouldSetDateWhenGiven()
41+
{
42+
var somewhen = new DateTime(1991, 5, 17);
43+
44+
var blog = BlogPost.Create("1", "2", "3", "4", false, somewhen);
45+
46+
blog.UpdatedDate.Should().Be(somewhen);
47+
}
3748
}
3849
}

LinkDotNet.Blog.UnitTests/Web/Shared/Admin/CreateNewBlogPostTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using Bunit;
34
using FluentAssertions;
45
using LinkDotNet.Blog.TestUtilities;
@@ -95,5 +96,37 @@ public void ShouldNotDeleteModelWhenNotSet()
9596

9697
blogPost.Should().NotBeNull();
9798
}
99+
100+
[Fact]
101+
public void ShouldNotUpdateUpdatedDateWhenCheckboxSet()
102+
{
103+
var somewhen = new DateTime(1991, 5, 17);
104+
var originalBlogPost = new BlogPostBuilder().WithUpdatedDate(somewhen).Build();
105+
BlogPost blogPostFromComponent = null;
106+
var cut = RenderComponent<CreateNewBlogPost>(
107+
p =>
108+
p.Add(c => c.OnBlogPostCreated, bp => blogPostFromComponent = bp)
109+
.Add(c => c.BlogPost, originalBlogPost));
110+
111+
cut.Find("#title").Change("My Title");
112+
cut.Find("#short").Change("My short Description");
113+
cut.Find("#content").Change("My content");
114+
cut.Find("#preview").Change("My preview url");
115+
cut.Find("#tags").Change("Tag1,Tag2,Tag3");
116+
cut.Find("#updatedate").Change(false);
117+
cut.Find("form").Submit();
118+
119+
blogPostFromComponent.UpdatedDate.Should().Be(somewhen);
120+
}
121+
122+
[Fact]
123+
public void ShouldNotSetOptionToNotUpdateUpdatedDateOnInitialCreate()
124+
{
125+
var cut = RenderComponent<CreateNewBlogPost>();
126+
127+
var found = cut.FindAll("#updatedate");
128+
129+
found.Should().HaveCount(0);
130+
}
98131
}
99132
}

LinkDotNet.Blog.Web/Shared/Admin/CreateNewBlogPost.razor

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
<label for="tags">Tags</label>
3535
<InputText class="form-control" id="tags" @bind-Value="model.Tags"/>
3636
</div>
37+
@if (BlogPost != null)
38+
{
39+
<div class="form-check">
40+
<InputCheckbox class="form-check-input" id="updatedate" @bind-Value="model.ShouldUpdateDate" />
41+
<label class="form-check-label" for="updatedate">Update Publish Date?</label><br/>
42+
<small id="updatedate" class="form-text text-muted">If set the publish date is set to now, otherwise its original date</small>
43+
</div>
44+
}
3745
<button class="btn btn-primary" type="submit">Submit</button>
3846
</EditForm>
3947
</div>

LinkDotNet.Blog.Web/Shared/Admin/CreateNewModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ public class CreateNewModel
2424
[Required]
2525
public bool IsPublished { get; set; } = true;
2626

27+
[Required]
28+
public bool ShouldUpdateDate { get; set; } = true;
29+
2730
public string Tags { get; set; }
2831

32+
public DateTime OriginalUpdatedDate { get; set; }
33+
2934
public static CreateNewModel FromBlogPost(BlogPost blogPost)
3035
{
3136
return new CreateNewModel
@@ -37,14 +42,16 @@ public static CreateNewModel FromBlogPost(BlogPost blogPost)
3742
ShortDescription = blogPost.ShortDescription,
3843
IsPublished = blogPost.IsPublished,
3944
PreviewImageUrl = blogPost.PreviewImageUrl,
45+
OriginalUpdatedDate = blogPost.UpdatedDate,
4046
};
4147
}
4248

4349
public BlogPost ToBlogPost()
4450
{
4551
var tags = string.IsNullOrWhiteSpace(Tags) ? ArraySegment<string>.Empty : Tags.Split(",");
52+
DateTime? updatedDate = ShouldUpdateDate ? null : OriginalUpdatedDate;
4653

47-
var blogPost = BlogPost.Create(Title, ShortDescription, Content, PreviewImageUrl, IsPublished, tags);
54+
var blogPost = BlogPost.Create(Title, ShortDescription, Content, PreviewImageUrl, IsPublished, updatedDate, tags);
4855
blogPost.Id = Id;
4956
return blogPost;
5057
}

LinkDotNet.Domain/BlogPost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ public static BlogPost Create(
3434
string content,
3535
string previewImageUrl,
3636
bool isPublished,
37+
DateTime? updatedDate = null,
3738
IEnumerable<string> tags = null)
3839
{
3940
var blogPost = new BlogPost
4041
{
4142
Title = title,
4243
ShortDescription = shortDescription,
4344
Content = content,
44-
UpdatedDate = DateTime.Now,
45+
UpdatedDate = updatedDate ?? DateTime.Now,
4546
PreviewImageUrl = previewImageUrl,
4647
IsPublished = isPublished,
4748
Tags = tags?.Select(t => new Tag { Content = t.Trim() }).ToList(),

0 commit comments

Comments
 (0)