Webiant Logo Webiant Logo
  1. No results found.

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

EmailAccountModelFactory.cs

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

namespace Nop.Web.Areas.Admin.Factories;

/// 
/// Represents the email account model factory implementation
/// 
public partial class EmailAccountModelFactory : IEmailAccountModelFactory
{
    #region Fields

    protected readonly EmailAccountSettings _emailAccountSettings;
    protected readonly IEmailAccountService _emailAccountService;

    #endregion

    #region Ctor

    public EmailAccountModelFactory(EmailAccountSettings emailAccountSettings,
        IEmailAccountService emailAccountService)
    {
        _emailAccountSettings = emailAccountSettings;
        _emailAccountService = emailAccountService;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare email account search model
    /// 
    /// Email account search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the email account search model
    /// 
    public virtual Task PrepareEmailAccountSearchModelAsync(EmailAccountSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //prepare page parameters
        searchModel.SetGridPageSize();

        return Task.FromResult(searchModel);
    }

    /// 
    /// Prepare paged email account list model
    /// 
    /// Email account search model
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the email account list model
    /// 
    public virtual async Task PrepareEmailAccountListModelAsync(EmailAccountSearchModel searchModel)
    {
        ArgumentNullException.ThrowIfNull(searchModel);

        //get email accounts
        var emailAccounts = (await _emailAccountService.GetAllEmailAccountsAsync()).ToPagedList(searchModel);

        //prepare grid model
        var model = new EmailAccountListModel().PrepareToGrid(searchModel, emailAccounts, () =>
        {
            return emailAccounts.Select(emailAccount =>
            {
                //fill in model values from the entity
                var emailAccountModel = emailAccount.ToModel();

                //fill in additional values (not existing in the entity)
                emailAccountModel.IsDefaultEmailAccount = emailAccount.Id == _emailAccountSettings.DefaultEmailAccountId;

                return emailAccountModel;
            });
        });

        return model;
    }

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

        //set default values for the new model
        if (emailAccount == null)
        {
            model.Port = 25;
            model.MaxNumberOfEmails = 50;
        }

        model.AvailableEmailAuthenticationMethods.AddRange(await EmailAuthenticationMethod.None.ToSelectListAsync(false));

        return model;
    }

    #endregion
}