Webiant Logo Webiant Logo
  1. No results found.

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

StoreMappingSupportedModelFactory.cs

using Microsoft.AspNetCore.Mvc.Rendering;
using Nop.Core;
using Nop.Core.Domain.Stores;
using Nop.Services.Stores;
using Nop.Web.Framework.Models;

namespace Nop.Web.Framework.Factories;

/// 
/// Represents the base store mapping supported model factory implementation
/// 
public partial class StoreMappingSupportedModelFactory : IStoreMappingSupportedModelFactory
{
    #region Fields

    protected readonly IStoreMappingService _storeMappingService;
    protected readonly IStoreService _storeService;

    #endregion

    #region Ctor

    public StoreMappingSupportedModelFactory(IStoreMappingService storeMappingService,
        IStoreService storeService)
    {
        _storeMappingService = storeMappingService;
        _storeService = storeService;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare selected and all available stores for the passed model
    /// 
    /// Store mapping supported model type
    /// Model
    /// A task that represents the asynchronous operation
    public virtual async Task PrepareModelStoresAsync(TModel model) where TModel : IStoreMappingSupportedModel
    {
        ArgumentNullException.ThrowIfNull(model);

        //prepare available stores
        var availableStores = await _storeService.GetAllStoresAsync();
        model.AvailableStores = availableStores.Select(store => new SelectListItem
        {
            Text = store.Name,
            Value = store.Id.ToString(),
            Selected = model.SelectedStoreIds.Contains(store.Id)
        }).ToList();
    }

    /// 
    /// Prepare selected and all available stores for the passed model by store mappings
    /// 
    /// Store mapping supported model type
    /// Store mapping supported entity type
    /// Model
    /// Entity
    /// Whether to ignore existing store mappings
    /// A task that represents the asynchronous operation
    public virtual async Task PrepareModelStoresAsync(TModel model, TEntity entity, bool ignoreStoreMappings)
        where TModel : IStoreMappingSupportedModel where TEntity : BaseEntity, IStoreMappingSupported
    {
        ArgumentNullException.ThrowIfNull(model);

        //prepare stores with granted access
        if (!ignoreStoreMappings && entity != null)
            model.SelectedStoreIds = (await _storeMappingService.GetStoresIdsWithAccessAsync(entity)).ToList();

        await PrepareModelStoresAsync(model);
    }

    #endregion
}