Skip to content

Commit afb8824

Browse files
committed
Use OPEN_JSON for persisting tags
1 parent f7771ad commit afb8824

File tree

18 files changed

+59
-121
lines changed

18 files changed

+59
-121
lines changed

src/LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Linq;
45

56
namespace LinkDotNet.Blog.Domain;
@@ -24,15 +25,15 @@ private BlogPost()
2425

2526
public DateTime? ScheduledPublishDate { get; private set; }
2627

27-
public ICollection<Tag> Tags { get; private set; }
28+
public ICollection<string> Tags { get; private set; }
2829

2930
public bool IsPublished { get; private set; }
3031

3132
public int Likes { get; set; }
3233

3334
public bool IsScheduled => ScheduledPublishDate is not null;
3435

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

3738
public static BlogPost Create(
3839
string title,
@@ -62,7 +63,7 @@ public static BlogPost Create(
6263
PreviewImageUrl = previewImageUrl,
6364
PreviewImageUrlFallback = previewImageUrlFallback,
6465
IsPublished = isPublished,
65-
Tags = tags?.Select(Tag.Create).ToList(),
66+
Tags = tags?.Select(t => t.Trim()).ToImmutableArray(),
6667
};
6768

6869
return blogPost;
@@ -89,22 +90,6 @@ public void Update(BlogPost from)
8990
PreviewImageUrl = from.PreviewImageUrl;
9091
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
9192
IsPublished = from.IsPublished;
92-
ReplaceTags(from.Tags);
93-
}
94-
95-
private void ReplaceTags(IEnumerable<Tag> tags)
96-
{
97-
Tags?.Clear();
98-
if (Tags == null || tags == null)
99-
{
100-
Tags = tags?.ToList();
101-
}
102-
else
103-
{
104-
foreach (var tag in tags)
105-
{
106-
Tags.Add(tag);
107-
}
108-
}
93+
Tags = from.Tags;
10994
}
11095
}

src/LinkDotNet.Blog.Domain/Tag.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/BlogDbContext.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public BlogDbContext(DbContextOptions options)
1414

1515
public DbSet<BlogPost> BlogPosts { get; set; }
1616

17-
public DbSet<Tag> Tags { get; set; }
18-
1917
public DbSet<ProfileInformationEntry> ProfileInformationEntries { get; set; }
2018

2119
public DbSet<Skill> Skills { get; set; }
@@ -27,7 +25,6 @@ public BlogDbContext(DbContextOptions options)
2725
protected override void OnModelCreating(ModelBuilder modelBuilder)
2826
{
2927
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
30-
modelBuilder.ApplyConfiguration(new TagsConfiguration());
3128
modelBuilder.ApplyConfiguration(new ProfileInformationEntryConfiguration());
3229
modelBuilder.ApplyConfiguration(new SkillConfiguration());
3330
modelBuilder.ApplyConfiguration(new UserRecordConfiguration());

src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/Mapping/BlogPostConfiguration.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
1010
{
1111
builder.HasKey(c => c.Id);
1212
builder.Property(c => c.Id).ValueGeneratedOnAdd();
13-
14-
builder.HasMany(t => t.Tags)
15-
.WithOne()
16-
.OnDelete(DeleteBehavior.Cascade);
17-
builder.Navigation(x => x.Tags).AutoInclude();
1813
builder.Property(x => x.Title).HasMaxLength(256).IsRequired();
1914
builder.Property(x => x.PreviewImageUrl).HasMaxLength(1024).IsRequired();
2015
builder.Property(x => x.PreviewImageUrlFallback).HasMaxLength(1024);

src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/Mapping/TagsConfiguration.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/LinkDotNet.Blog.Infrastructure/Persistence/Sql/Repository.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,21 @@ public async ValueTask StoreAsync(TEntity entity)
6969
}
7070
else
7171
{
72-
blogDbContext.Entry(entity).State = EntityState.Modified;
72+
var dbEntity = blogDbContext.Set<TEntity>().Local.FirstOrDefault(e => e.Id == entity.Id);
73+
if (dbEntity != null)
74+
{
75+
blogDbContext.Entry(dbEntity).CurrentValues.SetValues(entity);
76+
blogDbContext.Entry(dbEntity).State = EntityState.Modified;
77+
}
78+
else
79+
{
80+
blogDbContext.Entry(entity).State = EntityState.Modified;
81+
}
7382
}
7483

7584
await blogDbContext.SaveChangesAsync();
7685
}
77-
86+
7887
public async ValueTask DeleteAsync(string id)
7988
{
8089
var entityToDelete = await GetByIdAsync(id);

src/LinkDotNet.Blog.Web/Controller/RssFeedController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ private static SyndicationItem CreateSyndicationItemFromBlogPost(string url, Blo
8686

8787
private static void AddCategories(ICollection<SyndicationCategory> categories, BlogPostRssInfo blogPost)
8888
{
89-
foreach (var tag in blogPost.Tags ?? Array.Empty<Tag>())
89+
foreach (var tag in blogPost.Tags ?? Array.Empty<string>())
9090
{
91-
categories.Add(new SyndicationCategory(tag.Content));
91+
categories.Add(new SyndicationCategory(tag));
9292
}
9393
}
9494

@@ -107,5 +107,5 @@ private sealed record BlogPostRssInfo(
107107
string ShortDescription,
108108
DateTime UpdatedDate,
109109
string PreviewImageUrl,
110-
ICollection<Tag> Tags);
110+
ICollection<string> Tags);
111111
}

src/LinkDotNet.Blog.Web/Features/Admin/Sitemap/Services/SitemapService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ private IEnumerable<SitemapUrl> CreateUrlsForTags(IEnumerable<BlogPost> blogPost
5757
{
5858
return blogPosts
5959
.SelectMany(b => b.Tags)
60-
.Select(t => t.Content)
6160
.Distinct()
6261
.Select(t => new SitemapUrl
6362
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{
2929
<li class="tags me-4">
3030
<ul>
31-
@foreach (var tag in BlogPost.Tags.Select(t => t.Content))
31+
@foreach (var tag in BlogPost.Tags)
3232
{
3333
<li><a class="goto-tag" href="/searchByTag/@(Uri.EscapeDataString(tag))">@tag</a></li>
3434
}

src/LinkDotNet.Blog.Web/Features/Search/SearchPage.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
{
2525
var term = Uri.UnescapeDataString(SearchTerm);
2626
blogPosts = await BlogPostRepository.GetAllAsync(t => t.IsPublished && (t.Title.Contains(term)
27-
|| t.Tags.Any(tag => tag.Content == term)),
27+
|| t.Tags.Any(tag => tag == term)),
2828
b => b.UpdatedDate);
2929
}
3030

0 commit comments

Comments
 (0)