diff --git a/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs b/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs index 26837fa2a..17d365036 100644 --- a/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs +++ b/services/Directory/FilterLists.Directory.Application/Commands/CreateList.cs @@ -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; + string? CreateReason = default) : IRequest; public record FilterListViewUrl(short SegmentNumber, short Primariness, Uri Url); @@ -45,11 +47,14 @@ public Handler(ICommandContext commandContext) public async Task 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 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); diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs index ab59d4c9a..92f362cb6 100644 --- a/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/FilterLists/FilterList.cs @@ -6,7 +6,6 @@ namespace FilterLists.Directory.Domain.Aggregates.FilterLists; public sealed class FilterList : AggregateRoot, IRequireChangeApproval { private ICollection _changes = new HashSet(); - 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(new[] { FilterListChange.Create(list, createReason) }); return list; } diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/License.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/License.cs index 16a53afc0..bfd8c357b 100644 --- a/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/License.cs +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/License.cs @@ -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; } diff --git a/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/LicenseExtensions.cs b/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/LicenseExtensions.cs new file mode 100644 index 000000000..7c64b797c --- /dev/null +++ b/services/Directory/FilterLists.Directory.Domain/Aggregates/Licenses/LicenseExtensions.cs @@ -0,0 +1,9 @@ +namespace FilterLists.Directory.Domain.Aggregates.Licenses; + +public static class LicenseExtensions +{ + public static IQueryable WhereIsDefaultForFilterList(this IQueryable licenses) + { + return licenses.Where(l => l.Name == "All Rights Reserved"); + } +} diff --git a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/EntityTypeConfigurations/FilterListTypeConfiguration.cs b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/EntityTypeConfigurations/FilterListTypeConfiguration.cs index b799aa3bc..a4298b110 100644 --- a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/EntityTypeConfigurations/FilterListTypeConfiguration.cs +++ b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Commands/EntityTypeConfigurations/FilterListTypeConfiguration.cs @@ -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 builder) { - // TODO: register and resolve INameRewriter - var nr = new SnakeCaseNameRewriter(CultureInfo.InvariantCulture); - builder.Property(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)