refactor(dir): 🚧 use TPH for Change

This commit is contained in:
Collin M. Barrett 2021-11-13 20:27:11 -06:00
parent 5b2a48d058
commit c90f1d8d94
8 changed files with 68 additions and 47 deletions

View file

@ -64,10 +64,10 @@ public async Task<Response> 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);

View file

@ -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;
}
}

View file

@ -0,0 +1,35 @@
using FilterLists.Directory.Domain.Aggregates.FilterLists;
namespace FilterLists.Directory.Domain.Aggregates.Changes;
public sealed class FilterListChange : Change, IChangeAggregate<FilterList>
{
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 };
}
}

View file

@ -0,0 +1,8 @@
namespace FilterLists.Directory.Domain.Aggregates.Changes;
public interface IChangeAggregate<TAggregate> where TAggregate : IAggregate
{
TAggregate? Current { get; init; }
TAggregate? Before { get; init; }
TAggregate? After { get; init; }
}

View file

@ -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

View file

@ -0,0 +1,5 @@
namespace FilterLists.Directory.Domain.Aggregates;
public interface IAggregate
{
}

View file

@ -12,7 +12,7 @@ public CommandDbContext(DbContextOptions<CommandDbContext> options) : base(optio
{
}
public DbSet<Change> Changes => Set<Change>();
public DbSet<FilterListChange> FilterListChanges => Set<FilterListChange>();
public DbSet<FilterList> FilterLists => Set<FilterList>();
public DbSet<License> Licenses => Set<License>();

View file

@ -7,7 +7,7 @@ namespace FilterLists.Directory.Infrastructure.Persistence.Commands.Context;
public interface ICommandContext
{
DbSet<Change> Changes { get; }
DbSet<FilterListChange> FilterListChanges { get; }
DbSet<FilterList> FilterLists { get; }
DbSet<License> Licenses { get; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);