Webiant Logo Webiant Logo
  1. No results found.

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

MeasureModelFactory.cs

using Nop.Core.Domain.Directory;
using Nop.Services.Directory;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Directory;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the measures model factory implementation
/// 
public partial class MeasureModelFactory : IMeasureModelFactory
{
    #region Fields

    protected readonly IMeasureService _measureService;
    protected readonly MeasureSettings _measureSettings;

    #endregion

    #region Ctor

    public MeasureModelFactory(IMeasureService measureService,
        MeasureSettings measureSettings)
    {
        _measureService = measureService;
        _measureSettings = measureSettings;
    }

    #endregion

    #region Utilities

    /// 
    /// Prepare measure dimension search model
    /// 
    /// Measure dimension search model
    /// Measure dimension search model
    protected virtual MeasureDimensionSearchModel PrepareMeasureDimensionSearchModel(MeasureDimensionSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    /// 
    /// Prepare measure weight search model
    /// 
    /// Measure weight search model
    /// Measure weight search model
    protected virtual MeasureWeightSearchModel PrepareMeasureWeightSearchModel(MeasureWeightSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare measure search model
    /// 
    /// Measure search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the measure search model
    /// 
    public virtual Task PrepareMeasureSearchModelAsync(MeasureSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare nested search models
        PrepareMeasureDimensionSearchModel(searchModel.MeasureDimensionSearchModel);
        PrepareMeasureWeightSearchModel(searchModel.MeasureWeightSearchModel);

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged measure dimension list model
    /// 
    /// Measure dimension search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the measure dimension list model
    /// 
    public virtual async Task PrepareMeasureDimensionListModelAsync(MeasureDimensionSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get dimensions
        var dimensions = (await _measureService.GetAllMeasureDimensionsAsync()).ToPagedList(searchModel);

        //prepare list model
        var model = new MeasureDimensionListModel().PrepareToGrid(searchModel, dimensions, () =>
        {
            return dimensions.Select(dimension =>
            {
                //fill in model values from the entity
                var dimensionModel = dimension.ToModel();

                //fill in additional values (not existing in the entity)
                dimensionModel.IsPrimaryDimension = dimension.Id == _measureSettings.BaseDimensionId;

                return dimensionModel;
            });
        });

        return model;
    }

    /// 
    /// Prepare paged measure weight list model
    /// 
    /// Measure weight search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the measure weight list model
    /// 
    public virtual async Task PrepareMeasureWeightListModelAsync(MeasureWeightSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get weights
        var weights = (await _measureService.GetAllMeasureWeightsAsync()).ToPagedList(searchModel);

        //prepare list model
        var model = new MeasureWeightListModel().PrepareToGrid(searchModel, weights, () =>
        {
            return weights.Select(weight =>
            {
                //fill in model values from the entity
                var weightModel = weight.ToModel();

                //fill in additional values (not existing in the entity)
                weightModel.IsPrimaryWeight = weight.Id == _measureSettings.BaseWeightId;

                return weightModel;
            });
        });

        return model;
    }

    #endregion
}