using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductTagCQ.Commands.CreateNewProductTag;
public class CreateNewProductTagCommandHandler : IRequestHandler<CreateNewProductTagCommand, CreateNewProductTagResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewProductTagResponseDto> Handle(CreateNewProductTagCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<ProductTag>();
        await _context.ProductTags.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewProductTagEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewProductTagResponseDto>();
    }
}

