| | | 1 | | using RoleRadar.Domain.Common; |
| | | 2 | | |
| | | 3 | | namespace RoleRadar.Domain.Notes; |
| | | 4 | | |
| | | 5 | | public sealed class NoteRevision |
| | | 6 | | { |
| | | 7 | | public const string ReasonReplaceSection = "replace-section"; |
| | | 8 | | public const string ReasonImportOverwrite = "import-overwrite"; |
| | | 9 | | |
| | 44 | 10 | | public Guid Id { get; } |
| | | 11 | | |
| | 20 | 12 | | public string OwnerId { get; } |
| | | 13 | | |
| | 20 | 14 | | public Guid NoteId { get; } |
| | | 15 | | |
| | 32 | 16 | | public int RevisionNumber { get; } |
| | | 17 | | |
| | 22 | 18 | | public string Reason { get; } |
| | | 19 | | |
| | 34 | 20 | | public string PriorBody { get; } |
| | | 21 | | |
| | 20 | 22 | | public string PriorBodyHash { get; } |
| | | 23 | | |
| | 20 | 24 | | public Guid PriorVersionToken { get; } |
| | | 25 | | |
| | 20 | 26 | | public DateTime CreatedAtUtc { get; } |
| | | 27 | | |
| | 36 | 28 | | private NoteRevision( |
| | 36 | 29 | | Guid id, |
| | 36 | 30 | | string ownerId, |
| | 36 | 31 | | Guid noteId, |
| | 36 | 32 | | int revisionNumber, |
| | 36 | 33 | | string reason, |
| | 36 | 34 | | string priorBody, |
| | 36 | 35 | | string priorBodyHash, |
| | 36 | 36 | | Guid priorVersionToken, |
| | 36 | 37 | | DateTime createdAtUtc) |
| | | 38 | | { |
| | 36 | 39 | | Id = id; |
| | 36 | 40 | | OwnerId = ownerId; |
| | 36 | 41 | | NoteId = noteId; |
| | 36 | 42 | | RevisionNumber = revisionNumber; |
| | 36 | 43 | | Reason = reason; |
| | 36 | 44 | | PriorBody = priorBody; |
| | 36 | 45 | | PriorBodyHash = priorBodyHash; |
| | 36 | 46 | | PriorVersionToken = priorVersionToken; |
| | 36 | 47 | | CreatedAtUtc = EnsureUtc(createdAtUtc); |
| | 36 | 48 | | } |
| | | 49 | | |
| | | 50 | | public static NoteRevision Create( |
| | | 51 | | string ownerId, |
| | | 52 | | Guid noteId, |
| | | 53 | | int revisionNumber, |
| | | 54 | | string reason, |
| | | 55 | | string priorBody, |
| | | 56 | | Guid priorVersionToken, |
| | | 57 | | DateTime createdAtUtc) |
| | | 58 | | { |
| | 22 | 59 | | var validOwner = new OwnerId(ownerId).Value; |
| | 22 | 60 | | if (noteId == Guid.Empty) |
| | | 61 | | { |
| | 0 | 62 | | throw new ArgumentException("Note identifier cannot be empty.", nameof(noteId)); |
| | | 63 | | } |
| | | 64 | | |
| | 22 | 65 | | if (revisionNumber < 1) |
| | | 66 | | { |
| | 0 | 67 | | throw new ArgumentOutOfRangeException(nameof(revisionNumber), "Revision number must be >= 1."); |
| | | 68 | | } |
| | | 69 | | |
| | 22 | 70 | | if (reason is not (ReasonReplaceSection or ReasonImportOverwrite)) |
| | | 71 | | { |
| | 0 | 72 | | throw new ArgumentException( |
| | 0 | 73 | | $"Revision reason must be '{ReasonReplaceSection}' or '{ReasonImportOverwrite}'.", |
| | 0 | 74 | | nameof(reason)); |
| | | 75 | | } |
| | | 76 | | |
| | 22 | 77 | | ArgumentNullException.ThrowIfNull(priorBody); |
| | 22 | 78 | | if (priorVersionToken == Guid.Empty) |
| | | 79 | | { |
| | 0 | 80 | | throw new ArgumentException("Prior version token cannot be empty.", nameof(priorVersionToken)); |
| | | 81 | | } |
| | | 82 | | |
| | 22 | 83 | | var priorBodyHash = Sha256Hasher.ComputeLowerHex(priorBody); |
| | | 84 | | |
| | 22 | 85 | | return new NoteRevision( |
| | 22 | 86 | | Guid.CreateVersion7(), |
| | 22 | 87 | | validOwner, |
| | 22 | 88 | | noteId, |
| | 22 | 89 | | revisionNumber, |
| | 22 | 90 | | reason, |
| | 22 | 91 | | priorBody, |
| | 22 | 92 | | priorBodyHash, |
| | 22 | 93 | | priorVersionToken, |
| | 22 | 94 | | EnsureUtc(createdAtUtc)); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public static NoteRevision Rehydrate( |
| | | 98 | | Guid id, |
| | | 99 | | string ownerId, |
| | | 100 | | Guid noteId, |
| | | 101 | | int revisionNumber, |
| | | 102 | | string reason, |
| | | 103 | | string priorBody, |
| | | 104 | | string priorBodyHash, |
| | | 105 | | Guid priorVersionToken, |
| | | 106 | | DateTime createdAtUtc) => |
| | 14 | 107 | | new(id, ownerId, noteId, revisionNumber, reason, priorBody, priorBodyHash, priorVersionToken, createdAtUtc); |
| | | 108 | | |
| | | 109 | | private static DateTime EnsureUtc(DateTime value) => |
| | 58 | 110 | | value.Kind == DateTimeKind.Utc ? value : DateTime.SpecifyKind(value, DateTimeKind.Utc); |
| | | 111 | | } |