Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

CustomerAttributeModelFactory.cs

using Nop.Core.Domain.Customers;
using Nop.Services.Attributes;
using Nop.Services.Localization;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Customers;
using Nop.Web.Framework.Factories;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the customer attribute model factory implementation
/// 
public partial class CustomerAttributeModelFactory : ICustomerAttributeModelFactory
{
    #region Fields

    protected readonly IAttributeService _customerAttributeService;
    protected readonly ILocalizationService _localizationService;
    protected readonly ILocalizedModelFactory _localizedModelFactory;

    #endregion

    #region Ctor

    public CustomerAttributeModelFactory(IAttributeService customerAttributeService,
        ILocalizationService localizationService,
        ILocalizedModelFactory localizedModelFactory)
    {
        _customerAttributeService = customerAttributeService;
        _localizationService = localizationService;
        _localizedModelFactory = localizedModelFactory;
    }

    #endregion

    #region Utilities

    /// 
    /// Prepare customer attribute value search model
    /// 
    /// Customer attribute value search model
    /// Customer attribute
    /// Customer attribute value search model
    protected virtual CustomerAttributeValueSearchModel PrepareCustomerAttributeValueSearchModel(CustomerAttributeValueSearchModel searchModel,
        CustomerAttribute customerAttribute)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        ArgumentNullException.ThrowIfNull(customerAttribute);

        searchModel.CustomerAttributeId = customerAttribute.Id;

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare customer attribute search model
    /// 
    /// Customer attribute search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the customer attribute search model
    /// 
    public virtual Task PrepareCustomerAttributeSearchModelAsync(CustomerAttributeSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged customer attribute list model
    /// 
    /// Customer attribute search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the customer attribute list model
    /// 
    public virtual async Task PrepareCustomerAttributeListModelAsync(CustomerAttributeSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get customer attributes
        var customerAttributes = (await _customerAttributeService.GetAllAttributesAsync()).ToPagedList(searchModel);

        //prepare list model
        var model = await new CustomerAttributeListModel().PrepareToGridAsync(searchModel, customerAttributes, () =>
        {
            return customerAttributes.SelectAwait(async attribute =>
            {
                //fill in model values from the entity
                var attributeModel = attribute.ToModel();

                //fill in additional values (not existing in the entity)
                attributeModel.AttributeControlTypeName = await _localizationService.GetLocalizedEnumAsync(attribute.AttributeControlType);

                return attributeModel;
            });
        });

        return model;
    }

    /// 
    /// Prepare customer attribute model
    /// 
    /// Customer attribute model
    /// Customer attribute
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the customer attribute model
    /// 
    public virtual async Task PrepareCustomerAttributeModelAsync(CustomerAttributeModel model,
        CustomerAttribute customerAttribute, bool excludeProperties = false)
    {
        Func localizedModelConfiguration = null;

        if (customerAttribute != null)
        {
            //fill in model values from the entity
            model ??= customerAttribute.ToModel();

            //prepare nested search model
            PrepareCustomerAttributeValueSearchModel(model.CustomerAttributeValueSearchModel, customerAttribute);

            //define localized model configuration action
            localizedModelConfiguration = async (locale, languageId) =>
            {
                locale.Name = await _localizationService.GetLocalizedAsync(customerAttribute, entity => entity.Name, languageId, false, false);
            };
        }

        //prepare localized models
        if (!excludeProperties)
            model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);

        return model;
    }

    /// 
    /// Prepare paged customer attribute value list model
    /// 
    /// Customer attribute value search model
    /// Customer attribute
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the customer attribute value list model
    /// 
    public virtual async Task PrepareCustomerAttributeValueListModelAsync(CustomerAttributeValueSearchModel searchModel,
        CustomerAttribute customerAttribute)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        ArgumentNullException.ThrowIfNull(customerAttribute);

        //get customer attribute values
        var customerAttributeValues = (await _customerAttributeService
            .GetAttributeValuesAsync(customerAttribute.Id)).ToPagedList(searchModel);

        //prepare list model
        var model = new CustomerAttributeValueListModel().PrepareToGrid(searchModel, customerAttributeValues, () =>
        {
            //fill in model values from the entity
            return customerAttributeValues.Select(value => value.ToModel());
        });

        return model;
    }

    /// 
    /// Prepare customer attribute value model
    /// 
    /// Customer attribute value model
    /// Customer attribute
    /// Customer attribute value
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the customer attribute value model
    /// 
    public virtual async Task PrepareCustomerAttributeValueModelAsync(CustomerAttributeValueModel model,
        CustomerAttribute customerAttribute, CustomerAttributeValue customerAttributeValue, bool excludeProperties = false)
    {
        ArgumentNullException.ThrowIfNull(customerAttribute);

        Func localizedModelConfiguration = null;

        if (customerAttributeValue != null)
        {
            //fill in model values from the entity
            model ??= customerAttributeValue.ToModel();

            //define localized model configuration action
            localizedModelConfiguration = async (locale, languageId) =>
            {
                locale.Name = await _localizationService.GetLocalizedAsync(customerAttributeValue, entity => entity.Name, languageId, false, false);
            };
        }

        model.AttributeId = customerAttribute.Id;

        //prepare localized models
        if (!excludeProperties)
            model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration);

        return model;
    }

    #endregion
}