Webiant Logo Webiant Logo
  1. No results found.

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

ForumModelFactory.cs

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

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the forum model factory implementation
/// 
public partial class ForumModelFactory : IForumModelFactory
{
    #region Fields

    protected readonly IDateTimeHelper _dateTimeHelper;
    protected readonly IForumService _forumService;

    #endregion

    #region Ctor

    public ForumModelFactory(IDateTimeHelper dateTimeHelper, IForumService forumService)
    {
        _dateTimeHelper = dateTimeHelper;
        _forumService = forumService;
    }

    #endregion

    #region Utilities

    /// 
    /// Prepare forum search model
    /// 
    /// Forum search model
    /// Forum search model
    protected virtual ForumSearchModel PrepareForumSearchModel(ForumSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return searchModel;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare forum group search model
    /// 
    /// Forum group search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the forum group search model
    /// 
    public virtual Task PrepareForumGroupSearchModelAsync(ForumGroupSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare nested search model
        PrepareForumSearchModel(searchModel.ForumSearch);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged forum group list model
    /// 
    /// Forum group search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the forum group list model
    /// 
    public virtual async Task PrepareForumGroupListModelAsync(ForumGroupSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get forum groups
        var forumGroups = (await _forumService.GetAllForumGroupsAsync()).ToPagedList(searchModel);

        //prepare list model
        var model = await new ForumGroupListModel().PrepareToGridAsync(searchModel, forumGroups, () =>
        {
            return forumGroups.SelectAwait(async forumGroup =>
            {
                //fill in model values from the entity
                var forumGroupModel = forumGroup.ToModel();

                //convert dates to the user time
                forumGroupModel.CreatedOn = await _dateTimeHelper.ConvertToUserTimeAsync(forumGroup.CreatedOnUtc, DateTimeKind.Utc);

                return forumGroupModel;
            });
        });

        return model;
    }

    /// 
    /// Prepare forum group model
    /// 
    /// Forum group model
    /// Forum group
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the forum group model
    /// 
    public virtual Task PrepareForumGroupModelAsync(ForumGroupModel model, ForumGroup forumGroup, bool excludeProperties = false)
    {
        //fill in model values from the entity
        if (forumGroup != null)
            model ??= forumGroup.ToModel();

        //set default values for the new model
        if (forumGroup == null)
            model.DisplayOrder = 1;

        return Task.FromResult(model);
    }

    /// 
    /// Prepare paged forum list model
    /// 
    /// Forum search model
    /// Forum group
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the forum list model
    /// 
    public virtual async Task PrepareForumListModelAsync(ForumSearchModel searchModel, ForumGroup forumGroup)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        ArgumentNullException.ThrowIfNull(forumGroup);

        //get forums
        var forums = (await _forumService.GetAllForumsByGroupIdAsync(forumGroup.Id)).ToPagedList(searchModel);

        //prepare list model
        var model = await new ForumListModel().PrepareToGridAsync(searchModel, forums, () =>
        {
            return forums.SelectAwait(async forum =>
            {
                //fill in model values from the entity
                var forumModel = forum.ToModel();

                //convert dates to the user time
                forumModel.CreatedOn = await _dateTimeHelper.ConvertToUserTimeAsync(forum.CreatedOnUtc, DateTimeKind.Utc);

                return forumModel;
            });
        });

        return model;
    }

    /// 
    /// Prepare forum model
    /// 
    /// Forum model
    /// Forum
    /// Whether to exclude populating of some properties of model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the forum model
    /// 
    public virtual async Task PrepareForumModelAsync(ForumModel model, Forum forum, bool excludeProperties = false)
    {
        //fill in model values from the entity
        if (forum != null)
            model ??= forum.ToModel();

        //set default values for the new model
        if (forum == null)
            model.DisplayOrder = 1;

        //prepare available forum groups
        foreach (var forumGroup in await _forumService.GetAllForumGroupsAsync())
        {
            model.ForumGroups.Add(forumGroup.ToModel());
        }

        return model;
    }

    #endregion
}