using AutoMapper;
using DbFirst.Application.Repositories;
using DbFirst.Contracts.Catalogs;
using MediatR;

namespace DbFirst.Application.Catalogs.Queries;

public class GetAllCatalogsHandler : IRequestHandler<GetAllCatalogsQuery, List<CatalogReadDto>>
{
    private readonly ICatalogRepository _repository;
    private readonly IMapper _mapper;

    public GetAllCatalogsHandler(ICatalogRepository repository, IMapper mapper)
    {
        _repository = repository;
        _mapper = mapper;
    }

    public async Task<List<CatalogReadDto>> Handle(GetAllCatalogsQuery request, CancellationToken cancellationToken)
    {
        var items = await _repository.GetAllAsync(cancellationToken);
        return _mapper.Map<List<CatalogReadDto>>(items);
    }
}
