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

namespace DbFirst.Application.MassData.Queries;

public class GetMassDataByCustomerNameHandler : IRequestHandler<GetMassDataByCustomerNameQuery, MassDataReadDto?>
{
    private readonly IMassDataRepository _repository;
    private readonly IMapper _mapper;

    public GetMassDataByCustomerNameHandler(IMassDataRepository repository, IMapper mapper)
    {
        _repository = repository;
        _mapper = mapper;
    }

    public async Task<MassDataReadDto?> Handle(GetMassDataByCustomerNameQuery request, CancellationToken cancellationToken)
    {
        var item = await _repository.GetByCustomerNameAsync(request.CustomerName, cancellationToken);
        return item == null ? null : _mapper.Map<MassDataReadDto>(item);
    }
}
