Skip to content

Commit

Permalink
Put logic to domain object
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Apr 29, 2023
1 parent 7ffbd31 commit 63e7857
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/LinkDotNet.Blog.Domain/BlogPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ private BlogPost()

public bool IsScheduled => ScheduledPublishDate is not null;

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

public static BlogPost Create(
string title,
string shortDescription,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static CreateNewModel FromBlogPost(BlogPost blogPost)
{
id = blogPost.Id,
Content = blogPost.Content,
Tags = blogPost.Tags != null ? string.Join(",", blogPost.Tags.Select(t => t.Content)) : null,
Tags = blogPost.TagsAsString,
Title = blogPost.Title,
ShortDescription = blogPost.ShortDescription,
IsPublished = blogPost.IsPublished,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ else
<OgData Title="@BlogPost.Title"
AbsolutePreviewImageUrl="@OgDataImage"
Description="@(Markdown.ToPlainText(BlogPost.ShortDescription))"
Keywords="@Tags"></OgData>
Keywords="@BlogPost.TagsAsString"></OgData>
<div class="d-flex justify-content-center pt-2 blog-outer-box">
<div class="content blog-container">
<div class="blog-inner-content">
Expand Down Expand Up @@ -65,11 +65,7 @@ else
[Parameter]
public string BlogPostId { get; set; }

private string Tags => BlogPost?.Tags != null
? string.Join(",", BlogPost.Tags.Select(b => b.Content))
: null;

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

private BlogPost BlogPost { get; set; }

Expand Down
20 changes: 20 additions & 0 deletions tests/LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,24 @@ public void GivenScheduledPublishDate_WhenCreating_ThenIsScheduledPublishDateIsT

bp.IsScheduled.Should().BeTrue();
}

[Fact]
public void GivenBlogPostWithTags_WhenCreatingStringFromTags_ThenTagsAreSeparatedByComma()
{
var bp = new BlogPostBuilder().WithTags("tag 1", "tag 2").Build();

var tags = bp.TagsAsString;

tags.Should().Be("tag 1, tag 2");
}

[Fact]
public void GivenBlogPostWithNoTags_WhenCreatingStringFromTags_ThenEmptyString()
{
var bp = new BlogPostBuilder().Build();

var tags = bp.TagsAsString;

tags.Should().BeEmpty();
}
}

0 comments on commit 63e7857

Please sign in to comment.