using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.TagCQ.Commands.CreateNewTag;
public class CreateNewTagCommandHandler : IRequestHandler<CreateNewTagCommand, CreateNewTagResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewTagResponseDto> Handle(CreateNewTagCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<Tag>();
        await _context.Tags.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewTagEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewTagResponseDto>();
    }
}

