Try your search with a different keyword or use * as a wildcard.
using Nop.Core;
using Nop.Services.Directory;
using Nop.Services.Localization;
using Nop.Web.Models.Directory;
namespace Nop.Web.Factories;
///
/// Represents the country model factory
///
public partial class CountryModelFactory : ICountryModelFactory
{
#region Fields
protected readonly ICountryService _countryService;
protected readonly ILocalizationService _localizationService;
protected readonly IStateProvinceService _stateProvinceService;
protected readonly IWorkContext _workContext;
#endregion
#region Ctor
public CountryModelFactory(ICountryService countryService,
ILocalizationService localizationService,
IStateProvinceService stateProvinceService,
IWorkContext workContext)
{
_countryService = countryService;
_localizationService = localizationService;
_stateProvinceService = stateProvinceService;
_workContext = workContext;
}
#endregion
#region Methods
///
/// Get states and provinces by country identifier
///
/// Country identifier
/// Whether to add "Select state" item to list of states
///
/// A task that represents the asynchronous operation
/// The task result contains the list of identifiers and names of states and provinces
///
public virtual async Task> GetStatesByCountryIdAsync(int countryId, bool addSelectStateItem)
{
var country = await _countryService.GetCountryByIdAsync(countryId);
var states =
(await _stateProvinceService.GetStateProvincesByCountryIdAsync(country?.Id ?? 0,
(await _workContext.GetWorkingLanguageAsync()).Id)).ToList();
var result = new List();
foreach (var state in states)
result.Add(new StateProvinceModel
{
id = state.Id,
name = await _localizationService.GetLocalizedAsync(state, x => x.Name)
});
if (country == null)
{
//country is not selected ("choose country" item)
if (addSelectStateItem)
result.Insert(0, new StateProvinceModel
{
id = 0,
name = await _localizationService.GetResourceAsync("Address.SelectState")
});
else
result.Insert(0, new StateProvinceModel
{
id = 0,
name = await _localizationService.GetResourceAsync("Address.Other")
});
}
else
{
//some country is selected
if (!result.Any())
//country does not have states
result.Insert(0, new StateProvinceModel
{
id = 0,
name = await _localizationService.GetResourceAsync("Address.Other")
});
else
{
//country has some states
if (addSelectStateItem)
result.Insert(0, new StateProvinceModel
{
id = 0,
name = await _localizationService.GetResourceAsync("Address.SelectState")
});
}
}
return result;
}
#endregion
}