using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.ProductGalleriesCQ.Commands.DeleteProductGalleries;
public class DeleteProductGallerysCommandHandler : IRequestHandler<DeleteProductGalleriesCommand, Unit>
{
    private readonly IApplicationDbContext _context;

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

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