Webiant Logo Webiant Logo
  1. No results found.

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

StateProvinceService.cs

using Nop.Core.Caching;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Directory;
using Nop.Data;
using Nop.Services.Localization;

namespace Nop.Services.Directory;

/// 
/// State province service
/// 
public partial class StateProvinceService : IStateProvinceService
{
    #region Fields

    protected readonly IStaticCacheManager _staticCacheManager;
    protected readonly ILocalizationService _localizationService;
    protected readonly IRepository _stateProvinceRepository;

    #endregion

    #region Ctor

    public StateProvinceService(IStaticCacheManager staticCacheManager,
        ILocalizationService localizationService,
        IRepository stateProvinceRepository)
    {
        _staticCacheManager = staticCacheManager;
        _localizationService = localizationService;
        _stateProvinceRepository = stateProvinceRepository;
    }

    #endregion

    #region Methods
    /// 
    /// Deletes a state/province
    /// 
    /// The state/province
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteStateProvinceAsync(StateProvince stateProvince)
    {
        await _stateProvinceRepository.DeleteAsync(stateProvince);
    }

    /// 
    /// Gets a state/province
    /// 
    /// The state/province identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the state/province
    /// 
    public virtual async Task GetStateProvinceByIdAsync(int stateProvinceId)
    {
        return await _stateProvinceRepository.GetByIdAsync(stateProvinceId, cache => default);
    }

    /// 
    /// Gets a state/province by abbreviation
    /// 
    /// The state/province abbreviation
    /// Country identifier; pass null to load the state regardless of a country
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the state/province
    /// 
    public virtual async Task GetStateProvinceByAbbreviationAsync(string abbreviation, int? countryId = null)
    {
        if (string.IsNullOrEmpty(abbreviation))
            return null;

        var key = _staticCacheManager.PrepareKeyForDefaultCache(NopDirectoryDefaults.StateProvincesByAbbreviationCacheKey
            , abbreviation, countryId ?? 0);

        var query = _stateProvinceRepository.Table.Where(state => state.Abbreviation == abbreviation);

        //filter by country
        if (countryId.HasValue)
            query = query.Where(state => state.CountryId == countryId);

        return await _staticCacheManager.GetAsync(key, async () => await query.FirstOrDefaultAsync());
    }

    /// 
    /// Gets a state/province by address 
    /// 
    /// Address
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the country
    /// 
    public virtual async Task GetStateProvinceByAddressAsync(Address address)
    {
        return await GetStateProvinceByIdAsync(address?.StateProvinceId ?? 0);
    }

    /// 
    /// Gets a state/province collection by country identifier
    /// 
    /// Country identifier
    /// Language identifier. It's used to sort states by localized names (if specified); pass 0 to skip it
    /// A value indicating whether to show hidden records
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the states
    /// 
    public virtual async Task> GetStateProvincesByCountryIdAsync(int countryId, int languageId = 0, bool showHidden = false)
    {
        var key = _staticCacheManager.PrepareKeyForDefaultCache(NopDirectoryDefaults.StateProvincesByCountryCacheKey, countryId, languageId, showHidden);

        return await _staticCacheManager.GetAsync(key, async () =>
        {
            var query = from sp in _stateProvinceRepository.Table
                orderby sp.DisplayOrder, sp.Name
                where sp.CountryId == countryId &&
                      (showHidden || sp.Published)
                select sp;
            var stateProvinces = await query.ToListAsync();

            if (languageId > 0)
                //we should sort states by localized names when they have the same display order
                stateProvinces = await stateProvinces.ToAsyncEnumerable()
                    .OrderBy(c => c.DisplayOrder)
                    .ThenByAwait(async c => await _localizationService.GetLocalizedAsync(c, x => x.Name, languageId))
                    .ToListAsync();

            return stateProvinces;
        });
    }

    /// 
    /// Gets all states/provinces
    /// 
    /// A value indicating whether to show hidden records
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the states
    /// 
    public virtual async Task> GetStateProvincesAsync(bool showHidden = false)
    {
        var query = from sp in _stateProvinceRepository.Table
            orderby sp.CountryId, sp.DisplayOrder, sp.Name
            where showHidden || sp.Published
            select sp;


        var stateProvinces = await _staticCacheManager.GetAsync(_staticCacheManager.PrepareKeyForDefaultCache(NopDirectoryDefaults.StateProvincesAllCacheKey, showHidden), async () => await query.ToListAsync());

        return stateProvinces;
    }

    /// 
    /// Inserts a state/province
    /// 
    /// State/province
    /// A task that represents the asynchronous operation
    public virtual async Task InsertStateProvinceAsync(StateProvince stateProvince)
    {
        await _stateProvinceRepository.InsertAsync(stateProvince);
    }

    /// 
    /// Updates a state/province
    /// 
    /// State/province
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateStateProvinceAsync(StateProvince stateProvince)
    {
        await _stateProvinceRepository.UpdateAsync(stateProvince);
    }

    #endregion
}