-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathBlogPost.cs
95 lines (74 loc) · 2.63 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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace LinkDotNet.Blog.Domain;
public sealed class BlogPost : Entity
{
private BlogPost()
{
}
public string Title { get; private set; }
public string ShortDescription { get; private set; }
public string Content { get; private set; }
public string PreviewImageUrl { get; private set; }
public string PreviewImageUrlFallback { get; private set; }
public DateTime UpdatedDate { get; private set; }
public DateTime? ScheduledPublishDate { get; private set; }
public IReadOnlyCollection<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 => Tags is null ? string.Empty : string.Join(", ", Tags);
public static BlogPost Create(
string title,
string shortDescription,
string content,
string previewImageUrl,
bool isPublished,
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(),
};
return blogPost;
}
public void Publish()
{
ScheduledPublishDate = null;
IsPublished = true;
}
public void Update(BlogPost 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;
Tags = from.Tags;
}
}