Webiant Logo Webiant Logo
  1. No results found.

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

TopicService.cs

using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain.Topics;
using Nop.Data;
using Nop.Services.Customers;
using Nop.Services.Security;
using Nop.Services.Stores;

namespace Nop.Services.Topics;

/// 
/// Topic service
/// 
public partial class TopicService : ITopicService
{
    #region Fields

    protected readonly IAclService _aclService;
    protected readonly ICustomerService _customerService;
    protected readonly IRepository _topicRepository;
    protected readonly IStaticCacheManager _staticCacheManager;
    protected readonly IStoreMappingService _storeMappingService;
    protected readonly IWorkContext _workContext;

    #endregion

    #region Ctor

    public TopicService(
        IAclService aclService,
        ICustomerService customerService,
        IRepository topicRepository,
        IStaticCacheManager staticCacheManager,
        IStoreMappingService storeMappingService,
        IWorkContext workContext)
    {
        _aclService = aclService;
        _customerService = customerService;
        _topicRepository = topicRepository;
        _staticCacheManager = staticCacheManager;
        _storeMappingService = storeMappingService;
        _workContext = workContext;
    }

    #endregion

    #region Methods

    /// 
    /// Deletes a topic
    /// 
    /// Topic
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteTopicAsync(Topic topic)
    {
        await _topicRepository.DeleteAsync(topic);
    }

    /// 
    /// Gets a topic
    /// 
    /// The topic identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the topic
    /// 
    public virtual async Task GetTopicByIdAsync(int topicId)
    {
        return await _topicRepository.GetByIdAsync(topicId, cache => default);
    }

    /// 
    /// Gets a topic
    /// 
    /// The topic system name
    /// Store identifier; pass 0 to ignore filtering by store and load the first one
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the topic
    /// 
    public virtual async Task GetTopicBySystemNameAsync(string systemName, int storeId = 0)
    {
        if (string.IsNullOrEmpty(systemName))
            return null;

        var customer = await _workContext.GetCurrentCustomerAsync();
        var customerRoleIds = await _customerService.GetCustomerRoleIdsAsync(customer);

        var cacheKey = _staticCacheManager.PrepareKeyForDefaultCache(NopTopicDefaults.TopicBySystemNameCacheKey, systemName, storeId, customerRoleIds);

        return await _staticCacheManager.GetAsync(cacheKey, async () =>
        {
            var query = _topicRepository.Table
                .Where(t => t.Published);

            //apply store mapping constraints
            query = await _storeMappingService.ApplyStoreMapping(query, storeId);

            //apply ACL constraints
            query = await _aclService.ApplyAcl(query, customerRoleIds);

            return query.Where(t => t.SystemName == systemName)
                .OrderBy(t => t.Id)
                .FirstOrDefault();
        });
    }

    /// 
    /// Gets all topics
    /// 
    /// Store identifier; pass 0 to load all records
    /// A value indicating whether to ignore ACL rules
    /// A value indicating whether to show hidden topics
    /// A value indicating whether to show only topics which include on the top menu
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the topics
    /// 
    public virtual async Task> GetAllTopicsAsync(int storeId,
        bool ignoreAcl = false, bool showHidden = false, bool onlyIncludedInTopMenu = false)
    {
        var customer = await _workContext.GetCurrentCustomerAsync();
        var customerRoleIds = await _customerService.GetCustomerRoleIdsAsync(customer);

        return await _topicRepository.GetAllAsync(async query =>
        {

            if (!showHidden || storeId > 0)
            {
                //apply store mapping constraints
                query = await _storeMappingService.ApplyStoreMapping(query, storeId);
            }

            if (!showHidden)
            {
                query = query.Where(t => t.Published);

                //apply ACL constraints
                if (!ignoreAcl)
                    query = await _aclService.ApplyAcl(query, customerRoleIds);
            }

            if (onlyIncludedInTopMenu)
                query = query.Where(t => t.IncludeInTopMenu);

            return query.OrderBy(t => t.DisplayOrder).ThenBy(t => t.SystemName);
        }, cache =>
        {
            return ignoreAcl
                ? cache.PrepareKeyForDefaultCache(NopTopicDefaults.TopicsAllCacheKey, storeId, showHidden, onlyIncludedInTopMenu)
                : cache.PrepareKeyForDefaultCache(NopTopicDefaults.TopicsAllWithACLCacheKey, storeId, showHidden, onlyIncludedInTopMenu, customerRoleIds);
        });
    }

    /// 
    /// Gets all topics
    /// 
    /// Store identifier; pass 0 to load all records
    /// Keywords to search into body or title
    /// A value indicating whether to ignore ACL rules
    /// A value indicating whether to show hidden topics
    /// A value indicating whether to show only topics which include on the top menu
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the topics
    /// 
    public virtual async Task> GetAllTopicsAsync(int storeId, string keywords,
        bool ignoreAcl = false, bool showHidden = false, bool onlyIncludedInTopMenu = false)
    {
        var topics = await GetAllTopicsAsync(storeId,
            ignoreAcl: ignoreAcl,
            showHidden: showHidden,
            onlyIncludedInTopMenu: onlyIncludedInTopMenu);

        if (!string.IsNullOrWhiteSpace(keywords))
        {
            return topics
                .Where(topic => (topic.Title?.Contains(keywords, StringComparison.InvariantCultureIgnoreCase) ?? false) ||
                                (topic.Body?.Contains(keywords, StringComparison.InvariantCultureIgnoreCase) ?? false))
                .ToList();
        }

        return topics;
    }

    /// 
    /// Inserts a topic
    /// 
    /// Topic
    /// A task that represents the asynchronous operation
    public virtual async Task InsertTopicAsync(Topic topic)
    {
        await _topicRepository.InsertAsync(topic);
    }

    /// 
    /// Updates the topic
    /// 
    /// Topic
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateTopicAsync(Topic topic)
    {
        await _topicRepository.UpdateAsync(topic);
    }

    #endregion
}