using NetinaShop.Domain.Entities.ProductCategories;

namespace NetinaShop.Repository.Handlers.ProductCategories;

public class GetCategoryQueryHandler : IRequestHandler<GetProductCategoryQuery, ProductCategoryLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetCategoryQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<ProductCategoryLDto> Handle(GetProductCategoryQuery request, CancellationToken cancellationToken)
    {
        var ent = await _repositoryWrapper.SetRepository<ProductCategory>().TableNoTracking
            .Where(b => b.Id == request.Id)
            .Select(CategoryMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (ent == null)
            throw new AppException("ProductCategory not found", ApiResultStatusCode.NotFound);
        return ent;
    }
}