| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | | using YamlDotNet.Serialization; |
| | 5 | |
|
| | 6 | | namespace ApplicationCore.Model |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Represents a Comment to be written to the repository in YML format. |
| | 10 | | /// </summary> |
| | 11 | | public class Comment |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Valid characters when mapping from the blog post slug to a file path |
| | 15 | | /// </summary> |
| 1 | 16 | | static readonly Regex _validPathChars = new Regex(@"[^a-zA-Z0-9-]"); |
| | 17 | |
|
| | 18 | | [YamlIgnore] |
| 24 | 19 | | public string PostId { get; } |
| | 20 | |
|
| 5 | 21 | | public string Id { get; } |
| | 22 | |
|
| 23 | 23 | | public DateTime Date { get; } |
| | 24 | |
|
| 24 | 25 | | public string Name { get; } |
| | 26 | |
|
| 26 | 27 | | public string Message { get; } |
| | 28 | |
|
| 4 | 29 | | public string? Email { get; } |
| | 30 | |
|
| 8 | 31 | | public string Score { get; } |
| | 32 | |
|
| | 33 | | [YamlMember(SerializeAs = typeof(string))] |
| 6 | 34 | | public Uri? Avatar { get; } |
| | 35 | |
|
| | 36 | | [YamlMember(SerializeAs = typeof(string))] |
| 3 | 37 | | public Uri? Url { get; } |
| | 38 | |
|
| 19 | 39 | | public Comment( |
| 19 | 40 | | string postId, |
| 19 | 41 | | string message, |
| 19 | 42 | | string name, |
| 19 | 43 | | string? email = null, |
| 19 | 44 | | Uri? url = null, |
| 19 | 45 | | string? avatar = null, |
| 19 | 46 | | string? score = null) |
| 19 | 47 | | { |
| 19 | 48 | | PostId = _validPathChars.Replace(postId, "-"); |
| | 49 | |
|
| 19 | 50 | | Message = message; |
| 19 | 51 | | Name = name; |
| 19 | 52 | | Email = email; |
| 19 | 53 | | Url = url; |
| | 54 | |
|
| 19 | 55 | | Date = DateTime.UtcNow; |
| 19 | 56 | | Id = new { PostId, Name, Message, Date }.GetHashCode().ToString("x8", CultureInfo.InvariantCulture); |
| | 57 | |
|
| 19 | 58 | | if (Uri.TryCreate(avatar, UriKind.Absolute, out var avatarUrl)) |
| 1 | 59 | | Avatar = avatarUrl; |
| | 60 | |
|
| 19 | 61 | | Score = score ?? "Not configured"; |
| 19 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Copy constructor for internal use only |
| | 66 | | /// </summary> |
| 3 | 67 | | private Comment( |
| 3 | 68 | | string postId, |
| 3 | 69 | | string message, |
| 3 | 70 | | string name, |
| 3 | 71 | | string? email, |
| 3 | 72 | | Uri? url, |
| 3 | 73 | | DateTime date, |
| 3 | 74 | | string id, |
| 3 | 75 | | Uri? avatar, |
| 3 | 76 | | string? score) |
| 3 | 77 | | { |
| 3 | 78 | | PostId = postId; |
| 3 | 79 | | Message = message; |
| 3 | 80 | | Name = name; |
| 3 | 81 | | Email = email; |
| 3 | 82 | | Url = url; |
| 3 | 83 | | Date = date; |
| 3 | 84 | | Id = id; |
| 3 | 85 | | Avatar = avatar; |
| 3 | 86 | | Score = score ?? "Not configured"; |
| 3 | 87 | | } |
| | 88 | |
|
| | 89 | | public Comment WithScore(string score) |
| 3 | 90 | | { |
| 3 | 91 | | return new Comment(PostId, Message, Name, Email, Url, Date, Id, Avatar, score); |
| 3 | 92 | | } |
| | 93 | | } |
| | 94 | | } |