Skip to content

Commit 4e070bf

Browse files
committed
Fixed bug where tags where added multiple times
1 parent 9e842be commit 4e070bf

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ public void Update(BlogPost from)
5757
UpdatedDate = from.UpdatedDate;
5858
PreviewImageUrl = from.PreviewImageUrl;
5959
IsPublished = from.IsPublished;
60-
Tags = from.Tags;
60+
ReplaceTags(from.Tags);
61+
}
62+
63+
private void ReplaceTags(IEnumerable<Tag> tags)
64+
{
65+
Tags?.Clear();
66+
if (Tags == null || tags == null)
67+
{
68+
Tags = tags?.ToList();
69+
}
70+
else
71+
{
72+
foreach (var tag in tags)
73+
{
74+
Tags.Add(tag);
75+
}
76+
}
6177
}
6278
}
6379
}

LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ public void ShouldUpdateBlogPost()
2727
blogPostToUpdate.Tags.Should().BeNullOrEmpty();
2828
}
2929

30+
[Fact]
31+
public void ShouldUpdateTagsWhenExisting()
32+
{
33+
var blogPostToUpdate = new BlogPostBuilder().WithTags("tag 1").Build();
34+
blogPostToUpdate.Id = "random-id";
35+
var blogPost = new BlogPostBuilder().WithTags("tag 2").Build();
36+
blogPost.Id = "something else";
37+
38+
blogPostToUpdate.Update(blogPost);
39+
40+
blogPostToUpdate.Tags.Should().HaveCount(1);
41+
blogPostToUpdate.Tags.Single().Content.Should().Be("tag 2");
42+
}
43+
3044
[Fact]
3145
public void ShouldTrimWhitespacesFromTags()
3246
{

LinkDotNet.Blog.Web/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace LinkDotNet.Blog.Web
55
{
6-
public class Program
6+
public static class Program
77
{
88
public static void Main(string[] args)
99
{

0 commit comments

Comments
 (0)