diff --git a/src/Lumina.Domain/Common/Primitives/ConflictError.cs b/src/Lumina.Domain/Common/Primitives/ConflictError.cs new file mode 100644 index 00000000..8a999742 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/ConflictError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the operation could not be completed due to a conflict with the current state. +/// +public record ConflictError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/Created.cs b/src/Lumina.Domain/Common/Primitives/Created.cs new file mode 100644 index 00000000..e5b184b4 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Created.cs @@ -0,0 +1,6 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Represents a successful creation operation result. +/// +public readonly record struct Created; diff --git a/src/Lumina.Domain/Common/Primitives/Deleted.cs b/src/Lumina.Domain/Common/Primitives/Deleted.cs new file mode 100644 index 00000000..49ec7ae6 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Deleted.cs @@ -0,0 +1,6 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Represents a successful deletion operation result. +/// +public readonly record struct Deleted; diff --git a/src/Lumina.Domain/Common/Primitives/Error.cs b/src/Lumina.Domain/Common/Primitives/Error.cs new file mode 100644 index 00000000..97eb1dba --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Error.cs @@ -0,0 +1,136 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Represents an error that can be implicitly converted to a Results. +/// +public abstract record Error +{ + /// + /// Gets the error code that uniquely identifies this error type. + /// + public abstract string Code { get; } + + /// + /// Gets the human-readable description of the error. + /// + public abstract string Description { get; } + + /// + /// Returns a string representation of the error. + /// + /// A formatted string containing the error code and description. + public override string ToString() + { + return $"{Code}: {Description}"; + } + + /// + /// Creates a new NotFoundError with the specified description. + /// + /// The human-readable description of the error. + /// A new NotFoundError instance with code "NotFound". + public static NotFoundError NotFound(string description) + { + return new("NotFound", description); + } + + /// + /// Creates a new InvalidOperationError with the specified description. + /// + /// The human-readable description of the error. + /// A new InvalidOperationError instance with code "InvalidOperation". + public static InvalidOperationError InvalidOperation(string description) + { + return new("InvalidOperation", description); + } + + /// + /// Creates a new ForbiddenError with the specified description. + /// + /// The human-readable description of the error. + /// A new ForbiddenError instance with code "Forbidden". + public static ForbiddenError Forbidden(string description) + { + return new("Forbidden", description); + } + + /// + /// Creates a new FailureError with the specified description. + /// + /// The human-readable description of the error. + /// A new FailureError instance with code "Failure". + public static FailureError Failure(string description) + { + return new("Failure", description); + } + + /// + /// Creates a new ValidationError with the specified description. + /// + /// The human-readable description of the error. + /// A new ValidationError instance with code "Validation". + public static ValidationError Validation(string description) + { + return new("Validation", description); + } + + /// + /// Creates a new ConflictError with the specified description. + /// + /// The human-readable description of the error. + /// A new ConflictError instance with code "Conflict". + public static ConflictError Conflict(string description) + { + return new("Conflict", description); + } + + /// + /// Creates a new TimeoutError with the specified description. + /// + /// The human-readable description of the error. + /// A new TimeoutError instance with code "Timeout". + public static TimeoutError Timeout(string description) + { + return new("Timeout", description); + } + + /// + /// Creates a new UnauthorizedError with the specified description. + /// + /// The human-readable description of the error. + /// A new UnauthorizedError instance with code "Unauthorized". + public static UnauthorizedError Unauthorized(string description) + { + return new("Unauthorized", description); + } + + /// + /// Creates a new InternalError with the specified description. + /// + /// The human-readable description of the error. + /// A new InternalError instance with code "Internal". + public static InternalError Internal(string description) + { + return new("Internal", description); + } + + /// + /// Creates a new ResourceUnavailableError with the specified description. + /// + /// The human-readable description of the error. + /// A new ResourceUnavailableError instance with code "ResourceUnavailable". + public static ResourceUnavailableError ResourceUnavailable(string description) + { + return new("ResourceUnavailable", description); + } + + /// + /// Creates a new NotImplementedError with the specified description. + /// + /// The human-readable description of the error. + /// A new NotImplementedError instance with code "NotImplemented". + public static NotImplementedError NotImplemented(string description) + { + return new("NotImplemented", description); + } +} diff --git a/src/Lumina.Domain/Common/Primitives/FailureError.cs b/src/Lumina.Domain/Common/Primitives/FailureError.cs new file mode 100644 index 00000000..548d18aa --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/FailureError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that some operation failing. +/// +public record FailureError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/ForbiddenError.cs b/src/Lumina.Domain/Common/Primitives/ForbiddenError.cs new file mode 100644 index 00000000..c96bdf2a --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/ForbiddenError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the operation is not allowed due to insufficient permissions or restrictions. +/// +public record ForbiddenError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/InternalError.cs b/src/Lumina.Domain/Common/Primitives/InternalError.cs new file mode 100644 index 00000000..0446e119 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/InternalError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that an unexpected internal error occurred. +/// +public record InternalError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/InvalidOperationError.cs b/src/Lumina.Domain/Common/Primitives/InvalidOperationError.cs new file mode 100644 index 00000000..7992af5e --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/InvalidOperationError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the operation is invalid in the current state or context. +/// +public record InvalidOperationError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/NotFoundError.cs b/src/Lumina.Domain/Common/Primitives/NotFoundError.cs new file mode 100644 index 00000000..e9724789 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/NotFoundError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the requested resource or entity was not found. +/// +public record NotFoundError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/NotImplementedError.cs b/src/Lumina.Domain/Common/Primitives/NotImplementedError.cs new file mode 100644 index 00000000..5798c3d8 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/NotImplementedError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the requested functionality is not implemented. +/// +public record NotImplementedError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/ResourceUnavailableError.cs b/src/Lumina.Domain/Common/Primitives/ResourceUnavailableError.cs new file mode 100644 index 00000000..4f08be8d --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/ResourceUnavailableError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the required resource is currently unavailable. +/// +public record ResourceUnavailableError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/Result.cs b/src/Lumina.Domain/Common/Primitives/Result.cs new file mode 100644 index 00000000..b7a28bc9 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Result.cs @@ -0,0 +1,237 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Primitive struct for explicitly handling success and failure cases in a functional manner. +/// +/// The type of the success value. +public readonly struct Result : IEquatable> +{ + private readonly TValue? _value; + private readonly List? _errors; + + /// + /// Gets a value indicating whether this result represents a success. + /// + public bool IsSuccess { get; } + + /// + /// Gets a value indicating whether this result represents a failure. + /// + public bool IsFailure => !IsSuccess; + + /// + /// Gets the success value. Only valid when IsSuccess is true. + /// + public TValue Value => IsSuccess ? _value! : throw new InvalidOperationException("Cannot access Value of a failed result."); + + /// + /// Gets the list of errors. Only valid when IsFailure is true. + /// + public List Errors => IsFailure + ? _errors ?? [] + : throw new InvalidOperationException("Cannot access Errors of a successful result."); + + /// + /// Returns the first error, if present. + /// + public Error FirstError => Errors[0]; + + /// + /// Initializes a new instance of the structure. + /// + /// The success value. + /// The list of errors. + /// Whether this represents a success or failure. + private Result(TValue? value, List? errors, bool isSuccess) + { + _value = value; + _errors = errors; + IsSuccess = isSuccess; + } + + /// + /// Creates a successful result with the specified value. + /// + /// The success value. + /// A successful result containing the specified value. + public static Result Success(TValue value) + { + return new Result(value, default, true); + } + + /// + /// Creates a failed result with the specified errors. + /// + /// The errors value. + /// A failed result containing the specified error. + public static Result Failure(List errors) + { + return new Result(default, errors, false); + } + + /// + /// Creates a failed result with the specified errors. + /// + /// The errors value. + /// A failed result containing the specified error. + public static Result Failure(params Error[] errors) + { + return new(default, [.. errors], false); + } + + /// + /// Custom implementation operator for converting to a . + /// + /// The value to be converted. + public static implicit operator Result(TValue value) + { + return Success(value); + } + + /// + /// Custom implementation operator for converting to a failed . + /// + /// The error to be converted. + public static implicit operator Result(Error error) + { + return Failure(error); + } + + /// + /// Custom implementation operator for converting to a failed . + /// + /// The errors to be converted. + public static implicit operator Result(List errors) + { + return Failure(errors); + } + + /// + /// Maps the success value to a new type using the provided function. + /// + /// The type of the new success value. + /// The function to transform the success value. + /// A new result with the transformed value, or the original errors. + public Result Map(Func mapper) + { + return IsSuccess + ? Result.Success(mapper(Value)) + : Result.Failure(Errors); + } + + /// + /// Chains another operation that returns a Result, allowing for monadic composition. + /// + /// The type of the new success value. + /// The function that returns a new Results. + /// The result of the chained operation, or the original error. + public Result Bind(Func> binder) + { + return IsSuccess ? binder(Value) : Result.Failure(Errors); + } + + /// + /// Executes an action if the result is successful, otherwise does nothing. + /// + /// The action to execute on success. + /// The original result for chaining. + public Result OnSuccess(Action action) + { + if (IsSuccess) + action(Value); + return this; + } + + /// + /// Executes an action if the result is a failure, otherwise does nothing. + /// + /// The action to execute on failure. + /// The original result for chaining. + public Result OnFailure(Action> action) + { + if (IsFailure) + action(Errors); + return this; + } + + /// + /// Matches the result and executes the appropriate function. + /// + /// The type of the result. + /// Function to execute on success. + /// Function to execute on failure. + /// The result of the executed function. + public TResult Match(Func onSuccess, Func, TResult> onFailure) + { + return IsSuccess ? onSuccess(Value) : onFailure(Errors); + } + + /// + /// Determines whether the specified Result is equal to the current Results. + /// + /// The Result to compare with the current Results. + /// true if the specified Result is equal to the current Result; otherwise, false. + public bool Equals(Result other) + { + if (IsSuccess != other.IsSuccess) + return false; + + return IsSuccess + ? EqualityComparer.Default.Equals(Value, other.Value) + : Errors.SequenceEqual(other.Errors); + } + + /// + /// Determines whether the specified object is equal to the current Results. + /// + /// The object to compare with the current Results. + /// true if the specified object is equal to the current Result; otherwise, false. + public override bool Equals(object? obj) + { + return obj is Result other && Equals(other); + } + + /// + /// Determines whether two specified Result instances are equal. + /// + /// The first Result to compare. + /// The second Result to compare. + /// true if the two Result instances are equal; otherwise, false. + public static bool operator ==(Result left, Result right) + { + return left.Equals(right); + } + + /// + /// Determines whether two specified Result instances are not equal. + /// + /// The first Result to compare. + /// The second Result to compare. + /// true if the two Result instances are not equal; otherwise, false. + public static bool operator !=(Result left, Result right) + { + return !(left == right); + } + + /// + /// Returns the hash code for this Results. + /// + /// A 32-bit signed integer hash code. + public override int GetHashCode() + { + return IsSuccess + ? HashCode.Combine(IsSuccess, Value) + : HashCode.Combine(IsSuccess, Errors); + } + + /// + /// Customized ToString() method. + /// + /// Custom string value showing relevant data for current result. + public override string ToString() + { + return IsSuccess + ? $"Success: {Value?.ToString()}" + : $"Failure: [{string.Join(", ", Errors.Select(error => error.ToString()))}]"; + } +} diff --git a/src/Lumina.Domain/Common/Primitives/Results.cs b/src/Lumina.Domain/Common/Primitives/Results.cs new file mode 100644 index 00000000..eae5de3c --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Results.cs @@ -0,0 +1,27 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Provides predefined success results for operations that don't return meaningful data. +/// +public static class Results +{ + /// + /// Represents a successful operation. + /// + public static Success Success { get; } = new(); + + /// + /// Represents a successful creation operation. + /// + public static Created Created { get; } = new(); + + /// + /// Represents a successful update operation. + /// + public static Updated Updated { get; } = new(); + + /// + /// Represents a successful deletion operation. + /// + public static Deleted Deleted { get; } = new(); +} diff --git a/src/Lumina.Domain/Common/Primitives/Success.cs b/src/Lumina.Domain/Common/Primitives/Success.cs new file mode 100644 index 00000000..b505ba31 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Success.cs @@ -0,0 +1,6 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Represents a successful operation result. +/// +public readonly record struct Success; diff --git a/src/Lumina.Domain/Common/Primitives/TimeoutError.cs b/src/Lumina.Domain/Common/Primitives/TimeoutError.cs new file mode 100644 index 00000000..610b06c0 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/TimeoutError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the operation timed out before completion. +/// +public record TimeoutError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/UnauthorizedError.cs b/src/Lumina.Domain/Common/Primitives/UnauthorizedError.cs new file mode 100644 index 00000000..686ad165 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/UnauthorizedError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that authentication is required or has failed. +/// +public record UnauthorizedError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Common/Primitives/Updated.cs b/src/Lumina.Domain/Common/Primitives/Updated.cs new file mode 100644 index 00000000..073fdc9a --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/Updated.cs @@ -0,0 +1,6 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Represents a successful update operation result. +/// +public readonly record struct Updated; diff --git a/src/Lumina.Domain/Common/Primitives/ValidationError.cs b/src/Lumina.Domain/Common/Primitives/ValidationError.cs new file mode 100644 index 00000000..3a5d1de7 --- /dev/null +++ b/src/Lumina.Domain/Common/Primitives/ValidationError.cs @@ -0,0 +1,10 @@ +namespace Lumina.Domain.Common.Primitives; + +/// +/// Error representing that the input or data failed validation rules. +/// +public record ValidationError(string Code, string Description) : Error +{ + public override string Code { get; } = Code; + public override string Description { get; } = Description; +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/WrittenContentLibraryBoundedContext/BookLibraryAggregate/Book.cs b/src/Lumina.Domain/Core/BoundedContexts/WrittenContentLibraryBoundedContext/BookLibraryAggregate/Book.cs index 5cf9e804..c6b1a436 100644 --- a/src/Lumina.Domain/Core/BoundedContexts/WrittenContentLibraryBoundedContext/BookLibraryAggregate/Book.cs +++ b/src/Lumina.Domain/Core/BoundedContexts/WrittenContentLibraryBoundedContext/BookLibraryAggregate/Book.cs @@ -1,5 +1,4 @@ #region ========================================================================= USING ===================================================================================== -using ErrorOr; using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary; using Lumina.Domain.Common.Models.Core; using Lumina.Domain.Common.Primitives; @@ -233,9 +232,9 @@ public sealed class Book : AggregateRoot /// The list of objects representing the unique identifiers of the media contributors of the book. /// The list of ratings for the book. /// - /// An containing either a successfully created , or an error message. + /// An containing either a successfully created , or an error message. /// - public static ErrorOr Create( + public static Result Create( LibraryId libraryId, string path, WrittenContentMetadata metadata, @@ -309,9 +308,9 @@ public sealed class Book : AggregateRoot /// The list of objects representing the unique identifiers of the media contributors of the book. /// The list of ratings for the book. /// - /// An containing either a successfully created , or an error message. + /// An containing either a successfully created , or an error message. /// - public static ErrorOr Create( + public static Result Create( BookId id, LibraryId libraryId, string path, diff --git a/src/Lumina.Presentation.Web/Controllers/Plugins/PluginsController.cs b/src/Lumina.Presentation.Web/Controllers/Plugins/PluginsController.cs index 6c103c82..4bfaff8b 100644 --- a/src/Lumina.Presentation.Web/Controllers/Plugins/PluginsController.cs +++ b/src/Lumina.Presentation.Web/Controllers/Plugins/PluginsController.cs @@ -45,10 +45,8 @@ public class PluginsController : Controller // load the settings of the loaded plugins, so that the settings forms can be rendered on the server Dictionary settingsByPluginId = []; foreach (PluginModel plugin in plugins) - { if (plugin.LoadStatus == PluginLoadStatus.Loaded) settingsByPluginId[plugin.Id] = await _apiHttpClient.GetAsync($"plugins/{plugin.Id}/settings", cancellationToken).ConfigureAwait(false); - } ViewData["pluginSettings"] = settingsByPluginId; return View(plugins);