Skip to content

Commit db23467

Browse files
Add author name in blog post (#428)
* Add author name in blog post entity * Add IsMultiModeEnabled config * Add author name in the claim at the time of login, if multi mode is enabled * Save author name in the db at the time of blog post create and update * Show author name in the top menu if multi mode is enabled * Update tests * Rename config (address review comments) * Remove if else from tests (address review comments) * Add if in the razor view (address review comments) * Add ef migration for author name (address review comments) * Use bootstrap 5x (address review comments) * add test for RavenDb (i forgot this before) * Address review comments * Address review comments * Update config doc * Show author name in blog preview * Show author name in read blog post page * Show author name in top menu * Add icon
1 parent e99c7bd commit db23467

File tree

35 files changed

+1157
-44
lines changed

35 files changed

+1157
-44
lines changed

docs/Migrations/Readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ This is contrasted by Minor changes. These are things where the user does not ne
66

77
Breaking changes are recorded in the [MIGRATION.md](../../MIGRATION.md). Since version 9 of the blog, “Entity Framework Migrations” has been introduced for all SQL providers. You can read more in the [documentation](../Storage/Readme.md). In a nutshell, this means that database migration can be carried out easily via the “ef migration” CLI tool. More on this in the documentation linked above.
88

9-
Changes for the appsettings.json must currently still be made manually. The exact changes that need to be made here can be found in MIGRATION.md.
9+
Changes for the appsettings.json must currently still be made manually. The exact changes that need to be made here can be found in MIGRATION.md.
10+
11+
## UNRELEASED
12+
13+
A new config has been added `UseMultiAuthorMode` in `appsettings.json`. The default value of this config is `false`. If set to `true` then author name will be associated with blog posts at the time of creation.

docs/Setup/Configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ The appsettings.json file has a lot of options to customize the content of the b
6565
"ServiceUrl": "",
6666
"ContainerName": "",
6767
"CdnEndpoint": ""
68-
}
68+
},
69+
"UseMultiAuthorMode": false
6970
}
7071
```
7172

@@ -109,3 +110,4 @@ The appsettings.json file has a lot of options to customize the content of the b
109110
| ServiceUrl | string | The host url of the Azure blob storage. Only used if `AuthenticationMode` is set to `Default` |
110111
| ContainerName | string | The container name for the image storage provider |
111112
| CdnEndpoint | string | Optional CDN endpoint to use for uploaded images. If set, the blog will return this URL instead of the storage account URL for uploaded assets. |
113+
| UseMultiAuthorMode | boolean | The default value is `false`. If set to `true` then author name will be associated with blog posts at the time of creation. This author name will be fetched from the identity provider's `name` or `nickname` or `preferred_username` claim property. |

src/LinkDotNet.Blog.Domain/BlogPost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public sealed partial class BlogPost : Entity
3838

3939
public string Slug => GenerateSlug();
4040

41+
public string? AuthorName { get; private set; }
42+
4143
private string GenerateSlug()
4244
{
4345
if (string.IsNullOrWhiteSpace(Title))
@@ -92,7 +94,8 @@ public static BlogPost Create(
9294
DateTime? updatedDate = null,
9395
DateTime? scheduledPublishDate = null,
9496
IEnumerable<string>? tags = null,
95-
string? previewImageUrlFallback = null)
97+
string? previewImageUrlFallback = null,
98+
string? authorName = null)
9699
{
97100
if (scheduledPublishDate is not null && isPublished)
98101
{
@@ -113,6 +116,7 @@ public static BlogPost Create(
113116
IsPublished = isPublished,
114117
Tags = tags?.Select(t => t.Trim()).ToImmutableArray() ?? [],
115118
ReadingTimeInMinutes = ReadingTimeCalculator.CalculateReadingTime(content),
119+
AuthorName = authorName
116120
};
117121

118122
return blogPost;
@@ -143,5 +147,6 @@ public void Update(BlogPost from)
143147
IsPublished = from.IsPublished;
144148
Tags = from.Tags;
145149
ReadingTimeInMinutes = from.ReadingTimeInMinutes;
150+
AuthorName = from.AuthorName;
146151
}
147152
}

src/LinkDotNet.Blog.Infrastructure/Migrations/20250830110439_AddAuthorNameInBlogPost.Designer.cs

Lines changed: 253 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
namespace LinkDotNet.Blog.Web.Migrations;
7+
8+
/// <inheritdoc />
9+
public partial class AddAuthorNameInBlogPost : Migration
10+
{
11+
/// <inheritdoc />
12+
protected override void Up(MigrationBuilder migrationBuilder)
13+
{
14+
ArgumentNullException.ThrowIfNull(migrationBuilder);
15+
16+
migrationBuilder.AddColumn<string>(
17+
name: "AuthorName",
18+
table: "BlogPosts",
19+
type: "nvarchar(256)",
20+
maxLength: 256,
21+
nullable: true);
22+
}
23+
24+
/// <inheritdoc />
25+
protected override void Down(MigrationBuilder migrationBuilder)
26+
{
27+
ArgumentNullException.ThrowIfNull(migrationBuilder);
28+
29+
migrationBuilder.DropColumn(
30+
name: "AuthorName",
31+
table: "BlogPosts");
32+
}
33+
}

src/LinkDotNet.Blog.Infrastructure/Migrations/BlogDbContextModelSnapshot.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <auto-generated />
1+
// <auto-generated />
22
using System;
33
using LinkDotNet.Blog.Infrastructure.Persistence.Sql;
44
using Microsoft.EntityFrameworkCore;
@@ -24,6 +24,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
2424
.IsUnicode(false)
2525
.HasColumnType("TEXT");
2626

27+
b.Property<string>("AuthorName")
28+
.HasMaxLength(256)
29+
.HasColumnType("nvarchar(256)");
30+
2731
b.Property<string>("Content")
2832
.IsRequired()
2933
.HasColumnType("TEXT");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void Configure(EntityTypeBuilder<BlogPost> builder)
2121
builder.Property(x => x.IsPublished).IsRequired();
2222

2323
builder.Property(x => x.Tags).HasMaxLength(2048);
24+
builder.Property(x => x.AuthorName).HasMaxLength(256).IsRequired(false);
2425

2526
builder.HasIndex(x => new { x.IsPublished, x.UpdatedDate })
2627
.HasDatabaseName("IX_BlogPosts_IsPublished_UpdatedDate")

src/LinkDotNet.Blog.Web/ApplicationConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public sealed record ApplicationConfiguration
2323
public bool ShowReadingIndicator { get; init; }
2424

2525
public bool ShowSimilarPosts { get; init; }
26+
27+
public bool UseMultiAuthorMode { get; init; }
2628
}

src/LinkDotNet.Blog.Web/Authentication/Dummy/DummyLoginManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Security.Claims;
33
using System.Threading.Tasks;
44
using Microsoft.AspNetCore.Authentication;
@@ -27,6 +27,7 @@ public async Task SignInAsync(string redirectUri)
2727
var claims = new[]
2828
{
2929
new Claim(ClaimTypes.Name, "Dummy user"),
30+
new Claim("name", "Dummy user"),
3031
new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
3132
};
3233
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

0 commit comments

Comments
 (0)