Refactor validation behavior and update result view command

Refactored ValidationBehavior to use C# 12 primary constructors and file-scoped namespaces, simplifying the Handle method logic. Updated CreateResultViewCommand to implement IAuthScoped and IRequest, replaced AddedWho with a non-serialized Scope property, and added necessary usings and namespace.
This commit is contained in:
2025-12-17 11:16:48 +01:00
parent cc54539aba
commit 0fa1a418de
2 changed files with 25 additions and 21 deletions

View File

@@ -1,30 +1,29 @@
using FluentValidation; using FluentValidation;
using MediatR; using MediatR;
namespace ReC.Application.Common.Behaviors namespace ReC.Application.Common.Behaviors;
public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{ {
public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse> public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancel)
where TRequest : notnull
{ {
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancel) if (validators.Any())
{ {
if (validators.Any()) var context = new ValidationContext<TRequest>(request);
{
var context = new ValidationContext<TRequest>(request);
var validationResults = await Task.WhenAll( var validationResults = await Task.WhenAll(
validators.Select(v => validators.Select(v =>
v.ValidateAsync(context, cancel))); v.ValidateAsync(context, cancel)));
var failures = validationResults var failures = validationResults
.SelectMany(r => r.Errors) .SelectMany(r => r.Errors)
.Where(f => f != null) .Where(f => f != null)
.ToList(); .ToList();
if (failures.Count != 0) if (failures.Count != 0)
throw new ValidationException(failures); throw new ValidationException(failures);
}
return await next(cancel);
} }
return await next(cancel);
} }
} }

View File

@@ -1,6 +1,10 @@
namespace ReC.Application.ResultViews.Commands; using MediatR;
using ReC.Application.Common.Interfaces;
using System.Text.Json.Serialization;
public class CreateResultViewCommand namespace ReC.Application.ResultViews.Commands;
public class CreateResultViewCommand : IAuthScoped, IRequest
{ {
public required long ActionId { get; set; } public required long ActionId { get; set; }
@@ -10,5 +14,6 @@ public class CreateResultViewCommand
public string? Body { get; set; } public string? Body { get; set; }
public string AddedWho { get; set; } = null!; [JsonIgnore]
public AuthScope Scope { get; } = new();
} }