using Microsoft.EntityFrameworkCore;
using NetinaShop.Domain.Entities.ProductCategories;

namespace NetinaShop.Repository.Handlers.ProductCategories;

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

    public GetProductCategoryQueryHandler(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(ProductCategoryMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (ent == null)
            throw new AppException("ProductCategory not found", ApiResultStatusCode.NotFound);

        //ent.

        return ent;
    }
}