Webiant Logo Webiant Logo
  1. No results found.

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

SearchTermService.cs

using Nop.Core;
using Nop.Core.Domain.Common;
using Nop.Data;

namespace Nop.Services.Common;

/// 
/// Search term service
/// 
public partial class SearchTermService : ISearchTermService
{
    #region Fields

    protected readonly IRepository _searchTermRepository;

    #endregion

    #region Ctor

    public SearchTermService(IRepository searchTermRepository)
    {
        _searchTermRepository = searchTermRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Gets a search term record by keyword
    /// 
    /// Search term keyword
    /// Store identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the search term
    /// 
    public virtual async Task GetSearchTermByKeywordAsync(string keyword, int storeId)
    {
        if (string.IsNullOrEmpty(keyword))
            return null;

        var query = from st in _searchTermRepository.Table
            where st.Keyword == keyword && st.StoreId == storeId
            orderby st.Id
            select st;
        var searchTerm = await query.FirstOrDefaultAsync();

        return searchTerm;
    }

    /// 
    /// Gets a search term statistics
    /// 
    /// Page index
    /// Page size
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains a list search term report lines
    /// 
    public virtual async Task> GetStatsAsync(int pageIndex = 0, int pageSize = int.MaxValue)
    {
        var query = (from st in _searchTermRepository.Table
                group st by st.Keyword into groupedResult
                select new
                {
                    Keyword = groupedResult.Key,
                    Count = groupedResult.Sum(o => o.Count)
                })
            .OrderByDescending(m => m.Count)
            .Select(r => new SearchTermReportLine
            {
                Keyword = r.Keyword,
                Count = r.Count
            });

        var result = await query.ToPagedListAsync(pageIndex, pageSize);

        return result;
    }

    /// 
    /// Inserts a search term record
    /// 
    /// Search term
    /// A task that represents the asynchronous operation
    public virtual async Task InsertSearchTermAsync(SearchTerm searchTerm)
    {
        await _searchTermRepository.InsertAsync(searchTerm);
    }

    /// 
    /// Updates the search term record
    /// 
    /// Search term
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateSearchTermAsync(SearchTerm searchTerm)
    {
        await _searchTermRepository.UpdateAsync(searchTerm);
    }

    #endregion
}