Webiant Logo Webiant Logo
  1. No results found.

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

VendorAttributeModelFactory.cs

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

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the vendor attribute model factory implementation
/// 
public partial class VendorAttributeModelFactory : IVendorAttributeModelFactory
{
    #region Fields

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

    #endregion

    #region Ctor

    public VendorAttributeModelFactory(IAttributeService vendorAttributeService,
        ILocalizationService localizationService,
        ILocalizedModelFactory localizedModelFactory)
    {
        _vendorAttributeService = vendorAttributeService;
        _localizationService = localizationService;
        _localizedModelFactory = localizedModelFactory;
    }

    #endregion

    #region Utilities

    /// 
    /// Prepare vendor attribute value search model
    /// 
    /// Vendor attribute value search model
    /// Vendor attribute
    /// Vendor attribute value search model
    protected virtual VendorAttributeValueSearchModel PrepareVendorAttributeValueSearchModel(VendorAttributeValueSearchModel searchModel,
        VendorAttribute vendorAttribute)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        ArgumentNullException.ThrowIfNull(vendorAttribute);

        searchModel.VendorAttributeId = vendorAttribute.Id;

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare vendor attribute search model
    /// 
    /// Vendor attribute search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the vendor attribute search model
    /// 
    public virtual Task PrepareVendorAttributeSearchModelAsync(VendorAttributeSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged vendor attribute list model
    /// 
    /// Vendor attribute search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the vendor attribute list model
    /// 
    public virtual async Task PrepareVendorAttributeListModelAsync(VendorAttributeSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get vendor attributes
        var vendorAttributes = (await _vendorAttributeService.GetAllAttributesAsync()).ToPagedList(searchModel);

        //prepare list model
        var model = await new VendorAttributeListModel().PrepareToGridAsync(searchModel, vendorAttributes, () =>
        {
            return vendorAttributes.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 vendor attribute model
    /// 
    /// Vendor attribute model
    /// Vendor attribute
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the vendor attribute model
    /// 
    public virtual async Task PrepareVendorAttributeModelAsync(VendorAttributeModel model,
        VendorAttribute vendorAttribute, bool excludeProperties = false)
    {
        Func localizedModelConfiguration = null;

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

            //prepare nested search model
            PrepareVendorAttributeValueSearchModel(model.VendorAttributeValueSearchModel, vendorAttribute);

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

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

        return model;
    }

    /// 
    /// Prepare paged vendor attribute value list model
    /// 
    /// Vendor attribute value search model
    /// Vendor attribute
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the vendor attribute value list model
    /// 
    public virtual async Task PrepareVendorAttributeValueListModelAsync(VendorAttributeValueSearchModel searchModel,
        VendorAttribute vendorAttribute)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        ArgumentNullException.ThrowIfNull(vendorAttribute);

        //get vendor attribute values
        var vendorAttributeValues = (await _vendorAttributeService.GetAttributeValuesAsync(vendorAttribute.Id)).ToPagedList(searchModel);

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

        return model;
    }

    /// 
    /// Prepare vendor attribute value model
    /// 
    /// Vendor attribute value model
    /// Vendor attribute
    /// Vendor attribute value
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the vendor attribute value model
    /// 
    public virtual async Task PrepareVendorAttributeValueModelAsync(VendorAttributeValueModel model,
        VendorAttribute vendorAttribute, VendorAttributeValue vendorAttributeValue, bool excludeProperties = false)
    {
        ArgumentNullException.ThrowIfNull(vendorAttribute);

        Func localizedModelConfiguration = null;

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

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

        model.AttributeId = vendorAttribute.Id;

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

        return model;
    }

    #endregion
}