namespace Brizco.Repository.Handlers.Complex;

public class GetComplexQueryHandler : IRequestHandler<GetComplexQuery, ComplexSDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetComplexQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }

    public async Task<ComplexSDto> Handle(GetComplexQuery request, CancellationToken cancellationToken)
    {
        var complex = await _repositoryWrapper.SetRepository<Domain.Entities.Complex.Complex>()
            .TableNoTracking
            .Where(s => s.Id == request.Id)
            .Select(ComplexMapper.ProjectToSDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (complex == null)
            throw new AppException("Complex not found", ApiResultStatusCode.NotFound);
        return complex;
    }
}