-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathBlogPost.cs
152 lines (119 loc) · 4.52 KB
/
BlogPost.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace LinkDotNet.Blog.Domain;
public sealed partial class BlogPost : Entity
{
public string Title { get; private set; } = default!;
public string ShortDescription { get; private set; } = default!;
public string Content { get; private set; } = default!;
public string PreviewImageUrl { get; private set; } = default!;
public string? PreviewImageUrlFallback { get; private set; }
public DateTime UpdatedDate { get; private set; }
public DateTime? ScheduledPublishDate { get; private set; }
public IList<string> Tags { get; private set; } = [];
public bool IsPublished { get; private set; }
public int Likes { get; set; }
public bool IsScheduled => ScheduledPublishDate is not null;
public string TagsAsString => string.Join(",", Tags);
public int ReadingTimeInMinutes { get; private set; }
public bool IsMembersOnly { get; private set; }
public string Slug => GenerateSlug();
private string GenerateSlug()
{
if (string.IsNullOrWhiteSpace(Title))
{
return Title;
}
var normalizedTitle = Title.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var c in normalizedTitle.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark))
{
stringBuilder.Append(c);
}
var cleanTitle = stringBuilder
.ToString()
.Normalize(NormalizationForm.FormC)
.ToLower(CultureInfo.CurrentCulture);
cleanTitle = MatchIfSpecialCharactersExist().Replace(cleanTitle, "");
cleanTitle = MatchIfAdditionalSpacesExist().Replace(cleanTitle, " ");
cleanTitle = MatchIfSpaceExist().Replace(cleanTitle, "-");
return cleanTitle.Trim();
}
[GeneratedRegex(
@"[^A-Za-z0-9\s]",
RegexOptions.CultureInvariant,
matchTimeoutMilliseconds: 1000)]
private static partial Regex MatchIfSpecialCharactersExist();
[GeneratedRegex(
@"\s+",
RegexOptions.CultureInvariant,
matchTimeoutMilliseconds: 1000)]
private static partial Regex MatchIfAdditionalSpacesExist();
[GeneratedRegex(
@"\s",
RegexOptions.CultureInvariant,
matchTimeoutMilliseconds: 1000)]
private static partial Regex MatchIfSpaceExist();
public static BlogPost Create(
string title,
string shortDescription,
string content,
string previewImageUrl,
bool isPublished,
bool isMembersOnly,
DateTime? updatedDate = null,
DateTime? scheduledPublishDate = null,
IEnumerable<string>? tags = null,
string? previewImageUrlFallback = null)
{
if (scheduledPublishDate is not null && isPublished)
{
throw new InvalidOperationException("Can't schedule publish date if the blog post is already published.");
}
var blogPostUpdateDate = scheduledPublishDate ?? updatedDate ?? DateTime.Now;
var blogPost = new BlogPost
{
Title = title,
ShortDescription = shortDescription,
Content = content,
UpdatedDate = blogPostUpdateDate,
ScheduledPublishDate = scheduledPublishDate,
PreviewImageUrl = previewImageUrl,
PreviewImageUrlFallback = previewImageUrlFallback,
IsPublished = isPublished,
Tags = tags?.Select(t => t.Trim()).ToImmutableArray() ?? [],
ReadingTimeInMinutes = ReadingTimeCalculator.CalculateReadingTime(content),
IsMembersOnly = isMembersOnly,
};
return blogPost;
}
public void Publish()
{
ScheduledPublishDate = null;
IsPublished = true;
}
public void Update(BlogPost from)
{
ArgumentNullException.ThrowIfNull(from);
if (from == this)
{
return;
}
Title = from.Title;
ShortDescription = from.ShortDescription;
Content = from.Content;
UpdatedDate = from.UpdatedDate;
ScheduledPublishDate = from.ScheduledPublishDate;
PreviewImageUrl = from.PreviewImageUrl;
PreviewImageUrlFallback = from.PreviewImageUrlFallback;
IsPublished = from.IsPublished;
IsMembersOnly = from.IsMembersOnly;
Tags = from.Tags;
ReadingTimeInMinutes = from.ReadingTimeInMinutes;
}
}