using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductImagesCQ.Commands.CreateNewProductImages;
public class CreateNewProductImagesCommandHandler : IRequestHandler<CreateNewProductImagesCommand, CreateNewProductImagesResponseDto>
{
    private readonly IApplicationDbContext _context;

    public CreateNewProductImagesCommandHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<CreateNewProductImagesResponseDto> Handle(CreateNewProductImagesCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<ProductImage>();
        await _context.ProductImages.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewProductImagesEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewProductImagesResponseDto>();
    }
}
