using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.CategoryCQ.Commands.CreateNewCategory;
public class CreateNewCategoryCommandHandler : IRequestHandler<CreateNewCategoryCommand, CreateNewCategoryResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewCategoryResponseDto> Handle(CreateNewCategoryCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<Category>();
        await _context.Categories.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewCategoryEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewCategoryResponseDto>();
    }
}

