Replaced `GetOrCreateEndpointCommand` with `ObtainEndpointCommand` to improve clarity and align with naming conventions. Removed `GetOrCreateEndpointCommand` and its handler, and introduced `ObtainEndpointCommand` with equivalent functionality. Updated `MappingProfile.cs` to map `ObtainEndpointCommand` to `Endpoint`. Refactored `CreateRecActionCommand.cs` to use the new `ObtainEndpointCommand` for retrieving or creating `Endpoint` entities. These changes ensure consistent naming and maintain the same behavior while improving code readability.
25 lines
774 B
C#
25 lines
774 B
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ReC.Domain.Entities;
|
|
|
|
namespace ReC.Application.Endpoints.Commands;
|
|
|
|
public class ObtainEndpointCommand : IRequest<Endpoint>
|
|
{
|
|
public string Uri { get; init; } = null!;
|
|
}
|
|
|
|
public class ObtainEndpointCommandHandler(IRepository<Endpoint> repo) : IRequestHandler<ObtainEndpointCommand, Endpoint>
|
|
{
|
|
public async Task<Endpoint> Handle(ObtainEndpointCommand request, CancellationToken cancel)
|
|
{
|
|
var endpoint = await repo.Where(e => e.Uri == request.Uri).FirstOrDefaultAsync(cancel);
|
|
|
|
if (endpoint is not null)
|
|
return endpoint;
|
|
|
|
endpoint = await repo.CreateAsync(request, cancel);
|
|
return endpoint;
|
|
}
|
|
} |