Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

ModelCacheEventConsumer.cs

using Nop.Core.Caching;
using Nop.Core.Domain.Tax;
using Nop.Core.Events;
using Nop.Plugin.Tax.FixedOrByCountryStateZip.Domain;
using Nop.Plugin.Tax.FixedOrByCountryStateZip.Services;
using Nop.Services.Configuration;
using Nop.Services.Events;

namespace Nop.Plugin.Tax.FixedOrByCountryStateZip.Infrastructure.Cache;

/// 
/// Model cache event consumer (used for caching of presentation layer models)
/// 
public class ModelCacheEventConsumer :
    //tax rates
    IConsumer>,
    IConsumer>,
    IConsumer>,
    //tax category
    IConsumer>
{
    #region Constants

    /// 
    /// Key for caching all tax rates
    /// 
    public static CacheKey ALL_TAX_RATES_MODEL_KEY = new("Nop.plugins.tax.fixedorbycountrystateziptaxrate.all", TAXRATE_PATTERN_KEY);
    public static CacheKey TAXRATE_ALL_KEY = new("Nop.plugins.tax.fixedorbycountrystateziptaxrate.taxrate.all", TAXRATE_PATTERN_KEY);

    public const string TAXRATE_PATTERN_KEY = "Nop.plugins.tax.fixedorbycountrystateziptaxrate.";

    #endregion

    #region Fields

    protected readonly ICountryStateZipService _taxRateService;
    protected readonly ISettingService _settingService;
    protected readonly IStaticCacheManager _staticCacheManager;

    #endregion

    #region Ctor

    public ModelCacheEventConsumer(ICountryStateZipService taxRateService,
        ISettingService settingService,
        IStaticCacheManager staticCacheManager)
    {
        _taxRateService = taxRateService;
        _settingService = settingService;
        _staticCacheManager = staticCacheManager;
    }

    #endregion

    #region Methods

    /// 
    /// Handle tax rate inserted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        //clear cache
        await _staticCacheManager.RemoveByPrefixAsync(TAXRATE_PATTERN_KEY);
    }

    /// 
    /// Handle tax rate updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        //clear cache
        await _staticCacheManager.RemoveByPrefixAsync(TAXRATE_PATTERN_KEY);
    }

    /// 
    /// Handle tax rate deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        //clear cache
        await _staticCacheManager.RemoveByPrefixAsync(TAXRATE_PATTERN_KEY);
    }

    /// 
    /// Handle tax category deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        var taxCategory = eventMessage?.Entity;
        if (taxCategory == null)
            return;

        //delete an appropriate record when tax category is deleted
        var recordsToDelete = (await _taxRateService.GetAllTaxRatesAsync()).Where(taxRate => taxRate.TaxCategoryId == taxCategory.Id).ToList();
        foreach (var taxRate in recordsToDelete)
        {
            await _taxRateService.DeleteTaxRateAsync(taxRate);
        }

        //delete saved fixed rate if exists
        var setting = await _settingService.GetSettingAsync(string.Format(FixedOrByCountryStateZipDefaults.FIXED_RATE_SETTINGS_KEY, taxCategory.Id));
        if (setting != null)
            await _settingService.DeleteSettingAsync(setting);
    }

    #endregion
}