Try your search with a different keyword or use * as a wildcard.
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Directory;
using Nop.Data;
using Nop.Services.Localization;
using Nop.Services.Stores;
namespace Nop.Services.Directory;
/// 
/// Country service
///  
public partial class CountryService : ICountryService
{
    #region Fields
    protected readonly IStaticCacheManager _staticCacheManager;
    protected readonly ILocalizationService _localizationService;
    protected readonly IRepository _countryRepository;
    protected readonly IStoreContext _storeContext;
    protected readonly IStoreMappingService _storeMappingService;
    #endregion
    #region Ctor
    public CountryService(
        IStaticCacheManager staticCacheManager,
        ILocalizationService localizationService,
        IRepository countryRepository,
        IStoreContext storeContext,
        IStoreMappingService storeMappingService)
    {
        _staticCacheManager = staticCacheManager;
        _localizationService = localizationService;
        _countryRepository = countryRepository;
        _storeContext = storeContext;
        _storeMappingService = storeMappingService;
    }
    #endregion
    #region Methods
    /// 
    /// Deletes a country
    ///  
    /// Country
    /// A task that represents the asynchronous operation 
    public virtual async Task DeleteCountryAsync(Country country)
    {
        await _countryRepository.DeleteAsync(country);
    }
    /// 
    /// Gets all countries
    ///  
    /// Language identifier. It's used to sort countries 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 countries
    ///  
    public virtual async Task> GetAllCountriesAsync(int languageId = 0, bool showHidden = false)
    {
        var store = await _storeContext.GetCurrentStoreAsync();
        var key = _staticCacheManager.PrepareKeyForDefaultCache(NopDirectoryDefaults.CountriesAllCacheKey, languageId,
            showHidden, store);
        return await _staticCacheManager.GetAsync(key, async () =>
        {
            var countries = await _countryRepository.GetAllAsync(async query =>
            {
                if (!showHidden)
                {
                    query = query.Where(c => c.Published);
                    //apply store mapping constraints
                    query = await _storeMappingService.ApplyStoreMapping(query, store.Id);
                }
                return query.OrderBy(c => c.DisplayOrder).ThenBy(c => c.Name);
            });
            if (languageId > 0)
            {
                //we should sort countries by localized names when they have the same display order
                countries = await countries
                    .ToAsyncEnumerable()
                    .OrderBy(c => c.DisplayOrder)
                    .ThenByAwait(async c => await _localizationService.GetLocalizedAsync(c, x => x.Name, languageId))
                    .ToListAsync();
            }
            return countries;
        });
    }
    /// 
    /// Gets all countries that allow billing
    ///  
    /// Language identifier. It's used to sort countries 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 countries
    ///  
    public virtual async Task> GetAllCountriesForBillingAsync(int languageId = 0, bool showHidden = false)
    {
        return (await GetAllCountriesAsync(languageId, showHidden)).Where(c => c.AllowsBilling).ToList();
    }
    /// 
    /// Gets all countries that allow shipping
    ///  
    /// Language identifier. It's used to sort countries 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 countries
    ///  
    public virtual async Task> GetAllCountriesForShippingAsync(int languageId = 0, bool showHidden = false)
    {
        return (await GetAllCountriesAsync(languageId, showHidden)).Where(c => c.AllowsShipping).ToList();
    }
    /// 
    /// Gets a country by address 
    ///  
    /// Address
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the country
    ///  
    public virtual async Task GetCountryByAddressAsync(Address address)
    {
        return await GetCountryByIdAsync(address?.CountryId ?? 0);
    }
    /// 
    /// Gets a country 
    ///  
    /// Country identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the country
    ///  
    public virtual async Task GetCountryByIdAsync(int countryId)
    {
        return await _countryRepository.GetByIdAsync(countryId, cache => default);
    }
    /// 
    /// Get countries by identifiers
    ///  
    /// Country identifiers
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the countries
    ///  
    public virtual async Task> GetCountriesByIdsAsync(int[] countryIds)
    {
        return await _countryRepository.GetByIdsAsync(countryIds);
    }
    /// 
    /// Gets a country by two letter ISO code
    ///  
    /// Country two letter ISO code
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the country
    ///  
    public virtual async Task GetCountryByTwoLetterIsoCodeAsync(string twoLetterIsoCode)
    {
        if (string.IsNullOrEmpty(twoLetterIsoCode))
            return null;
        var key = _staticCacheManager.PrepareKeyForDefaultCache(NopDirectoryDefaults.CountriesByTwoLetterCodeCacheKey, twoLetterIsoCode);
        var query = from c in _countryRepository.Table
            where c.TwoLetterIsoCode == twoLetterIsoCode
            select c;
        return await _staticCacheManager.GetAsync(key, async () => await query.FirstOrDefaultAsync());
    }
    /// 
    /// Gets a country by three letter ISO code
    ///  
    /// Country three letter ISO code
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the country
    ///  
    public virtual async Task GetCountryByThreeLetterIsoCodeAsync(string threeLetterIsoCode)
    {
        if (string.IsNullOrEmpty(threeLetterIsoCode))
            return null;
        var key = _staticCacheManager.PrepareKeyForDefaultCache(NopDirectoryDefaults.CountriesByThreeLetterCodeCacheKey, threeLetterIsoCode);
        var query = from c in _countryRepository.Table
            where c.ThreeLetterIsoCode == threeLetterIsoCode
            select c;
        return await _staticCacheManager.GetAsync(key, async () => await query.FirstOrDefaultAsync());
    }
    /// 
    /// Inserts a country
    ///  
    /// Country
    /// A task that represents the asynchronous operation 
    public virtual async Task InsertCountryAsync(Country country)
    {
        await _countryRepository.InsertAsync(country);
    }
    /// 
    /// Updates the country
    ///  
    /// Country
    /// A task that represents the asynchronous operation 
    public virtual async Task UpdateCountryAsync(Country country)
    {
        await _countryRepository.UpdateAsync(country);
    }
    #endregion
}