Try your search with a different keyword or use * as a wildcard.
using Nop.Core.Domain.Catalog;
using Nop.Services.Catalog;
using Nop.Services.Localization;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Catalog;
using Nop.Web.Framework.Factories;
using Nop.Web.Framework.Models.Extensions;
namespace Nop.Web.Areas.Admin.Factories;
///
/// Represents the product attribute model factory implementation
///
public partial class ProductAttributeModelFactory : IProductAttributeModelFactory
{
#region Fields
protected readonly ILocalizationService _localizationService;
protected readonly ILocalizedModelFactory _localizedModelFactory;
protected readonly IProductAttributeService _productAttributeService;
protected readonly IProductService _productService;
#endregion
#region Ctor
public ProductAttributeModelFactory(ILocalizationService localizationService,
ILocalizedModelFactory localizedModelFactory,
IProductAttributeService productAttributeService,
IProductService productService)
{
_localizationService = localizationService;
_localizedModelFactory = localizedModelFactory;
_productAttributeService = productAttributeService;
_productService = productService;
}
#endregion
#region Utilities
///
/// Prepare predefined product attribute value search model
///
/// Predefined product attribute value search model
/// Product attribute
/// Predefined product attribute value search model
protected virtual PredefinedProductAttributeValueSearchModel PreparePredefinedProductAttributeValueSearchModel(
PredefinedProductAttributeValueSearchModel searchModel, ProductAttribute productAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(productAttribute);
searchModel.ProductAttributeId = productAttribute.Id;
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
///
/// Prepare search model of products that use the product attribute
///
/// Search model of products that use the product attribute
/// Product attribute
/// Search model of products that use the product attribute
protected virtual ProductAttributeProductSearchModel PrepareProductAttributeProductSearchModel(ProductAttributeProductSearchModel searchModel,
ProductAttribute productAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(productAttribute);
searchModel.ProductAttributeId = productAttribute.Id;
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
#endregion
#region Methods
///
/// Prepare product attribute search model
///
/// Product attribute search model
///
/// A task that represents the asynchronous operation
/// The task result contains the product attribute search model
///
public virtual Task PrepareProductAttributeSearchModelAsync(ProductAttributeSearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//prepare page parameters
searchModel.SetGridPageSize();
return Task.FromResult(searchModel);
}
///
/// Prepare paged product attribute list model
///
/// Product attribute search model
///
/// A task that represents the asynchronous operation
/// The task result contains the product attribute list model
///
public virtual async Task PrepareProductAttributeListModelAsync(ProductAttributeSearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//get product attributes
var productAttributes = await _productAttributeService
.GetAllProductAttributesAsync(pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);
//prepare list model
var model = new ProductAttributeListModel().PrepareToGrid(searchModel, productAttributes, () =>
{
//fill in model values from the entity
return productAttributes.Select(attribute => attribute.ToModel());
});
return model;
}
///
/// Prepare product attribute model
///
/// Product attribute model
/// Product attribute
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the product attribute model
///
public virtual async Task PrepareProductAttributeModelAsync(ProductAttributeModel model,
ProductAttribute productAttribute, bool excludeProperties = false)
{
Func localizedModelConfiguration = null;
if (productAttribute != null)
{
//fill in model values from the entity
model ??= productAttribute.ToModel();
//prepare nested search models
PreparePredefinedProductAttributeValueSearchModel(model.PredefinedProductAttributeValueSearchModel, productAttribute);
PrepareProductAttributeProductSearchModel(model.ProductAttributeProductSearchModel, productAttribute);
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(productAttribute, entity => entity.Name, languageId, false, false);
locale.Description = await _localizationService.GetLocalizedAsync(productAttribute, entity => entity.Description, languageId, false, false);
};
}
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
return model;
}
///
/// Prepare paged predefined product attribute value list model
///
/// Predefined product attribute value search model
/// Product attribute
///
/// A task that represents the asynchronous operation
/// The task result contains the predefined product attribute value list model
///
public virtual async Task PreparePredefinedProductAttributeValueListModelAsync(
PredefinedProductAttributeValueSearchModel searchModel, ProductAttribute productAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(productAttribute);
//get predefined product attribute values
var values = (await _productAttributeService.GetPredefinedProductAttributeValuesAsync(productAttribute.Id)).ToPagedList(searchModel);
//prepare list model
var model = new PredefinedProductAttributeValueListModel().PrepareToGrid(searchModel, values, () =>
{
return values.Select(value =>
{
//fill in model values from the entity
var predefinedProductAttributeValueModel = value.ToModel();
//fill in additional values (not existing in the entity)
predefinedProductAttributeValueModel.WeightAdjustmentStr = value.WeightAdjustment.ToString("G29");
predefinedProductAttributeValueModel.PriceAdjustmentStr = value.PriceAdjustment
.ToString("G29") + (value.PriceAdjustmentUsePercentage ? " %" : string.Empty);
return predefinedProductAttributeValueModel;
});
});
return model;
}
///
/// Prepare predefined product attribute value model
///
/// Predefined product attribute value model
/// Product attribute
/// Predefined product attribute value
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the predefined product attribute value model
///
public virtual async Task PreparePredefinedProductAttributeValueModelAsync(PredefinedProductAttributeValueModel model,
ProductAttribute productAttribute, PredefinedProductAttributeValue productAttributeValue, bool excludeProperties = false)
{
ArgumentNullException.ThrowIfNull(productAttribute);
Func localizedModelConfiguration = null;
if (productAttributeValue != null)
{
//fill in model values from the entity
if (model == null)
{
model = productAttributeValue.ToModel();
}
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(productAttributeValue, entity => entity.Name, languageId, false, false);
};
}
model.ProductAttributeId = productAttribute.Id;
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
return model;
}
///
/// Prepare paged list model of products that use the product attribute
///
/// Search model of products that use the product attribute
/// Product attribute
///
/// A task that represents the asynchronous operation
/// The task result contains the list model of products that use the product attribute
///
public virtual async Task PrepareProductAttributeProductListModelAsync(ProductAttributeProductSearchModel searchModel,
ProductAttribute productAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(productAttribute);
//get products
var products = await _productService.GetProductsByProductAttributeIdAsync(productAttributeId: productAttribute.Id,
pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);
//prepare list model
var model = new ProductAttributeProductListModel().PrepareToGrid(searchModel, products, () =>
{
//fill in model values from the entity
return products.Select(product =>
{
var productAttributeProductModel = product.ToModel();
productAttributeProductModel.ProductName = product.Name;
return productAttributeProductModel;
});
});
return model;
}
#endregion
}