Add handler for CreateRecActionCommand with validation
Updated `CreateRecActionCommand` to implement `IRequest` for MediatR compatibility. Introduced `CreateRecActionCommandHandler` to process requests, including validation for `EndpointId` and `EndpointUri`. Added logic to fetch or create an endpoint when `EndpointUri` is provided. Updated `EndpointId` property to be mutable. Added necessary `using` directives and adjusted the namespace declaration.
This commit is contained in:
parent
c6f2437300
commit
d30feb6034
@ -1,12 +1,18 @@
|
||||
namespace ReC.Application.RecActions.Commands;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Exceptions;
|
||||
using MediatR;
|
||||
using ReC.Application.Endpoints.Commands;
|
||||
using ReC.Domain.Entities;
|
||||
|
||||
public record CreateRecActionCommand
|
||||
namespace ReC.Application.RecActions.Commands;
|
||||
|
||||
public record CreateRecActionCommand : IRequest
|
||||
{
|
||||
public long ProfileId { get; init; }
|
||||
|
||||
public bool Active { get; init; } = true;
|
||||
|
||||
public long? EndpointId { get; init; }
|
||||
public long? EndpointId { get; set; }
|
||||
|
||||
public string? EndpointUri { get; init; }
|
||||
|
||||
@ -15,4 +21,19 @@ public record CreateRecActionCommand
|
||||
public string? HeaderQuery { get; init; }
|
||||
|
||||
public string BodyQuery { get; init; } = "SELECT NULL AS REQUEST_BODY;";
|
||||
}
|
||||
|
||||
public class CreateRecActionCommandHandler(ISender sender) : IRequestHandler<CreateRecActionCommand>
|
||||
{
|
||||
public async Task Handle(CreateRecActionCommand request, CancellationToken cancel)
|
||||
{
|
||||
if(request.EndpointId is null)
|
||||
if(request.EndpointUri is string endpointUri)
|
||||
{
|
||||
var endpoint = await sender.Send(new GetOrCreateCommand { Uri = endpointUri }, cancel);
|
||||
request.EndpointId = endpoint.Id;
|
||||
}
|
||||
else
|
||||
throw new BadRequestException("Either EndpointId or EndpointUri must be provided.");
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user