Try your search with a different keyword or use * as a wildcard.
using Nop.Core.Domain.Directory;
using Nop.Services.Directory;
using Nop.Services.Localization;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Directory;
using Nop.Web.Framework.Factories;
using Nop.Web.Framework.Models.Extensions;
namespace Nop.Web.Areas.Admin.Factories;
///
/// Represents the country model factory implementation
///
public partial class CountryModelFactory : ICountryModelFactory
{
#region Fields
protected readonly ICountryService _countryService;
protected readonly ILocalizationService _localizationService;
protected readonly ILocalizedModelFactory _localizedModelFactory;
protected readonly IStoreMappingSupportedModelFactory _storeMappingSupportedModelFactory;
protected readonly IStateProvinceService _stateProvinceService;
#endregion
#region Ctor
public CountryModelFactory(ICountryService countryService,
ILocalizationService localizationService,
ILocalizedModelFactory localizedModelFactory,
IStoreMappingSupportedModelFactory storeMappingSupportedModelFactory,
IStateProvinceService stateProvinceService)
{
_countryService = countryService;
_localizationService = localizationService;
_localizedModelFactory = localizedModelFactory;
_storeMappingSupportedModelFactory = storeMappingSupportedModelFactory;
_stateProvinceService = stateProvinceService;
}
#endregion
#region Utilities
///
/// Prepare state and province search model
///
/// State and province search model
/// Country
/// State and province search model
protected virtual StateProvinceSearchModel PrepareStateProvinceSearchModel(StateProvinceSearchModel searchModel, Country country)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(country);
searchModel.CountryId = country.Id;
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
#endregion
#region Methods
///
/// Prepare country search model
///
/// Country search model
///
/// A task that represents the asynchronous operation
/// The task result contains the country search model
///
public virtual Task PrepareCountrySearchModelAsync(CountrySearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//prepare page parameters
searchModel.SetGridPageSize();
return Task.FromResult(searchModel);
}
///
/// Prepare paged country list model
///
/// Country search model
///
/// A task that represents the asynchronous operation
/// The task result contains the country list model
///
public virtual async Task PrepareCountryListModelAsync(CountrySearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//get countries
var countries = (await _countryService.GetAllCountriesAsync(showHidden: true)).ToPagedList(searchModel);
//prepare list model
var model = await new CountryListModel().PrepareToGridAsync(searchModel, countries, () =>
{
//fill in model values from the entity
return countries.SelectAwait(async country =>
{
var countryModel = country.ToModel();
countryModel.NumberOfStates = (await _stateProvinceService.GetStateProvincesByCountryIdAsync(country.Id))?.Count ?? 0;
return countryModel;
});
});
return model;
}
///
/// Prepare country model
///
/// Country model
/// Country
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the country model
///
public virtual async Task PrepareCountryModelAsync(CountryModel model, Country country, bool excludeProperties = false)
{
Func localizedModelConfiguration = null;
if (country != null)
{
//fill in model values from the entity
if (model == null)
{
model = country.ToModel();
model.NumberOfStates = (await _stateProvinceService.GetStateProvincesByCountryIdAsync(country.Id))?.Count ?? 0;
}
//prepare nested search model
PrepareStateProvinceSearchModel(model.StateProvinceSearchModel, country);
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(country, entity => entity.Name, languageId, false, false);
};
}
//set default values for the new model
if (country == null)
{
model.Published = true;
model.AllowsBilling = true;
model.AllowsShipping = true;
}
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
//prepare available stores
await _storeMappingSupportedModelFactory.PrepareModelStoresAsync(model, country, excludeProperties);
return model;
}
///
/// Prepare paged state and province list model
///
/// State and province search model
/// Country
///
/// A task that represents the asynchronous operation
/// The task result contains the state and province list model
///
public virtual async Task PrepareStateProvinceListModelAsync(StateProvinceSearchModel searchModel, Country country)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(country);
//get comments
var states = (await _stateProvinceService.GetStateProvincesByCountryIdAsync(country.Id, showHidden: true)).ToPagedList(searchModel);
//prepare list model
var model = new StateProvinceListModel().PrepareToGrid(searchModel, states, () =>
{
//fill in model values from the entity
return states.Select(state => state.ToModel());
});
return model;
}
///
/// Prepare state and province model
///
/// State and province model
/// Country
/// State or province
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the state and province model
///
public virtual async Task PrepareStateProvinceModelAsync(StateProvinceModel model,
Country country, StateProvince state, bool excludeProperties = false)
{
Func localizedModelConfiguration = null;
if (state != null)
{
//fill in model values from the entity
model ??= state.ToModel();
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(state, entity => entity.Name, languageId, false, false);
};
}
model.CountryId = country.Id;
//set default values for the new model
if (state == null)
model.Published = true;
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
return model;
}
#endregion
}