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 specification attribute model factory implementation
///
public partial class SpecificationAttributeModelFactory : ISpecificationAttributeModelFactory
{
#region Fields
protected readonly IBaseAdminModelFactory _baseAdminModelFactory;
protected readonly ILocalizationService _localizationService;
protected readonly ILocalizedModelFactory _localizedModelFactory;
protected readonly ISpecificationAttributeService _specificationAttributeService;
#endregion
#region Ctor
public SpecificationAttributeModelFactory(IBaseAdminModelFactory baseAdminModelFactory,
ILocalizationService localizationService,
ILocalizedModelFactory localizedModelFactory,
ISpecificationAttributeService specificationAttributeService)
{
_baseAdminModelFactory = baseAdminModelFactory;
_localizationService = localizationService;
_localizedModelFactory = localizedModelFactory;
_specificationAttributeService = specificationAttributeService;
}
#endregion
#region Utilities
///
/// Prepare specification attribute option search model
///
/// Specification attribute option search model
/// Specification attribute
/// Specification attribute option search model
protected virtual SpecificationAttributeOptionSearchModel PrepareSpecificationAttributeOptionSearchModel(
SpecificationAttributeOptionSearchModel searchModel, SpecificationAttribute specificationAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(specificationAttribute);
searchModel.SpecificationAttributeId = specificationAttribute.Id;
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
///
/// Prepare search model of products that use the specification attribute
///
/// Search model of products that use the specification attribute
/// Specification attribute
/// Search model of products that use the specification attribute
protected virtual SpecificationAttributeProductSearchModel PrepareSpecificationAttributeProductSearchModel(
SpecificationAttributeProductSearchModel searchModel, SpecificationAttribute specificationAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(specificationAttribute);
searchModel.SpecificationAttributeId = specificationAttribute.Id;
//prepare page parameters
searchModel.SetGridPageSize();
return searchModel;
}
#endregion
#region Methods
///
/// Prepare specification attribute group search model
///
/// Specification attribute group search model
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute group search model
///
public virtual Task PrepareSpecificationAttributeGroupSearchModelAsync(SpecificationAttributeGroupSearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//prepare page parameters
searchModel.SetGridPageSize();
return Task.FromResult(searchModel);
}
///
/// Prepare paged specification attribute group list model
///
/// Specification attribute group search model
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute group list model
///
public virtual async Task PrepareSpecificationAttributeGroupListModelAsync(SpecificationAttributeGroupSearchModel searchModel)
{
ArgumentNullException.ThrowIfNull(searchModel);
//get specification attribute groups
var specificationAttributeGroups = await _specificationAttributeService
.GetSpecificationAttributeGroupsAsync(searchModel.Page - 1, searchModel.PageSize);
if (searchModel.Page == 1)
{
//dislpay default group with non-grouped specification attributes on first page
specificationAttributeGroups.Insert(0, new SpecificationAttributeGroup
{
Name = await _localizationService.GetResourceAsync("Admin.Catalog.Attributes.SpecificationAttributes.SpecificationAttributeGroup.DefaultGroupName")
});
}
//prepare list model
var model = new SpecificationAttributeGroupListModel().PrepareToGrid(searchModel, specificationAttributeGroups, () =>
{
//fill in model values from the entity
return specificationAttributeGroups.Select(attribute => attribute.ToModel());
});
return model;
}
///
/// Prepare specification attribute group model
///
/// Specification attribute group model
/// Specification attribute group
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute group model
///
public virtual async Task PrepareSpecificationAttributeGroupModelAsync(SpecificationAttributeGroupModel model,
SpecificationAttributeGroup specificationAttributeGroup, bool excludeProperties = false)
{
Func localizedModelConfiguration = null;
if (specificationAttributeGroup != null)
{
//fill in model values from the entity
model ??= specificationAttributeGroup.ToModel();
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(specificationAttributeGroup, entity => entity.Name, languageId, false, false);
};
}
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
return model;
}
///
/// Prepare paged specification attribute list model
///
/// Specification attribute search model
/// Specification attribute group
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute list model
///
public virtual async Task PrepareSpecificationAttributeListModelAsync(SpecificationAttributeSearchModel searchModel, SpecificationAttributeGroup group)
{
ArgumentNullException.ThrowIfNull(searchModel);
//get specification attributes
var specificationAttributes = (await _specificationAttributeService.GetSpecificationAttributesByGroupIdAsync(group?.Id)).ToPagedList(searchModel);
//prepare list model
var model = new SpecificationAttributeListModel().PrepareToGrid(searchModel, specificationAttributes, () =>
{
//fill in model values from the entity
return specificationAttributes.Select(attribute => attribute.ToModel());
});
return model;
}
///
/// Prepare specification attribute model
///
/// Specification attribute model
/// Specification attribute
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute model
///
public virtual async Task PrepareSpecificationAttributeModelAsync(SpecificationAttributeModel model,
SpecificationAttribute specificationAttribute, bool excludeProperties = false)
{
Func localizedModelConfiguration = null;
if (specificationAttribute != null)
{
//fill in model values from the entity
model ??= specificationAttribute.ToModel();
//prepare nested search models
PrepareSpecificationAttributeOptionSearchModel(model.SpecificationAttributeOptionSearchModel, specificationAttribute);
PrepareSpecificationAttributeProductSearchModel(model.SpecificationAttributeProductSearchModel, specificationAttribute);
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(specificationAttribute, entity => entity.Name, languageId, false, false);
};
}
//prepare localized models
if (!excludeProperties)
{
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
await _baseAdminModelFactory.PrepareSpecificationAttributeGroupsAsync(model.AvailableGroups,
defaultItemText: await _localizationService.GetResourceAsync("Admin.Catalog.Attributes.SpecificationAttributes.SpecificationAttribute.Fields.SpecificationAttributeGroup.None"));
}
return model;
}
///
/// Prepare paged specification attribute option list model
///
/// Specification attribute option search model
/// Specification attribute
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute option list model
///
public virtual async Task PrepareSpecificationAttributeOptionListModelAsync(
SpecificationAttributeOptionSearchModel searchModel, SpecificationAttribute specificationAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(specificationAttribute);
//get specification attribute options
var options = (await _specificationAttributeService
.GetSpecificationAttributeOptionsBySpecificationAttributeAsync(specificationAttribute.Id)).ToPagedList(searchModel);
//prepare list model
var model = await new SpecificationAttributeOptionListModel().PrepareToGridAsync(searchModel, options, () =>
{
return options.SelectAwait(async option =>
{
//fill in model values from the entity
var optionModel = option.ToModel();
//in order to save performance to do not check whether a product is deleted, etc
optionModel.NumberOfAssociatedProducts = await _specificationAttributeService
.GetProductSpecificationAttributeCountAsync(specificationAttributeOptionId: option.Id);
return optionModel;
});
});
return model;
}
///
/// Prepare specification attribute option model
///
/// Specification attribute option model
/// Specification attribute
/// Specification attribute option
/// Whether to exclude populating of some properties of model
///
/// A task that represents the asynchronous operation
/// The task result contains the specification attribute option model
///
public virtual async Task PrepareSpecificationAttributeOptionModelAsync(SpecificationAttributeOptionModel model,
SpecificationAttribute specificationAttribute, SpecificationAttributeOption specificationAttributeOption,
bool excludeProperties = false)
{
ArgumentNullException.ThrowIfNull(specificationAttribute);
Func localizedModelConfiguration = null;
if (specificationAttributeOption != null)
{
//fill in model values from the entity
model ??= specificationAttributeOption.ToModel();
model.EnableColorSquaresRgb = !string.IsNullOrEmpty(specificationAttributeOption.ColorSquaresRgb);
//define localized model configuration action
localizedModelConfiguration = async (locale, languageId) =>
{
locale.Name = await _localizationService.GetLocalizedAsync(specificationAttributeOption, entity => entity.Name, languageId, false, false);
};
}
model.SpecificationAttributeId = specificationAttribute.Id;
//prepare localized models
if (!excludeProperties)
model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);
return model;
}
///
/// Prepare paged list model of products that use the specification attribute
///
/// Search model of products that use the specification attribute
/// Specification attribute
///
/// A task that represents the asynchronous operation
/// The task result contains the list model of products that use the specification attribute
///
public virtual async Task PrepareSpecificationAttributeProductListModelAsync(
SpecificationAttributeProductSearchModel searchModel, SpecificationAttribute specificationAttribute)
{
ArgumentNullException.ThrowIfNull(searchModel);
ArgumentNullException.ThrowIfNull(specificationAttribute);
//get products
var products = await _specificationAttributeService.GetProductsBySpecificationAttributeIdAsync(
specificationAttributeId: specificationAttribute.Id,
pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize);
//prepare list model
var model = new SpecificationAttributeProductListModel().PrepareToGrid(searchModel, products, () =>
{
//fill in model values from the entity
return products.Select(product =>
{
var specificationAttributeProductModel = product.ToModel();
specificationAttributeProductModel.ProductId = product.Id;
specificationAttributeProductModel.ProductName = product.Name;
specificationAttributeProductModel.SpecificationAttributeId = specificationAttribute.Id;
return specificationAttributeProductModel;
});
});
return model;
}
#endregion
}