diff --git a/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs b/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs index f0f5c73..e12d53d 100644 --- a/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs +++ b/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs @@ -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 { public string Uri { get; init; } = null!; } + +public class GetOrCreateCommandHandler(IRepository repo) : IRequestHandler +{ + public async Task 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; + } +} \ No newline at end of file