Updated ObtainEndpointCommandHandler to depend on IRepository<Endpoint> and IMapper. Now maps Endpoint entities to EndpointDto using AutoMapper before returning, ensuring proper separation of concerns and DTO exposure.
27 lines
901 B
C#
27 lines
901 B
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ReC.Application.Common.Dto;
|
|
using ReC.Domain.Entities;
|
|
|
|
namespace ReC.Application.Endpoints.Commands;
|
|
|
|
public class ObtainEndpointCommand : IRequest<EndpointDto>
|
|
{
|
|
public string Uri { get; init; } = null!;
|
|
}
|
|
|
|
public class ObtainEndpointCommandHandler(IRepository<Endpoint> repo, IMapper mapper) : IRequestHandler<ObtainEndpointCommand, EndpointDto>
|
|
{
|
|
public async Task<EndpointDto> Handle(ObtainEndpointCommand request, CancellationToken cancel)
|
|
{
|
|
var endpoint = await repo.Where(e => e.Uri == request.Uri).FirstOrDefaultAsync(cancel);
|
|
|
|
if (endpoint is not null)
|
|
return mapper.Map<EndpointDto>(endpoint);
|
|
|
|
endpoint = await repo.CreateAsync(request, cancel);
|
|
return mapper.Map<EndpointDto>(endpoint);
|
|
}
|
|
} |