Webiant Logo Webiant Logo
  1. No results found.

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

StoreModelFactory.cs

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

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the store model factory implementation
/// 
public partial class StoreModelFactory : IStoreModelFactory
{
    #region Fields

    protected readonly IBaseAdminModelFactory _baseAdminModelFactory;
    protected readonly ILocalizationService _localizationService;
    protected readonly ILocalizedModelFactory _localizedModelFactory;
    protected readonly IStoreService _storeService;

    #endregion

    #region Ctor

    public StoreModelFactory(IBaseAdminModelFactory baseAdminModelFactory,
        ILocalizationService localizationService,
        ILocalizedModelFactory localizedModelFactory,
        IStoreService storeService)
    {
        _baseAdminModelFactory = baseAdminModelFactory;
        _localizationService = localizationService;
        _localizedModelFactory = localizedModelFactory;
        _storeService = storeService;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare store search model
    /// 
    /// Store search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the store search model
    /// 
    public virtual Task PrepareStoreSearchModelAsync(StoreSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged store list model
    /// 
    /// Store search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the store list model
    /// 
    public virtual async Task PrepareStoreListModelAsync(StoreSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get stores
        var stores = (await _storeService.GetAllStoresAsync()).ToPagedList(searchModel);

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

        return model;
    }

    /// 
    /// Prepare store model
    /// 
    /// Store model
    /// Store
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the store model
    /// 
    public virtual async Task PrepareStoreModelAsync(StoreModel model, Store store, bool excludeProperties = false)
    {
        Func localizedModelConfiguration = null;

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

            //define localized model configuration action
            localizedModelConfiguration = async (locale, languageId) =>
            {
                locale.Name = await _localizationService.GetLocalizedAsync(store, entity => entity.Name, languageId, false, false);
                locale.DefaultTitle = await _localizationService.GetLocalizedAsync(store, entity => entity.DefaultTitle, languageId, false, false);
                locale.DefaultMetaDescription = await _localizationService.GetLocalizedAsync(store, entity => entity.DefaultMetaDescription, languageId, false, false);
                locale.DefaultMetaKeywords = await _localizationService.GetLocalizedAsync(store, entity => entity.DefaultMetaKeywords, languageId, false, false);
                locale.HomepageDescription = await _localizationService.GetLocalizedAsync(store, entity => entity.HomepageDescription, languageId, false, false);
                locale.HomepageTitle = await _localizationService.GetLocalizedAsync(store, entity => entity.HomepageTitle, languageId, false, false);
            };
        }

        //prepare available languages
        await _baseAdminModelFactory.PrepareLanguagesAsync(model.AvailableLanguages,
            defaultItemText: await _localizationService.GetResourceAsync("Admin.Common.EmptyItemText"));

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

        return model;
    }

    #endregion
}