Add GetOrCreateCommandHandler for endpoint management

Introduced `GetOrCreateCommandHandler` to handle `GetOrCreateCommand` requests.
The handler checks if an `Endpoint` with the specified `Uri` exists in the repository
and returns it if found. If not, it creates a new `Endpoint`, saves it, and returns
the newly created object. Added necessary `using` directives for repository abstraction
and Entity Framework Core functionality.
This commit is contained in:
tekh 2025-12-01 15:41:38 +01:00
parent 594057c4db
commit 79e5097590

View File

@ -1,4 +1,6 @@
using MediatR;
using DigitalData.Core.Abstraction.Application.Repository;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ReC.Domain.Entities;
namespace ReC.Application.Endpoints.Commands;
@ -7,3 +9,21 @@ public class GetOrCreateCommand : IRequest<Endpoint>
{
public string Uri { get; init; } = null!;
}
public class GetOrCreateCommandHandler(IRepository<Endpoint> repo) : IRequestHandler<GetOrCreateCommand, Endpoint>
{
public async Task<Endpoint> Handle(GetOrCreateCommand request, CancellationToken cancel)
{
var endpoint = await repo.Where(e => e.Uri == request.Uri).FirstOrDefaultAsync(cancel);
if (endpoint is not null)
return endpoint;
endpoint = new Endpoint
{
Uri = request.Uri
};
await repo.CreateAsync(endpoint, cancel);
return endpoint;
}
}