using NetinaCMS.Repository.Repositories.Base.Contracts;

namespace NetinaCMS.Repository.Handlers.BlogCategories;

public class GetBlogCategoryCommandHandler : IRequestHandler<GetBlogCategoryQuery,BlogCategoryLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetBlogCategoryCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<BlogCategoryLDto> Handle(GetBlogCategoryQuery request, CancellationToken cancellationToken)
    {
        var ent = await _repositoryWrapper.SetRepository<BlogCategory>()
            .TableNoTracking
            .Where(bc => bc.Id == request.Id)
            .Select(BlogCategoryMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (ent == null)
            throw new AppException("BlogCategory not found", ApiResultStatusCode.NotFound);

        return ent;
    }
}