-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCreateNewModel.cs
More file actions
144 lines (125 loc) · 3.49 KB
/
Copy pathCreateNewModel.cs
File metadata and controls
144 lines (125 loc) · 3.49 KB
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
using System;
using System.ComponentModel.DataAnnotations;
using LinkDotNet.Blog.Domain;
namespace LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
public sealed class CreateNewModel
{
private DateTime originalUpdatedDate;
private string id = default!;
private DateTime? scheduledPublishDate;
private string? authorName;
[Required]
[MaxLength(256)]
public string Title
{
get;
set => SetProperty(out field, value);
} = string.Empty;
[Required]
public string ShortDescription
{
get;
set => SetProperty(out field, value);
} = string.Empty;
[Required]
public string Content
{
get;
set => SetProperty(out field, value);
} = string.Empty;
[Required]
[MaxLength(1024)]
public string PreviewImageUrl
{
get;
set => SetProperty(out field, value);
} = string.Empty;
[Required]
[PublishedWithScheduledDateValidation]
public bool IsPublished
{
get;
set => SetProperty(out field, value);
} = true;
[Required]
public bool ShouldUpdateDate
{
get;
set => SetProperty(out field, value);
}
[FutureDateValidation]
public DateTime? ScheduledPublishDate
{
get => scheduledPublishDate;
set => SetProperty(out scheduledPublishDate, value);
}
public string Tags
{
get;
set => SetProperty(out field, value);
} = string.Empty;
[MaxLength(256)]
[FallbackUrlValidation]
public string PreviewImageUrlFallback
{
get;
set => SetProperty(out field, value);
} = string.Empty;
public bool ShouldInvalidateCache
{
get;
set => SetProperty(out field, value);
}
public string? AuthorName
{
get => authorName;
set => SetProperty(out authorName, value);
}
public bool IsDirty { get; private set; }
public static CreateNewModel FromBlogPost(BlogPost blogPost)
{
ArgumentNullException.ThrowIfNull(blogPost);
return new CreateNewModel
{
id = blogPost.Id,
Content = blogPost.Content,
Tags = blogPost.TagsAsString,
Title = blogPost.Title,
ShortDescription = blogPost.ShortDescription,
IsPublished = blogPost.IsPublished,
PreviewImageUrl = blogPost.PreviewImageUrl,
originalUpdatedDate = blogPost.UpdatedDate,
PreviewImageUrlFallback = blogPost.PreviewImageUrlFallback ?? string.Empty,
scheduledPublishDate = blogPost.ScheduledPublishDate?.ToUniversalTime(),
authorName = blogPost.AuthorName,
IsDirty = false,
};
}
public BlogPost ToBlogPost()
{
var tagList = string.IsNullOrWhiteSpace(Tags)
? []
: Tags.Split(",", StringSplitOptions.RemoveEmptyEntries);
DateTime? updatedDate = ShouldUpdateDate || originalUpdatedDate == default
? null
: originalUpdatedDate;
var blogPost = BlogPost.Create(
Title,
ShortDescription,
Content,
PreviewImageUrl,
IsPublished,
updatedDate,
scheduledPublishDate,
tagList,
PreviewImageUrlFallback,
AuthorName);
blogPost.Id = id;
return blogPost;
}
private void SetProperty<T>(out T backingField, T value)
{
backingField = value;
IsDirty = true;
}
}