From c90f1d8d9427703db3c27b698a1d535ac3975520 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 13 Nov 2021 20:27:11 -0600 Subject: [PATCH] =?UTF-8?q?refactor(dir):=20=E2=9C=A8=F0=9F=9A=A7=20use=20?= =?UTF-8?q?TPH=20for=20Change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CreateList.cs | 6 +-- .../Aggregates/Changes/Change.cs | 53 +++++-------------- .../Aggregates/Changes/FilterListChange.cs | 35 ++++++++++++ .../Aggregates/Changes/IChangeAggregate.cs | 8 +++ .../Aggregates/FilterLists/FilterList.cs | 4 +- .../Aggregates/IAggregate.cs | 5 ++ .../Commands/Context/CommandDbContext.cs | 2 +- .../Commands/Context/ICommandContext.cs | 2 +- 8 files changed, 68 insertions(+), 47 deletions(-) create mode 100644 services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/FilterListChange.cs create mode 100644 services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/IChangeAggregate.cs create mode 100644 services/Directory/FilterLists.Directory.Domain/Aggregates/IAggregate.cs diff --git a/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs b/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs index 341ca74b2..66363c45e 100644 --- a/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs +++ b/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs @@ -64,10 +64,10 @@ public async Task Handle(Command request, CancellationToken cancellati request.EmailAddress, request.DonateUrl, request.ViewUrls); - _commandContext.FilterLists.Add(filterList); + //_commandContext.FilterLists.Add(filterList); - var change = Change.CreateFilterList(filterList, request.ChangeReason); - _commandContext.Changes.Add(change); + var change = FilterListChange.Create(filterList, request.ChangeReason); + _commandContext.FilterListChanges.Add(change); await _commandContext.SaveChangesAsync(cancellationToken); diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/Change.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/Change.cs index 0a03e8eb7..68a09bbc9 100644 --- a/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/Change.cs +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/Change.cs @@ -1,48 +1,21 @@ -using System.Text.Json; -using FilterLists.Directory.Domain.Aggregates.FilterLists; +namespace FilterLists.Directory.Domain.Aggregates.Changes; -namespace FilterLists.Directory.Domain.Aggregates.Changes; - -public class Change +public abstract class Change { - private Change() - { - } - - public ChangeType Type { get; init; } - - // TODO: push json handling from Domain to Infrastructure - public JsonDocument? AggregateBefore { get; init; } - public JsonDocument? AggregateAfter { get; init; } - public string? Reason { get; init; } public DateTime SubmittedAt { get; init; } = DateTime.UtcNow; - public DateTime? AppliedAt { get; init; } - public DateTime? RejectedAt { get; init; } - public string? RejectedReason { get; init; } - //public int? FilterListId { get; init; } - public FilterList? FilterList { get; init; } - //public string? LanguageIso6391 { get; init; } - //public Language? Language { get; } - //public int? LicenseId { get; init; } - //public License? License { get; } - //public int? MaintainerId { get; init; } - //public Maintainer? Maintainer { get; } - //public int? SoftwareId { get; init; } - //public Software? Software { get; } - //public int? SyntaxId { get; init; } - //public Syntax? Syntax { get; } - //public int? TagId { get; init; } - //public Tag? Tag { get; } + public DateTime? AppliedAt { get; private set; } + public DateTime? RejectedAt { get; private set; } + public string? RejectedReason { get; private set; } - public static Change CreateFilterList(FilterList filterList, string? reason) + public void Approve() { - return new Change - { - Type = ChangeType.Create, - AggregateAfter = JsonSerializer.SerializeToDocument(filterList), - Reason = reason, - FilterList = filterList - }; + AppliedAt = DateTime.UtcNow; + } + + public void Reject(string? reason) + { + RejectedAt = DateTime.UtcNow; + RejectedReason = reason; } } diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/FilterListChange.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/FilterListChange.cs new file mode 100644 index 000000000..6f63d591b --- /dev/null +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/FilterListChange.cs @@ -0,0 +1,35 @@ +using FilterLists.Directory.Domain.Aggregates.FilterLists; + +namespace FilterLists.Directory.Domain.Aggregates.Changes; + +public sealed class FilterListChange : Change, IChangeAggregate +{ + private FilterListChange() + { + } + + private FilterListChange(FilterList current, string? reason) + { + Current = current; + Reason = reason; + } + + public FilterList? Current { get; init; } + public FilterList? Before { get; init; } + public FilterList? After { get; init; } + + public static FilterListChange Create(FilterList filterList, string? reason) + { + return new FilterListChange(filterList, reason) { After = filterList }; + } + + public static FilterListChange Update(FilterList before, FilterList after, string? reason) + { + return new FilterListChange(before, reason) { Before = before, After = after }; + } + + public static FilterListChange Delete(FilterList filterList, string? reason) + { + return new FilterListChange(filterList, reason) { Before = filterList }; + } +} diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/IChangeAggregate.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/IChangeAggregate.cs new file mode 100644 index 000000000..1f8d0ba2b --- /dev/null +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/Changes/IChangeAggregate.cs @@ -0,0 +1,8 @@ +namespace FilterLists.Directory.Domain.Aggregates.Changes; + +public interface IChangeAggregate where TAggregate : IAggregate +{ + TAggregate? Current { get; init; } + TAggregate? Before { get; init; } + TAggregate? After { get; init; } +} diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs index 95c636f93..ec3d4d40a 100644 --- a/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs @@ -2,7 +2,7 @@ namespace FilterLists.Directory.Domain.Aggregates.FilterLists; -public class FilterList +public class FilterList : IAggregate { private FilterList() { @@ -40,7 +40,7 @@ public static FilterList Create( if (viewUrls.Count == 0) { // TODO: create and handle DomainExceptions - throw new ArgumentException("At lest one view URL is required", nameof(viewUrls)); + throw new ArgumentException("At lest one view URL is required.", nameof(viewUrls)); } return new FilterList diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/IAggregate.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/IAggregate.cs new file mode 100644 index 000000000..4349329d2 --- /dev/null +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/IAggregate.cs @@ -0,0 +1,5 @@ +namespace FilterLists.Directory.Domain.Aggregates; + +public interface IAggregate +{ +} diff --git a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/CommandDbContext.cs b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/CommandDbContext.cs index 91c11e9a1..be4ac429d 100644 --- a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/CommandDbContext.cs +++ b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/CommandDbContext.cs @@ -12,7 +12,7 @@ public CommandDbContext(DbContextOptions options) : base(optio { } - public DbSet Changes => Set(); + public DbSet FilterListChanges => Set(); public DbSet FilterLists => Set(); public DbSet Licenses => Set(); diff --git a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/ICommandContext.cs b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/ICommandContext.cs index e977e02e8..8f747ba89 100644 --- a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/ICommandContext.cs +++ b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/Context/ICommandContext.cs @@ -7,7 +7,7 @@ namespace FilterLists.Directory.Infrastructure.Persistence.Commands.Context; public interface ICommandContext { - DbSet Changes { get; } + DbSet FilterListChanges { get; } DbSet FilterLists { get; } DbSet Licenses { get; } Task SaveChangesAsync(CancellationToken cancellationToken);