using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductTagCQ.Commands.DeleteProductTag;
public class DeleteProductTagCommandHandler : IRequestHandler<DeleteProductTagCommand, Unit>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<Unit> Handle(DeleteProductTagCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.ProductTags
            .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(ProductTag), request.Id);
        entity.IsDeleted = true;
        _context.ProductTags.Update(entity);
        entity.AddDomainEvent(new DeleteProductTagEvent(entity));
        await _context.SaveChangesAsync(cancellationToken);
        return Unit.Value;
    }
}

