38 lines
2.0 KiB
C#
38 lines
2.0 KiB
C#
using ItWorx.KnowledgeHub.Application.Common;
|
|
using ItWorx.KnowledgeHub.Domain.Ingestion;
|
|
|
|
namespace ItWorx.KnowledgeHub.Application.Ingestion.UseCases;
|
|
|
|
public sealed record CreateIngestionProposalCommand(Guid WorkspaceId, Guid ActorId, string Manifest, string CorrelationId,
|
|
string? IdempotencyKey, IngestionPublication? Publication = null,
|
|
int MaxPublishRetries = IngestionProposal.DefaultMaxPublishRetries);
|
|
|
|
public sealed class CreateIngestionProposalUseCase(IIngestionProposalRepository proposals, TimeProvider timeProvider)
|
|
{
|
|
public async Task<OperationResultValue<Guid>> ExecuteAsync(CreateIngestionProposalCommand command, CancellationToken cancellationToken)
|
|
{
|
|
if (command.WorkspaceId == Guid.Empty || command.ActorId == Guid.Empty || string.IsNullOrWhiteSpace(command.Manifest)
|
|
|| command.Manifest.Length > 100_000 || command.MaxPublishRetries is < 1 or > 3)
|
|
return OperationResultValue.Failure<Guid>(OperationStatus.ValidationFailed, ["Ongeldig voorstel."]);
|
|
|
|
if (!string.IsNullOrWhiteSpace(command.IdempotencyKey))
|
|
{
|
|
var existing = await proposals.FindByIdempotencyKeyAsync(command.WorkspaceId, command.IdempotencyKey, cancellationToken);
|
|
if (existing is not null)
|
|
return OperationResultValue.Success(existing.Id);
|
|
}
|
|
|
|
var proposal = IngestionProposal.Create(command.WorkspaceId, command.ActorId,
|
|
string.IsNullOrWhiteSpace(command.CorrelationId) ? "ingestion" : command.CorrelationId,
|
|
command.Manifest, command.MaxPublishRetries);
|
|
if (!string.IsNullOrWhiteSpace(command.IdempotencyKey)) proposal.AssignIdempotencyKey(command.IdempotencyKey);
|
|
if (command.Publication is not null)
|
|
{
|
|
proposal.SetPublication(command.Publication, command.ActorId, timeProvider.GetUtcNow());
|
|
}
|
|
proposals.Add(proposal);
|
|
await proposals.SaveChangesAsync(cancellationToken);
|
|
return OperationResultValue.Success(proposal.Id);
|
|
}
|
|
}
|