using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductCategoryCQ.Commands.CreateNewProductCategory;
public class CreateNewProductCategoryCommandHandler : IRequestHandler<CreateNewProductCategoryCommand, CreateNewProductCategoryResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<CreateNewProductCategoryResponseDto> Handle(CreateNewProductCategoryCommand request,
        CancellationToken cancellationToken)
    {
        var entity = request.Adapt<ProductCategory>();
        await _context.ProductCategories.AddAsync(entity, cancellationToken);
        entity.AddDomainEvent(new CreateNewProductCategoryEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return entity.Adapt<CreateNewProductCategoryResponseDto>();
    }
}
