refactor(dir): ♻ resolve temporary inconsistent state of newly created FilterList's License

This commit is contained in:
Collin M. Barrett 2021-11-20 09:43:18 -06:00
parent 2608baedfd
commit cee5db6952
5 changed files with 24 additions and 33 deletions

View file

@ -1,7 +1,9 @@
using FilterLists.Directory.Domain.Aggregates.FilterLists;
using FilterLists.Directory.Domain.Aggregates.Licenses;
using FilterLists.Directory.Infrastructure.Persistence.Commands.Context;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace FilterLists.Directory.Application.Commands;
@ -20,7 +22,7 @@ public record Command(string Name,
Uri? ChatUrl = default,
string? EmailAddress = default,
Uri? DonateUrl = default,
string? ChangeReason = default) : IRequest<Response>;
string? CreateReason = default) : IRequest<Response>;
public record FilterListViewUrl(short SegmentNumber, short Primariness, Uri Url);
@ -45,11 +47,14 @@ public Handler(ICommandContext commandContext)
public async Task<Response> Handle(Command request, CancellationToken cancellationToken)
{
// TODO: push applying domain rule of specified or default license into domain layer?
var license = request.LicenseId != null
? await _commandContext.Licenses.FindAsync(new object[] { request.LicenseId.Value },
cancellationToken) ?? throw new ArgumentException($"LicenseId {request.LicenseId} not found.",
nameof(request.LicenseId))
: default;
? await _commandContext.Licenses
.FindAsync(new object[] { request.LicenseId.Value }, cancellationToken)
?? throw new ArgumentException($"LicenseId {request.LicenseId} not found.", nameof(request.LicenseId))
: await _commandContext.Licenses
.WhereIsDefaultForFilterList()
.SingleAsync(cancellationToken);
var filterList = FilterList.Create(
request.Name,
@ -65,7 +70,7 @@ public async Task<Response> Handle(Command request, CancellationToken cancellati
request.EmailAddress,
request.DonateUrl,
request.ViewUrls.Select(u => (u.SegmentNumber, u.Primariness, u.Url)),
request.ChangeReason);
request.CreateReason);
_commandContext.FilterLists.Add(filterList);
await _commandContext.SaveChangesAsync(cancellationToken);

View file

@ -6,7 +6,6 @@ namespace FilterLists.Directory.Domain.Aggregates.FilterLists;
public sealed class FilterList : AggregateRoot, IRequireChangeApproval<FilterListChange>
{
private ICollection<FilterListChange> _changes = new HashSet<FilterListChange>();
private int _licenseId;
private FilterList()
{
@ -14,7 +13,7 @@ private FilterList()
public string Name { get; private init; } = null!;
public string? Description { get; private init; }
public License License { get; private set; } = null!;
public License License { get; private init; } = null!;
public Uri? HomeUrl { get; private init; }
public Uri? OnionUrl { get; private init; }
public Uri? PolicyUrl { get; private init; }
@ -30,7 +29,7 @@ private FilterList()
public static FilterList Create(
string name,
string? description,
License? license,
License license,
Uri? homeUrl,
Uri? onionUrl,
Uri? policyUrl,
@ -54,6 +53,7 @@ public static FilterList Create(
{
Name = name,
Description = description,
License = license,
HomeUrl = homeUrl,
OnionUrl = onionUrl,
PolicyUrl = policyUrl,
@ -65,17 +65,6 @@ public static FilterList Create(
DonateUrl = donateUrl,
ViewUrls = urls
};
// TODO: resolve temporary inconsistent state between nav prop (default null) and ID field (default 0) between creation and adding to DbContext
if (license is not null)
{
list.License = license;
}
else
{
list._licenseId = License.DefaultAllRightsReservedId;
}
list._changes = new HashSet<FilterListChange>(new[] { FilterListChange.Create(list, createReason) });
return list;
}

View file

@ -4,8 +4,6 @@ public sealed class License
{
private License() { }
public static int DefaultAllRightsReservedId => 5;
public string Name { get; private init; } = null!;
public Uri? Url { get; private init; }
public bool PermitsModification { get; private init; }

View file

@ -0,0 +1,9 @@
namespace FilterLists.Directory.Domain.Aggregates.Licenses;
public static class LicenseExtensions
{
public static IQueryable<License> WhereIsDefaultForFilterList(this IQueryable<License> licenses)
{
return licenses.Where(l => l.Name == "All Rights Reserved");
}
}

View file

@ -1,6 +1,4 @@
using System.Globalization;
using EFCore.NamingConventions.Internal;
using FilterLists.Directory.Domain.Aggregates.FilterLists;
using FilterLists.Directory.Domain.Aggregates.FilterLists;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
@ -10,15 +8,7 @@ internal class FilterListTypeConfiguration : IEntityTypeConfiguration<FilterList
{
public virtual void Configure(EntityTypeBuilder<FilterList> builder)
{
// TODO: register and resolve INameRewriter
var nr = new SnakeCaseNameRewriter(CultureInfo.InvariantCulture);
builder.Property<int>(nameof(Queries.Entities.FilterList.Id));
builder.Property("_licenseId")
.HasColumnName(nr.RewriteName(nameof(Queries.Entities.FilterList.LicenseId)));
builder.HasOne(f => f.License)
.WithMany()
.HasForeignKey("_licenseId");
builder.Navigation(f => f.Changes)
.AutoInclude();
builder.Navigation(f => f.ViewUrls)