using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.CreateNewProductGalleries;
public class CreateNewProductGallerysCommandHandler : IRequestHandler<CreateNewProductGalleriesCommand, CreateNewProductGallerysResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewProductGallerysResponseDto> Handle(CreateNewProductGalleriesCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<ProductGallery>();
        await _context.ProductGalleries.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewProductGallerysEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewProductGallerysResponseDto>();
    }
}
