Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.4 KB
Raw Blame
using Grand.Data;
using Grand.Domain.Catalog;
using Grand.Domain.Seo;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Events;
using Grand.Mediator;

namespace Grand.Business.Catalog.Events.Handlers;

public class CollectionDeletedEventHandler : INotificationHandler<EntityDeleted<Collection>>
{
    private readonly ICacheBase _cacheBase;
    private readonly IRepository<EntityUrl> _entityUrlRepository;
    private readonly IRepository<Product> _productRepository;

    public CollectionDeletedEventHandler(
        IRepository<EntityUrl> entityUrlRepository,
        IRepository<Product> productRepository,
        ICacheBase cacheBase)
    {
        _entityUrlRepository = entityUrlRepository;
        _productRepository = productRepository;
        _cacheBase = cacheBase;
    }

    public async Task Handle(EntityDeleted<Collection> notification, CancellationToken cancellationToken)
    {
        //delete url
        await _entityUrlRepository.DeleteManyAsync(x =>
            x.EntityId == notification.Entity.Id && x.EntityName == EntityTypes.Collection);

        //delete on the product
        await _productRepository.RemoveCollectionFieldItem(string.Empty, x => x.ProductCollections,
            z => z.CollectionId == notification.Entity.Id);

        //clear cache
        await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_PATTERN_KEY);
    }
}