mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 01:11:10 +00:00
Defines base implementation for mediatr and fluentvalidation with samples
This commit is contained in:
1 parent
76db0c1597
commit
9a5c7473f9
33 files changed
+1123
-131
No files matched your search
@@ -0,0 +1,8 @@
|
||||
namespace Lumina.Application.Common.CQRS;
|
||||
|
||||
/// <summary>
|
||||
/// Marker interface for command types handled by <see cref="ICommandHandler{TCommand, TResult}"/>.
|
||||
/// </summary>
|
||||
public interface ICommand
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.CQRS;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a contract for handling commands in the application layer.
|
||||
/// Commands represent operations that modify application state.
|
||||
/// </summary>
|
||||
/// <typeparam name="TCommand">The type of command to handle.</typeparam>
|
||||
/// <typeparam name="TResult">The type of result returned after handling the command.</typeparam>
|
||||
public interface ICommandHandler<in TCommand, TResult> where TCommand : ICommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles the specified command and returns a result.
|
||||
/// </summary>
|
||||
/// <param name="command">The command to handle.</param>
|
||||
/// <param name="cancellationToken">Token to monitor for cancellation requests.</param>
|
||||
/// <returns>A task that represents the asynchronous operation, containing the result of the command execution.</returns>
|
||||
Task<TResult> HandleAsync(TCommand command, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Lumina.Application.Common.CQRS;
|
||||
|
||||
/// <summary>
|
||||
/// Marker interface for query types handled by <see cref="IQueryHandler{TQuery, TResult}"/>.
|
||||
/// </summary>
|
||||
public interface IQuery
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.CQRS;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a contract for handling queries in the application layer.
|
||||
/// Queries represent read-only operations that retrieve data without modifying application state.
|
||||
/// </summary>
|
||||
/// <typeparam name="TQuery">The type of query to handle.</typeparam>
|
||||
/// <typeparam name="TResult">The type of result returned after handling the query.</typeparam>
|
||||
public interface IQueryHandler<in TQuery, TResult> where TQuery : IQuery
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles the specified query and returns a result.
|
||||
/// </summary>
|
||||
/// <param name="query">The query to handle.</param>
|
||||
/// <param name="cancellationToken">Token to monitor for cancellation requests.</param>
|
||||
/// <returns>A task that represents the asynchronous operation, containing the result of the query execution.</returns>
|
||||
Task<TResult> HandleAsync(TQuery query, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -3,7 +3,11 @@ using FluentValidation;
|
||||
using Lumina.Application.Common.Behaviors;
|
||||
using Mediator;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.DependencyInjection;
|
||||
@@ -33,6 +37,32 @@ public static class ApplicationLayerServices
|
||||
// register fluent validators
|
||||
services.AddValidatorsFromAssembly(typeof(ApplicationLayerServices).Assembly);
|
||||
|
||||
Type[] handlers =
|
||||
[
|
||||
typeof(CQRS.ICommandHandler<,>),
|
||||
typeof(CQRS.IQueryHandler<,>),
|
||||
typeof(Infrastructure.Validation.IValidator<>),
|
||||
];
|
||||
|
||||
IEnumerable<Type> types = Assembly.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(type => !type.IsInterface && !type.IsAbstract);
|
||||
|
||||
var registrations = types
|
||||
.SelectMany(type => type.GetInterfaces(),
|
||||
(implementation, service) => new { Service = service, Implmentation = implementation })
|
||||
.Where(type => type.Service.IsGenericType && handlers.Contains(type.Service.GetGenericTypeDefinition()));
|
||||
|
||||
foreach (var registration in registrations)
|
||||
{
|
||||
if (!services.Any(service => service.ServiceType == registration.Service))
|
||||
{
|
||||
if (registration.Service.GetGenericTypeDefinition() == typeof(Infrastructure.Validation.IValidator<>))
|
||||
services.AddSingleton(registration.Service, registration.Implmentation);
|
||||
else
|
||||
services.AddScoped(registration.Service, registration.Implmentation);
|
||||
}
|
||||
}
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for creating validators for commands and queries.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) to be validated.</typeparam>
|
||||
public abstract class AbstractValidator<TRequest> : IValidator<TRequest>
|
||||
{
|
||||
private readonly List<IValidationRule<TRequest>> _rules = [];
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule for a specific property on the request being validated.
|
||||
/// </summary>
|
||||
/// <typeparam name="TProperty">The type of the property being validated.</typeparam>
|
||||
/// <param name="propertySelector">A function that selects the property to validate from the request instance.</param>
|
||||
/// <returns>An <see cref="IRuleBuilder{TRequest, TProperty}"/> instance to configure the rule fluently.</returns>
|
||||
protected IRuleBuilder<TRequest, TProperty> RuleFor<TProperty>(Func<TRequest, TProperty> propertySelector)
|
||||
{
|
||||
ValidationRule<TRequest, TProperty> rule = new(propertySelector);
|
||||
_rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that is evaluated against each item within a collection property on the request being validated.
|
||||
/// </summary>
|
||||
/// <typeparam name="TItem">The type of the items in the collection being validated.</typeparam>
|
||||
/// <param name="collectionSelector">A function that selects the collection to validate from the request instance.</param>
|
||||
/// <returns>An <see cref="IRuleBuilder{TRequest, TItem}"/> instance to configure the per-item rule fluently.</returns>
|
||||
protected IRuleBuilder<TRequest, TItem> RuleForEach<TItem>(Func<TRequest, IEnumerable<TItem>?> collectionSelector)
|
||||
{
|
||||
ValidationRuleForEach<TRequest, TItem> rule = new(collectionSelector);
|
||||
_rules.Add(rule);
|
||||
return rule;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates the specified request instance against all defined rules.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>A list of validation errors. If the instance is valid, the list will be empty.</returns>
|
||||
public List<Error> Validate(TRequest instance)
|
||||
{
|
||||
List<Error> errors = [];
|
||||
|
||||
foreach (IValidationRule<TRequest> rule in _rules)
|
||||
errors.AddRange(rule.Validate(instance));
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Provides a fluent API for building validation rules for commands and queries.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being validated.</typeparam>
|
||||
/// <typeparam name="TProperty">The type of the specific property on the request that the rule applies to.</typeparam>
|
||||
public interface IRuleBuilder<TRequest, TProperty>
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies a condition that the property must satisfy to be considered valid.
|
||||
/// </summary>
|
||||
/// <param name="predicate">A function to test the property value.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
IRuleBuilder<TRequest, TProperty> Must(Func<TProperty, bool> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a condition that must be met for the validation rule to be executed.
|
||||
/// </summary>
|
||||
/// <param name="condition">A function evaluating the request instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
IRuleBuilder<TRequest, TProperty> When(Func<TRequest, bool> condition);
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a custom <see cref="Error"/> object to be returned when the validation fails.
|
||||
/// </summary>
|
||||
/// <param name="error">The specific error to return upon failure.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
IRuleBuilder<TRequest, TProperty> WithError(Error error);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for triggering commands and queries validator evaluation.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being evaluated.</typeparam>
|
||||
internal interface IValidationRule<in TRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Evaluates the validation rule against the specified request instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>The <see cref="Error"/>s produced by this rule; empty if the rule passes.</returns>
|
||||
IEnumerable<Error> Validate(TRequest instance);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for validating requests of commands and queries.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) to be validated.</typeparam>
|
||||
public interface IValidator<in TRequest>
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates the specified request instance against the defined rules.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>A list of validation errors. If the instance is valid, the list will be empty.</returns>
|
||||
List<Error> Validate(TRequest instance);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule for commands and queries that evaluates a specific property of a request.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being validated.</typeparam>
|
||||
/// <typeparam name="TProperty">The type of the specific property on the request that the rule applies to.</typeparam>
|
||||
internal sealed class ValidationRule<TRequest, TProperty> : IValidationRule<TRequest>, IRuleBuilder<TRequest, TProperty>
|
||||
{
|
||||
private readonly Func<TRequest, TProperty> _propertySelector;
|
||||
private readonly List<Func<TProperty, bool>> _predicates = [];
|
||||
private Error error = Error.Validation();
|
||||
private Func<TRequest, bool>? condition;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationRule{TRequest, TProperty}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="propertySelector">A function that extracts the property value from the request instance.</param>
|
||||
public ValidationRule(Func<TRequest, TProperty> propertySelector)
|
||||
{
|
||||
_propertySelector = propertySelector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a condition that the property must satisfy to be considered valid.
|
||||
/// </summary>
|
||||
/// <param name="predicate">A function to test the property value.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TProperty> Must(Func<TProperty, bool> predicate)
|
||||
{
|
||||
_predicates.Add(predicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a condition that must be met for the validation rule to be executed.
|
||||
/// </summary>
|
||||
/// <param name="condition">A function evaluating the request instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TProperty> When(Func<TRequest, bool> condition)
|
||||
{
|
||||
this.condition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a custom <see cref="Error"/> object to be returned when the validation fails.
|
||||
/// </summary>
|
||||
/// <param name="error">The specific error to return upon failure.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TProperty> WithError(Error error)
|
||||
{
|
||||
this.error = error;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates all configured predicates against the specified request instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>The <see cref="Error"/> if any predicate fails; empty if the rule passes.</returns>
|
||||
public IEnumerable<Error> Validate(TRequest instance)
|
||||
{
|
||||
// evaluate the condition first, if it returns false, skip validation entirely.
|
||||
if (condition is not null && !condition(instance))
|
||||
yield break;
|
||||
|
||||
TProperty value = _propertySelector(instance);
|
||||
|
||||
foreach (Func<TProperty, bool> predicate in _predicates)
|
||||
{
|
||||
if (!predicate(value))
|
||||
{
|
||||
yield return error;
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Validation;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule for commands and queries that evaluates each item within a collection property of a request.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request (command or query) being validated.</typeparam>
|
||||
/// <typeparam name="TItem">The type of the items in the collection that the rule applies to.</typeparam>
|
||||
internal sealed class ValidationRuleForEach<TRequest, TItem> : IValidationRule<TRequest>, IRuleBuilder<TRequest, TItem>
|
||||
{
|
||||
private readonly Func<TRequest, IEnumerable<TItem>?> _collectionSelector;
|
||||
private readonly List<Func<TItem, bool>> _predicates = [];
|
||||
private Error error = Error.Validation();
|
||||
private Func<TRequest, bool>? condition;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationRuleForEach{TRequest, TItem}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="collectionSelector">A function that extracts the collection to iterate from the request instance.</param>
|
||||
public ValidationRuleForEach(Func<TRequest, IEnumerable<TItem>?> collectionSelector)
|
||||
{
|
||||
_collectionSelector = collectionSelector;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a condition that each item in the collection must satisfy to be considered valid.
|
||||
/// </summary>
|
||||
/// <param name="predicate">A function to test each item value.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TItem> Must(Func<TItem, bool> predicate)
|
||||
{
|
||||
_predicates.Add(predicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a condition that must be met for the validation rule to be executed.
|
||||
/// </summary>
|
||||
/// <param name="condition">A function evaluating the request instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TItem> When(Func<TRequest, bool> condition)
|
||||
{
|
||||
this.condition = condition;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies a custom <see cref="Error"/> object to be returned when the validation fails.
|
||||
/// </summary>
|
||||
/// <param name="error">The specific error to return upon failure.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TItem}"/> instance for method chaining.</returns>
|
||||
public IRuleBuilder<TRequest, TItem> WithError(Error error)
|
||||
{
|
||||
this.error = error;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates all configured predicates against each item in the collection.
|
||||
/// </summary>
|
||||
/// <param name="instance">The request instance to validate.</param>
|
||||
/// <returns>One <see cref="Error"/> per item that fails a predicate; empty if every item passes.</returns>
|
||||
public IEnumerable<Error> Validate(TRequest instance)
|
||||
{
|
||||
// evaluate the condition first, if it returns false, skip validation entirely.
|
||||
if (condition is not null && !condition(instance))
|
||||
yield break;
|
||||
|
||||
IEnumerable<TItem>? collection = _collectionSelector(instance);
|
||||
if (collection is null)
|
||||
yield break;
|
||||
|
||||
foreach (TItem item in collection)
|
||||
{
|
||||
foreach (Func<TItem, bool> predicate in _predicates)
|
||||
{
|
||||
if (!predicate(item))
|
||||
{
|
||||
yield return error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Validator extension methods for the <see cref="IRuleBuilder{TRequest, TProperty}"/> to define common validation rules.
|
||||
/// </summary>
|
||||
public static class ValidatorUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the integer property is greater than the specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <param name="valueToCompare">The value that the property must be greater than.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, int> GreaterThan<TRequest>(this IRuleBuilder<TRequest, int> ruleBuilder, int valueToCompare)
|
||||
{
|
||||
return ruleBuilder.Must(value => value > valueToCompare);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the nullable integer property is greater than the specified value.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If the property is <see langword="null"/>, this rule passes (use <see cref="NotNull{TRequest, TProperty}(IRuleBuilder{TRequest, TProperty})"/> to check for missing values).
|
||||
/// </remarks>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <param name="valueToCompare">The value that the property must be greater than.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, int?> GreaterThan<TRequest>(this IRuleBuilder<TRequest, int?> ruleBuilder, int valueToCompare)
|
||||
{
|
||||
return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the time span property is greater than the specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <param name="valueToCompare">The value that the property must be greater than.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, TimeSpan> GreaterThan<TRequest>(this IRuleBuilder<TRequest, TimeSpan> ruleBuilder, TimeSpan valueToCompare)
|
||||
{
|
||||
return ruleBuilder.Must(value => value > valueToCompare);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the property is not <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This rule checks reference/nullable-value nullness only. Combine with
|
||||
/// <see cref="NotEmpty{TRequest}(IRuleBuilder{TRequest, string})"/> or
|
||||
/// <see cref="NotEmpty{TRequest, TItem}(IRuleBuilder{TRequest, List{TItem}})"/> to also reject empty values.
|
||||
/// </remarks>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <typeparam name="TProperty">The type of the property being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, TProperty> NotNull<TRequest, TProperty>(this IRuleBuilder<TRequest, TProperty> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must(value => value is not null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the string property is not empty or consisting only of white-space characters.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If the property is <see langword="null"/>, this rule passes (use <see cref="NotNull{TRequest, TProperty}(IRuleBuilder{TRequest, TProperty})"/> to check for missing values).
|
||||
/// </remarks>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, string> NotEmpty<TRequest>(this IRuleBuilder<TRequest, string> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must(value => value is null || value.Trim().Length > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the List property is not empty.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If the property is <see langword="null"/>, this rule passes (use <see cref="NotNull{TRequest, TProperty}(IRuleBuilder{TRequest, TProperty})"/> to check for missing values).
|
||||
/// </remarks>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <typeparam name="TItem">The type of the items in the list being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, List<TItem>> NotEmpty<TRequest, TItem>(this IRuleBuilder<TRequest, List<TItem>> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must(value => value is null || value.Count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the <see cref="Guid"/> property is not <see cref="Guid.Empty"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, Guid> NotEmpty<TRequest>(this IRuleBuilder<TRequest, Guid> ruleBuilder)
|
||||
{
|
||||
return ruleBuilder.Must(value => value != Guid.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures the value is a defined member of the specified enumeration type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <typeparam name="TEnum">The type of the enumeration.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, TEnum> IsInEnum<TRequest, TEnum>(this IRuleBuilder<TRequest, TEnum> ruleBuilder) where TEnum : struct, Enum
|
||||
{
|
||||
return ruleBuilder.Must(value => Enum.IsDefined(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines a validation rule that ensures a nullable enum value is either <see langword="null"/> or a defined member of the specified enumeration type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <typeparam name="TEnum">The type of the enumeration.</typeparam>
|
||||
/// <param name="ruleBuilder">The rule builder instance.</param>
|
||||
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
|
||||
public static IRuleBuilder<TRequest, TEnum?> IsInEnum<TRequest, TEnum>(this IRuleBuilder<TRequest, TEnum?> ruleBuilder) where TEnum : struct, Enum
|
||||
{
|
||||
return ruleBuilder.Must(value => value.HasValue && Enum.IsDefined(value.Value));
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -1,7 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Mediator;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -18,4 +16,4 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
|
||||
public record AddRoleCommand(
|
||||
string RoleName,
|
||||
List<Guid> Permissions
|
||||
) : IRequest<ErrorOr<RolePermissionsResponse>>;
|
||||
) : ICommand;
|
||||
+15
-6
@@ -1,14 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.Authorization;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Mediator;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -19,11 +21,12 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
|
||||
/// <summary>
|
||||
/// Handler for the command to add an authorization role.
|
||||
/// </summary>
|
||||
public class AddRoleCommandHandler : IRequestHandler<AddRoleCommand, ErrorOr<RolePermissionsResponse>>
|
||||
public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, ErrorOr<RolePermissionsResponse>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IValidator<AddRoleCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AddRoleCommandHandler"/> class.
|
||||
@@ -31,11 +34,13 @@ public class AddRoleCommandHandler : IRequestHandler<AddRoleCommand, ErrorOr<Rol
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
public AddRoleCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public AddRoleCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork, IValidator<AddRoleCommand> validator)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -46,8 +51,12 @@ public class AddRoleCommandHandler : IRequestHandler<AddRoleCommand, ErrorOr<Rol
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully created <see cref="RoleResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async ValueTask<ErrorOr<RolePermissionsResponse>> Handle(AddRoleCommand request, CancellationToken cancellationToken)
|
||||
public async Task<ErrorOr<RolePermissionsResponse>> HandleAsync(AddRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(request);
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// only admins can create authorization roles
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin)
|
||||
@@ -59,13 +68,13 @@ public class AddRoleCommandHandler : IRequestHandler<AddRoleCommand, ErrorOr<Rol
|
||||
RoleEntity newRole = new()
|
||||
{
|
||||
RoleName = request.RoleName,
|
||||
RolePermissions = request.Permissions.Select(permissionId => new RolePermissionEntity()
|
||||
RolePermissions = [.. request.Permissions.Select(permissionId => new RolePermissionEntity()
|
||||
{
|
||||
PermissionId = permissionId,
|
||||
Permission = null!,
|
||||
Role = null!,
|
||||
RoleId = default
|
||||
}).ToList()
|
||||
})]
|
||||
};
|
||||
// save the new role in the repository
|
||||
ErrorOr<Created> insertRoleResult = await roleRepository.InsertAsync(newRole, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
+9
-7
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
@@ -17,14 +18,15 @@ public class AddRoleCommandValidator : AbstractValidator<AddRoleCommand>
|
||||
public AddRoleCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.RoleName)
|
||||
.NotNull().WithMessage(Errors.Authorization.RoleNameCannotBeNull.Description)
|
||||
.NotEmpty().WithMessage(Errors.Authorization.RoleNameCannotBeEmpty.Description);
|
||||
.NotNull().WithError(Errors.Authorization.RoleNameCannotBeNull)
|
||||
.NotEmpty().WithError(Errors.Authorization.RoleNameCannotBeEmpty);
|
||||
|
||||
RuleFor(x => x.Permissions)
|
||||
.NotNull().WithMessage(Errors.Authorization.PermissionsListCannotBeNull.Description)
|
||||
.NotEmpty().WithMessage(Errors.Authorization.PermissionsListCannotBeEmpty.Description);
|
||||
.NotNull().WithError(Errors.Authorization.PermissionsListCannotBeNull)
|
||||
.NotEmpty().WithError(Errors.Authorization.PermissionsListCannotBeEmpty);
|
||||
|
||||
RuleForEach(x => x.Permissions)
|
||||
.NotEmpty().WithMessage(Errors.Authorization.PermissionIdCannotBeEmpty.Description)
|
||||
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.PermissionIdCannotBeEmpty.Description);
|
||||
.NotEmpty().WithError(Errors.Authorization.PermissionIdCannotBeEmpty)
|
||||
.Must(id => id != Guid.Empty).WithError(Errors.Authorization.PermissionIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -1,7 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Mediator;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
@@ -15,4 +13,4 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermi
|
||||
[DebuggerDisplay("RoleId: {RoleId}")]
|
||||
public record GetRolePermissionsQuery(
|
||||
Guid RoleId
|
||||
) : IRequest<ErrorOr<RolePermissionsResponse>>;
|
||||
) : IQuery;
|
||||
+13
-4
@@ -1,14 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.Authorization;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Mediator;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -18,11 +20,12 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermi
|
||||
/// <summary>
|
||||
/// Handler for the query to retrieve the list of authorization permissions for a role.
|
||||
/// </summary>
|
||||
public class GetRolePermissionsQueryHandler : IRequestHandler<GetRolePermissionsQuery, ErrorOr<RolePermissionsResponse>>
|
||||
public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQuery, ErrorOr<RolePermissionsResponse>>
|
||||
{
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IRoleRepository _roleRepository;
|
||||
private readonly IValidator<GetRolePermissionsQuery> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetRolePermissionsQueryHandler"/> class.
|
||||
@@ -30,11 +33,13 @@ public class GetRolePermissionsQueryHandler : IRequestHandler<GetRolePermissions
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
public GetRolePermissionsQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public GetRolePermissionsQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork, IValidator<GetRolePermissionsQuery> validator)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,8 +50,12 @@ public class GetRolePermissionsQueryHandler : IRequestHandler<GetRolePermissions
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async ValueTask<ErrorOr<RolePermissionsResponse>> Handle(GetRolePermissionsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<ErrorOr<RolePermissionsResponse>> HandleAsync(GetRolePermissionsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(request);
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// only admins can see the list of authorization roles
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin)
|
||||
|
||||
+4
-3
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
@@ -17,7 +18,7 @@ public class GetRolePermissionsQueryValidator : AbstractValidator<GetRolePermiss
|
||||
public GetRolePermissionsQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.RoleId)
|
||||
.NotEmpty().WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description)
|
||||
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description);
|
||||
.NotEmpty().WithError(Errors.Authorization.RoleIdCannotBeEmpty)
|
||||
.Must(id => id != Guid.Empty).WithError(Errors.Authorization.RoleIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
@@ -32,4 +32,8 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Common\CQRS\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,59 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.Utilities;
|
||||
|
||||
/// <summary>
|
||||
/// Exception extension methods.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Based on https://stackoverflow.com/a/62283895.
|
||||
/// </remarks>
|
||||
public static class ExceptionUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Concatenates all inner exception(s) messages of <paramref name="ex"/>.
|
||||
/// </summary>
|
||||
/// <param name="ex">The exception for which to get the inner exception(s) message.</param>
|
||||
/// <returns>A string representing the concatenated messages of all inner expcetion(s) of <paramref name="ex"/>.</returns>
|
||||
public static string AggregateMessages(this Exception ex)
|
||||
{
|
||||
return ex.GetInnerExceptions().Aggregate(new StringBuilder(), (stringBuilder, exception) => stringBuilder.AppendLine(exception.Message + " -> "), stringBuilder => stringBuilder.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively gets the inner exception(s) of <paramref name="exception"/>, at <paramref name="maximumDepth"/> depth.
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception for which to get the inner exception(s).</param>
|
||||
/// <param name="maximumDepth">The depth of the recursivity of getting the inner exception(s) of <paramref name="exception"/>.</param>
|
||||
/// <returns>A list of exceptions representing the list of inner exception(s) of <paramref name="exception"/>.</returns>
|
||||
public static IEnumerable<Exception> GetInnerExceptions(this Exception exception, int maximumDepth = 5)
|
||||
{
|
||||
// check if there are any more inner exceptions to return
|
||||
if (exception == null || maximumDepth <= 0)
|
||||
yield break;
|
||||
// yield the current level exception itself
|
||||
yield return exception;
|
||||
// if the exception is an AggregateException, treat it differently and get its all exceptions
|
||||
if (exception is AggregateException aggregateException)
|
||||
foreach (Exception? innerException in aggregateException.InnerExceptions.SelectMany(innerException => innerException.GetInnerExceptions(maximumDepth - 1)))
|
||||
yield return innerException;
|
||||
// if it's a normal exception, recursively get its list of inner exceptions and yield them
|
||||
foreach (Exception innerException in exception.InnerException?.GetInnerExceptions(maximumDepth - 1) ?? [])
|
||||
yield return innerException;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concatenates the relevant properties of <paramref name="ex"/>.
|
||||
/// </summary>
|
||||
/// <param name="ex">The exception for which to get the relevant properties.</param>
|
||||
/// <returns>A string representing the concatenated relevant properties of <paramref name="ex"/>.</returns>
|
||||
public static string AggregateMessage(this Exception ex)
|
||||
{
|
||||
return $"Message: {ex.Message}; CallStack: {ex.StackTrace}";
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -1,12 +1,12 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Authorization;
|
||||
using Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
|
||||
using Lumina.Contracts.Requests.Authorization;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Mediator;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -19,15 +19,15 @@ namespace Lumina.Presentation.Api.Core.Endpoints.Admin.Authorization.Roles.AddRo
|
||||
/// </summary>
|
||||
public class AddRoleEndpoint : BaseEndpoint<AddRoleRequest, IResult>
|
||||
{
|
||||
private readonly ISender _sender;
|
||||
private readonly ICommandHandler<AddRoleCommand, ErrorOr<RolePermissionsResponse>> _addRoleCommandHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AddRoleEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="sender">Injected service for mediating commands and queries.</param>
|
||||
public AddRoleEndpoint(ISender sender)
|
||||
/// <param name="addRoleCommandHandler">Injected service for handling add role commands.</param>
|
||||
public AddRoleEndpoint(ICommandHandler<AddRoleCommand, ErrorOr<RolePermissionsResponse>> addRoleCommandHandler)
|
||||
{
|
||||
_sender = sender;
|
||||
_addRoleCommandHandler = addRoleCommandHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -35,7 +35,7 @@ public class AddRoleEndpoint : BaseEndpoint<AddRoleRequest, IResult>
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(Http.POST);
|
||||
Verbs(FastEndpoints.Http.POST);
|
||||
Routes(ApiRoutes.Roles.CREATE_ROLE);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
@@ -48,7 +48,7 @@ public class AddRoleEndpoint : BaseEndpoint<AddRoleRequest, IResult>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(AddRoleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
ErrorOr<RolePermissionsResponse> result = await _sender.Send(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
ErrorOr<RolePermissionsResponse> result = await _addRoleCommandHandler.HandleAsync(request.ToCommand(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -1,12 +1,12 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using FastEndpoints;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.Mapping.Authorization;
|
||||
using Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermissions;
|
||||
using Lumina.Contracts.Requests.Authorization;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
|
||||
using Lumina.Presentation.Api.Core.Endpoints.Common;
|
||||
using Mediator;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -19,15 +19,15 @@ namespace Lumina.Presentation.Api.Core.Endpoints.Admin.Authorization.Roles.GetRo
|
||||
/// </summary>
|
||||
public class GetRolePermissionsEndpoint : BaseEndpoint<GetRolePermissionsRequest, IResult>
|
||||
{
|
||||
private readonly ISender _sender;
|
||||
private readonly IQueryHandler<GetRolePermissionsQuery, ErrorOr<RolePermissionsResponse>> _addRoleQueryHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetRolePermissionsEndpoint"/> class.
|
||||
/// </summary>
|
||||
/// <param name="sender">Injected service for mediating commands and queries.</param>
|
||||
public GetRolePermissionsEndpoint(ISender sender)
|
||||
/// <param name="addRoleQueryHandler">Injected service for handling add role commands.</param>
|
||||
public GetRolePermissionsEndpoint(IQueryHandler<GetRolePermissionsQuery, ErrorOr<RolePermissionsResponse>> addRoleQueryHandler)
|
||||
{
|
||||
_sender = sender;
|
||||
_addRoleQueryHandler = addRoleQueryHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -35,7 +35,7 @@ public class GetRolePermissionsEndpoint : BaseEndpoint<GetRolePermissionsRequest
|
||||
/// </summary>
|
||||
public override void Configure()
|
||||
{
|
||||
Verbs(Http.GET);
|
||||
Verbs(FastEndpoints.Http.GET);
|
||||
Routes(ApiRoutes.Roles.GET_ROLE_PERMISSIONS_BY_ROLE_ID);
|
||||
Version(1);
|
||||
DontCatchExceptions();
|
||||
@@ -48,7 +48,7 @@ public class GetRolePermissionsEndpoint : BaseEndpoint<GetRolePermissionsRequest
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public override async Task<IResult> ExecuteAsync(GetRolePermissionsRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
ErrorOr<RolePermissionsResponse> result = await _sender.Send(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
ErrorOr<RolePermissionsResponse> result = await _addRoleQueryHandler.HandleAsync(request.ToQuery(), cancellationToken).ConfigureAwait(false);
|
||||
return result.Match(success => TypedResults.Ok(success), Problem);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
@@ -13,7 +13,10 @@
|
||||
<DockerfileTag>lumina-api</DockerfileTag>
|
||||
<DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\AssemblyInfo_Shared.cs" Link="Properties\AssemblyInfo_Shared.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -191,6 +191,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "architecture-decision-log",
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0001.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0001.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0002.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0002.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0003.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0003.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0004.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0004.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0005.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0005.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0006.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0006.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0007.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0007.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0008.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0008.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0009.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0009.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0010.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0010.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0011.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0011.md
|
||||
..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0012.md = ..\docs\technical\architecture\architecture-knowledge-management\architecture-decision-log\architecture-decision-record-0012.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lumina.Plugins.Contracts", "Lumina.Plugins.Contracts\Lumina.Plugins.Contracts.csproj", "{8CF6FEA4-624C-4EC9-93D6-EB7626059D7C}"
|
||||
|
||||
Reference in new issue
Block a user