-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCreateNewModel.cs
More file actions
58 lines (45 loc) · 1.64 KB
/
Copy pathCreateNewModel.cs
File metadata and controls
58 lines (45 loc) · 1.64 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
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using LinkDotNet.Blog.Domain;
namespace LinkDotNet.Blog.Web.Features.Admin.BlogPostEditor.Components;
public class CreateNewModel
{
public string Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string ShortDescription { get; set; }
[Required]
public string Content { get; set; }
[Required]
public string PreviewImageUrl { get; set; }
[Required]
public bool IsPublished { get; set; } = true;
[Required]
public bool ShouldUpdateDate { get; set; } = true;
public string Tags { get; set; }
public DateTime OriginalUpdatedDate { get; set; }
public static CreateNewModel FromBlogPost(BlogPost blogPost)
{
return new CreateNewModel
{
Id = blogPost.Id,
Content = blogPost.Content,
Tags = blogPost.Tags != null ? string.Join(",", blogPost.Tags.Select(t => t.Content)) : null,
Title = blogPost.Title,
ShortDescription = blogPost.ShortDescription,
IsPublished = blogPost.IsPublished,
PreviewImageUrl = blogPost.PreviewImageUrl,
OriginalUpdatedDate = blogPost.UpdatedDate,
};
}
public BlogPost ToBlogPost()
{
var tags = string.IsNullOrWhiteSpace(Tags) ? ArraySegment<string>.Empty : Tags.Split(",");
DateTime? updatedDate = ShouldUpdateDate ? null : OriginalUpdatedDate;
var blogPost = BlogPost.Create(Title, ShortDescription, Content, PreviewImageUrl, IsPublished, updatedDate, tags);
blogPost.Id = Id;
return blogPost;
}
}