using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.RoleCQ.Commands.CreateNewRole;
public class CreateNewRoleCommandHandler : IRequestHandler<CreateNewRoleCommand, CreateNewRoleResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewRoleResponseDto> Handle(CreateNewRoleCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<Role>();
        await _context.Roles.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewRoleEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewRoleResponseDto>();
    }
}
