mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 00:11:10 +00:00
Adds the test suites for the library path templates and centralizes their resolution in the domain service
This commit is contained in:
1 parent
ca4f308b0a
commit
7ed6f05632
52 files changed
+4724
-40
No files matched your search
+4
-7
@@ -120,12 +120,9 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
|
||||
Result<IReadOnlyList<LibraryPathPart>> pathTemplatePartsResult = command.PathTemplateParts.ToDomainParts();
|
||||
if (pathTemplatePartsResult.IsFailure)
|
||||
return pathTemplatePartsResult.Errors;
|
||||
LibraryPathTemplate pathTemplate = command.PathTemplateParts is null
|
||||
? _pathTemplateService.GetDefaultTemplate(libraryType)
|
||||
: LibraryPathTemplate.Create(pathTemplatePartsResult.Value);
|
||||
Result<Success> validatePathTemplateResult = _pathTemplateService.Validate(libraryType, pathTemplate);
|
||||
if (validatePathTemplateResult.IsFailure)
|
||||
return validatePathTemplateResult.Errors;
|
||||
Result<LibraryPathTemplate> pathTemplateResult = _pathTemplateService.ResolveTemplate(libraryType, pathTemplatePartsResult.Value);
|
||||
if (pathTemplateResult.IsFailure)
|
||||
return pathTemplateResult.Errors;
|
||||
|
||||
// Create a domain library object.
|
||||
Result<Library> createLibraryResult = Library.Create(
|
||||
@@ -139,7 +136,7 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
|
||||
command.CanDownloadMetadataFromWeb,
|
||||
command.ShouldSaveMetadataInMediaDirectories,
|
||||
command.ShouldSkipUnchangedDirectoriesDuringScan,
|
||||
pathTemplate,
|
||||
pathTemplateResult.Value,
|
||||
[]
|
||||
);
|
||||
|
||||
|
||||
+4
-7
@@ -134,12 +134,9 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
Result<IReadOnlyList<LibraryPathPart>> pathTemplatePartsResult = command.PathTemplateParts.ToDomainParts();
|
||||
if (pathTemplatePartsResult.IsFailure)
|
||||
return pathTemplatePartsResult.Errors;
|
||||
LibraryPathTemplate pathTemplate = command.PathTemplateParts is null
|
||||
? _pathTemplateService.GetDefaultTemplate(newLibraryType)
|
||||
: LibraryPathTemplate.Create(pathTemplatePartsResult.Value);
|
||||
Result<Success> validatePathTemplateResult = _pathTemplateService.Validate(newLibraryType, pathTemplate);
|
||||
if (validatePathTemplateResult.IsFailure)
|
||||
return validatePathTemplateResult.Errors;
|
||||
Result<LibraryPathTemplate> pathTemplateResult = _pathTemplateService.ResolveTemplate(newLibraryType, pathTemplatePartsResult.Value);
|
||||
if (pathTemplateResult.IsFailure)
|
||||
return pathTemplateResult.Errors;
|
||||
|
||||
Result<Library> createLibraryResult = Library.Create(
|
||||
LibraryId.Create(command.Id),
|
||||
@@ -153,7 +150,7 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
command.CanDownloadMetadataFromWeb,
|
||||
command.ShouldSaveMetadataInMediaDirectories,
|
||||
command.ShouldSkipUnchangedDirectoriesDuringScan,
|
||||
pathTemplate,
|
||||
pathTemplateResult.Value,
|
||||
[.. getLibraryResult.Value.LibraryScans.Select(libraryScan => ScanId.Create(libraryScan.Id))]
|
||||
);
|
||||
if (createLibraryResult.IsFailure)
|
||||
|
||||
@@ -20,14 +20,13 @@ public static partial class Errors
|
||||
public static Error LibraryNotFound => Error.NotFound(description: nameof(LibraryNotFound));
|
||||
public static Error LibraryTypeCannotBeNull => Error.Validation(description: nameof(LibraryTypeCannotBeNull));
|
||||
public static Error CoverFileMustBeAnImage => Error.Validation(description: nameof(CoverFileMustBeAnImage));
|
||||
public static Error UnknownLibraryType => Error.Unexpected(description: nameof(UnknownLibraryType));
|
||||
public static Error UnknownLibraryType => Error.Forbidden(description: nameof(UnknownLibraryType));
|
||||
public static Error PathsListCannotBeNull => Error.Validation(description: nameof(PathsListCannotBeNull));
|
||||
public static Error PathsListCannotBeEmpty => Error.Validation(description: nameof(PathsListCannotBeEmpty));
|
||||
public static Error TitleCannotBeEmpty => Error.Validation(description: nameof(TitleCannotBeEmpty));
|
||||
public static Error TitleMustBeMaximum255CharactersLong => Error.Validation(description: nameof(TitleMustBeMaximum255CharactersLong));
|
||||
public static Error PathTemplateLiteralCannotBeEmpty => Error.Validation(description: nameof(PathTemplateLiteralCannotBeEmpty));
|
||||
public static Error PathTemplateValuePartMustContainSinglePlaceholder => Error.Validation(description: nameof(PathTemplateValuePartMustContainSinglePlaceholder));
|
||||
public static Error PathTemplateCannotContainDuplicateValuePart => Error.Validation(description: nameof(PathTemplateCannotContainDuplicateValuePart));
|
||||
public static Error PathTemplatePartKindNotSupportedForLibraryType => Error.Validation(description: nameof(PathTemplatePartKindNotSupportedForLibraryType));
|
||||
public static Error PathTemplateIsNotSupportedForLibraryType => Error.Validation(description: nameof(PathTemplateIsNotSupportedForLibraryType));
|
||||
}
|
||||
|
||||
+2
-1
@@ -77,7 +77,8 @@ public class Library : AggregateRoot<LibraryId>
|
||||
public bool ShouldSkipUnchangedDirectoriesDuringScan { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the template describing the structure of the media library on disk, always populated, with the ideal structure of the library type as its default.
|
||||
/// Gets the template describing the structure of the media library on disk, always populated with the effective template: the one specified by the
|
||||
/// user, or the ideal structure of the library type when none was provided.
|
||||
/// </summary>
|
||||
public LibraryPathTemplate PathTemplate { get; private set; }
|
||||
|
||||
|
||||
+12
@@ -3,6 +3,7 @@ using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.PathTemplate;
|
||||
@@ -19,6 +20,17 @@ public interface ILibraryPathTemplateService
|
||||
/// <returns>The default path template.</returns>
|
||||
LibraryPathTemplate GetDefaultTemplate(LibraryType libraryType);
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the effective path template of a media library of the provided <paramref name="libraryType"/>: the template built from the provided
|
||||
/// <paramref name="providedParts"/>, or the ideal structure of the library type when no parts are provided.
|
||||
/// </summary>
|
||||
/// <param name="libraryType">The media library type the template belongs to.</param>
|
||||
/// <param name="providedParts">The parts provided by the user. An empty collection means no template was provided, so the library type default is used.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either the resolved path template, or an error.
|
||||
/// </returns>
|
||||
Result<LibraryPathTemplate> ResolveTemplate(LibraryType libraryType, IReadOnlyList<LibraryPathPart> providedParts);
|
||||
|
||||
/// <summary>
|
||||
/// Validates the provided <paramref name="template"/> against the catalog of the provided <paramref name="libraryType"/>.
|
||||
/// </summary>
|
||||
|
||||
+41
-4
@@ -3,6 +3,7 @@ using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -23,6 +24,7 @@ public sealed class LibraryPathTemplateService : ILibraryPathTemplateService
|
||||
private static readonly Regex s_placeholderRegex = new(@"\{0(?::(?<width>0+))?\}", RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
||||
|
||||
private readonly IReadOnlyList<ILibraryPathPartCatalog> _catalogs;
|
||||
private readonly ConcurrentDictionary<string, Regex> _compiledPatterns = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LibraryPathTemplateService"/> class.
|
||||
@@ -43,6 +45,27 @@ public sealed class LibraryPathTemplateService : ILibraryPathTemplateService
|
||||
return ResolveCatalog(libraryType)?.GetDefaultTemplate() ?? LibraryPathTemplate.Empty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the effective path template of a media library of the provided <paramref name="libraryType"/>: the template built from the provided
|
||||
/// <paramref name="providedParts"/>, or the ideal structure of the library type when no parts are provided.
|
||||
/// </summary>
|
||||
/// <param name="libraryType">The media library type the template belongs to.</param>
|
||||
/// <param name="providedParts">The parts provided by the user. An empty collection means no template was provided, so the library type default is used.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either the resolved path template, or an error.
|
||||
/// </returns>
|
||||
public Result<LibraryPathTemplate> ResolveTemplate(LibraryType libraryType, IReadOnlyList<LibraryPathPart> providedParts)
|
||||
{
|
||||
// An empty set of parts means the user did not provide a template, so the ideal structure of the library type is used.
|
||||
LibraryPathTemplate template = providedParts.Count == 0
|
||||
? GetDefaultTemplate(libraryType)
|
||||
: LibraryPathTemplate.Create(providedParts);
|
||||
Result<Success> validationResult = Validate(libraryType, template);
|
||||
if (validationResult.IsFailure)
|
||||
return validationResult.Errors;
|
||||
return template;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the provided <paramref name="template"/> against the catalog of the provided <paramref name="libraryType"/>.
|
||||
/// </summary>
|
||||
@@ -88,7 +111,7 @@ public sealed class LibraryPathTemplateService : ILibraryPathTemplateService
|
||||
ILibraryPathPartCatalog catalog = ResolveCatalog(libraryType)!;
|
||||
Dictionary<LibraryPathPartKind, LibraryPathPartDefinition> definitions = catalog.GetPartDefinitions()
|
||||
.ToDictionary(definition => definition.Kind);
|
||||
Regex pattern = BuildPattern(template, definitions, pathSeparator);
|
||||
Regex pattern = GetCompiledPattern(template, definitions, pathSeparator);
|
||||
Match match = pattern.Match(relativePath);
|
||||
if (!match.Success)
|
||||
return Result<ParsedLibraryPath?>.Success(null);
|
||||
@@ -123,13 +146,27 @@ public sealed class LibraryPathTemplateService : ILibraryPathTemplateService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the regular expression that matches the provided <paramref name="template"/>.
|
||||
/// Gets the compiled regular expression that matches the provided <paramref name="template"/>, building and compiling it only once per distinct
|
||||
/// pattern, so that scanning a library does not pay the cost of compiling the same pattern once per file.
|
||||
/// </summary>
|
||||
/// <param name="template">The path template to compile.</param>
|
||||
/// <param name="definitions">The available part definitions of the template's library type.</param>
|
||||
/// <param name="pathSeparator">The character used to separate path segments on the current platform.</param>
|
||||
/// <returns>The compiled regular expression.</returns>
|
||||
private static Regex BuildPattern(LibraryPathTemplate template, IReadOnlyDictionary<LibraryPathPartKind, LibraryPathPartDefinition> definitions, char pathSeparator)
|
||||
private Regex GetCompiledPattern(LibraryPathTemplate template, IReadOnlyDictionary<LibraryPathPartKind, LibraryPathPartDefinition> definitions, char pathSeparator)
|
||||
{
|
||||
string patternText = BuildPattern(template, definitions, pathSeparator);
|
||||
return _compiledPatterns.GetOrAdd(patternText, pattern => new Regex(pattern, RegexOptions.Compiled | RegexOptions.CultureInvariant));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the regular expression text that matches the provided <paramref name="template"/>.
|
||||
/// </summary>
|
||||
/// <param name="template">The path template to compile.</param>
|
||||
/// <param name="definitions">The available part definitions of the template's library type.</param>
|
||||
/// <param name="pathSeparator">The character used to separate path segments on the current platform.</param>
|
||||
/// <returns>The regular expression text.</returns>
|
||||
private static string BuildPattern(LibraryPathTemplate template, IReadOnlyDictionary<LibraryPathPartKind, LibraryPathPartDefinition> definitions, char pathSeparator)
|
||||
{
|
||||
StringBuilder patternBuilder = new("^");
|
||||
int index = 0;
|
||||
@@ -153,7 +190,7 @@ public sealed class LibraryPathTemplateService : ILibraryPathTemplateService
|
||||
patternBuilder.Append(runBuilder);
|
||||
}
|
||||
patternBuilder.Append('$');
|
||||
return new Regex(patternBuilder.ToString(), RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
||||
return patternBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+24
-1
@@ -114,6 +114,30 @@ public class AddLibraryEndpointSummary : Summary<AddLibraryEndpoint, AddLibraryR
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the authenticated user is not allowed to manage media libraries, or because the request carried an unsupported library type.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/libraries",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "UnknownLibraryType",
|
||||
instance = "/api/v1/libraries",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Response(422, "The request did not pass validation checks.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
@@ -128,7 +152,6 @@ public class AddLibraryEndpointSummary : Summary<AddLibraryEndpoint, AddLibraryR
|
||||
"General.Validation", new[]
|
||||
{
|
||||
"LibraryTypeCannotBeNull",
|
||||
"UnknownLibraryType",
|
||||
"PathsListCannotBeNull",
|
||||
"PathsListCannotBeEmpty",
|
||||
"PathCannotBeEmpty",
|
||||
|
||||
+20
-9
@@ -118,15 +118,27 @@ public class UpdateLibraryEndpointSummary : Summary<UpdateLibraryEndpoint, Updat
|
||||
}
|
||||
);
|
||||
|
||||
Response(403, "The request failed because the user making the request is not an Admin, or the owner of the media library.", "application/problem+json",
|
||||
example: new
|
||||
Response(403, "The request failed because the authenticated user is not allowed to manage the media library, or because the request carried an unsupported library type.", "application/problem+json",
|
||||
example: new[]
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/libraries/{id}",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Failure",
|
||||
status = 403,
|
||||
detail = "NotAuthorized",
|
||||
instance = "/api/v1/libraries/{id}",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
},
|
||||
new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
|
||||
title = "General.Forbidden",
|
||||
status = 403,
|
||||
detail = "UnknownLibraryType",
|
||||
instance = "/api/v1/libraries/{id}",
|
||||
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -158,7 +170,6 @@ public class UpdateLibraryEndpointSummary : Summary<UpdateLibraryEndpoint, Updat
|
||||
"LibraryIdCannotBeEmpty",
|
||||
"UserIdCannotBeEmpty",
|
||||
"LibraryTypeCannotBeNull",
|
||||
"UnknownLibraryType",
|
||||
"PathsListCannotBeNull",
|
||||
"PathsListCannotBeEmpty",
|
||||
"PathCannotBeEmpty",
|
||||
|
||||
Reference in new issue
Block a user