Webiant Logo Webiant Logo
  1. No results found.

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

TemplateModelFactory.cs

using Nop.Services.Catalog;
using Nop.Services.Topics;
using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions;
using Nop.Web.Areas.Admin.Models.Templates;
using Nop.Web.Framework.Models.Extensions;

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the template model factory implementation
/// 
public partial class TemplateModelFactory : ITemplateModelFactory
{
    #region Fields

    protected readonly ICategoryTemplateService _categoryTemplateService;
    protected readonly IManufacturerTemplateService _manufacturerTemplateService;
    protected readonly IProductTemplateService _productTemplateService;
    protected readonly ITopicTemplateService _topicTemplateService;

    #endregion

    #region Ctor

    public TemplateModelFactory(ICategoryTemplateService categoryTemplateService,
        IManufacturerTemplateService manufacturerTemplateService,
        IProductTemplateService productTemplateService,
        ITopicTemplateService topicTemplateService)
    {
        _categoryTemplateService = categoryTemplateService;
        _manufacturerTemplateService = manufacturerTemplateService;
        _productTemplateService = productTemplateService;
        _topicTemplateService = topicTemplateService;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare templates model
    /// 
    /// Templates model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the mplates model
    /// 
    public virtual async Task PrepareTemplatesModelAsync(TemplatesModel model)
    {
        ArgumentNullException.ThrowIfNull(model);

        //prepare nested search models
        await PrepareCategoryTemplateSearchModelAsync(model.TemplatesCategory);
        await PrepareManufacturerTemplateSearchModelAsync(model.TemplatesManufacturer);
        await PrepareProductTemplateSearchModelAsync(model.TemplatesProduct);
        await PrepareTopicTemplateSearchModelAsync(model.TemplatesTopic);

        return model;
    }

    /// 
    /// Prepare paged category template list model
    /// 
    /// Category template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the category template list model
    /// 
    public virtual async Task PrepareCategoryTemplateListModelAsync(CategoryTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get category templates
        var categoryTemplates = (await _categoryTemplateService.GetAllCategoryTemplatesAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new CategoryTemplateListModel().PrepareToGrid(searchModel, categoryTemplates,
            () => categoryTemplates.Select(template => template.ToModel()));

        return model;
    }

    /// 
    /// Prepare paged manufacturer template list model
    /// 
    /// Manufacturer template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the manufacturer template list model
    /// 
    public virtual async Task PrepareManufacturerTemplateListModelAsync(ManufacturerTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get manufacturer templates
        var manufacturerTemplates = (await _manufacturerTemplateService.GetAllManufacturerTemplatesAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new ManufacturerTemplateListModel().PrepareToGrid(searchModel, manufacturerTemplates,
            () => manufacturerTemplates.Select(template => template.ToModel()));

        return model;
    }

    /// 
    /// Prepare paged product template list model
    /// 
    /// Product template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the product template list model
    /// 
    public virtual async Task PrepareProductTemplateListModelAsync(ProductTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get product templates
        var productTemplates = (await _productTemplateService.GetAllProductTemplatesAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new ProductTemplateListModel().PrepareToGrid(searchModel, productTemplates,
            () => productTemplates.Select(template => template.ToModel()));

        return model;
    }

    /// 
    /// Prepare paged topic template list model
    /// 
    /// Topic template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the topic template list model
    /// 
    public virtual async Task PrepareTopicTemplateListModelAsync(TopicTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get topic templates
        var topicTemplates = (await _topicTemplateService.GetAllTopicTemplatesAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new TopicTemplateListModel().PrepareToGrid(searchModel, topicTemplates,
            () => topicTemplates.Select(template => template.ToModel()));

        return model;
    }

    /// 
    /// Prepare category template search model
    /// 
    /// Category template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the category template search model
    /// 
    public virtual Task PrepareCategoryTemplateSearchModelAsync(CategoryTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare manufacturer template search model
    /// 
    /// Manufacturer template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the manufacturer template search model
    /// 
    public virtual Task PrepareManufacturerTemplateSearchModelAsync(ManufacturerTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare product template search model
    /// 
    /// Product template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the product template search model
    /// 
    public virtual Task PrepareProductTemplateSearchModelAsync(ProductTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare topic template search model
    /// 
    /// Topic template search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the topic template search model
    /// 
    public virtual Task PrepareTopicTemplateSearchModelAsync(TopicTemplateSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    #endregion
}