using Nop.Core; using Nop.Core.Caching; using Nop.Core.Domain.News; using Nop.Data; using Nop.Services.Stores; namespace Nop.Services.News; /// /// News service /// public partial class NewsService : INewsService { #region Fields protected readonly IRepository _newsCommentRepository; protected readonly IRepository _newsItemRepository; protected readonly IStaticCacheManager _staticCacheManager; protected readonly IStoreMappingService _storeMappingService; #endregion #region Ctor public NewsService( IRepository newsCommentRepository, IRepository newsItemRepository, IStaticCacheManager staticCacheManager, IStoreMappingService storeMappingService) { _newsCommentRepository = newsCommentRepository; _newsItemRepository = newsItemRepository; _staticCacheManager = staticCacheManager; _storeMappingService = storeMappingService; } #endregion #region Methods #region News /// /// Deletes a news /// /// News item /// A task that represents the asynchronous operation public virtual async Task DeleteNewsAsync(NewsItem newsItem) { await _newsItemRepository.DeleteAsync(newsItem); } /// /// Gets a news /// /// The news identifier /// /// A task that represents the asynchronous operation /// The task result contains the news /// public virtual async Task GetNewsByIdAsync(int newsId) { return await _newsItemRepository.GetByIdAsync(newsId, cache => default); } /// /// Gets all news /// /// Language identifier; 0 if you want to get all records /// Store identifier; 0 if you want to get all records /// Page index /// Page size /// A value indicating whether to show hidden records /// Filter by news item title /// /// A task that represents the asynchronous operation /// The task result contains the news items /// public virtual async Task> GetAllNewsAsync(int languageId = 0, int storeId = 0, int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false, string title = null) { var news = await _newsItemRepository.GetAllPagedAsync(async query => { if (languageId > 0) query = query.Where(n => languageId == n.LanguageId); if (!string.IsNullOrEmpty(title)) query = query.Where(n => n.Title.Contains(title)); if (!showHidden || storeId > 0) { //apply store mapping constraints query = await _storeMappingService.ApplyStoreMapping(query, storeId); } if (!showHidden) { var utcNow = DateTime.UtcNow; query = query.Where(n => n.Published); query = query.Where(n => !n.StartDateUtc.HasValue || n.StartDateUtc <= utcNow); query = query.Where(n => !n.EndDateUtc.HasValue || n.EndDateUtc >= utcNow); } return query.OrderByDescending(n => n.StartDateUtc ?? n.CreatedOnUtc); }, pageIndex, pageSize); return news; } /// /// Inserts a news item /// /// News item /// A task that represents the asynchronous operation public virtual async Task InsertNewsAsync(NewsItem news) { await _newsItemRepository.InsertAsync(news); } /// /// Updates the news item /// /// News item /// A task that represents the asynchronous operation public virtual async Task UpdateNewsAsync(NewsItem news) { await _newsItemRepository.UpdateAsync(news); } /// /// Get a value indicating whether a news item is available now (availability dates) /// /// News item /// Datetime to check; pass null to use current date /// Result public virtual bool IsNewsAvailable(NewsItem newsItem, DateTime? dateTime = null) { ArgumentNullException.ThrowIfNull(newsItem); if (newsItem.StartDateUtc.HasValue && newsItem.StartDateUtc.Value >= dateTime) return false; if (newsItem.EndDateUtc.HasValue && newsItem.EndDateUtc.Value <= dateTime) return false; return true; } #endregion #region News comments /// /// Gets all comments /// /// Customer identifier; 0 to load all records /// Store identifier; pass 0 to load all records /// News item ID; 0 or null to load all records /// A value indicating whether to content is approved; null to load all records /// Item creation from; null to load all records /// Item creation to; null to load all records /// Search comment text; null to load all records /// /// A task that represents the asynchronous operation /// The task result contains the comments /// public virtual async Task> GetAllCommentsAsync(int customerId = 0, int storeId = 0, int? newsItemId = null, bool? approved = null, DateTime? fromUtc = null, DateTime? toUtc = null, string commentText = null) { return await _newsCommentRepository.GetAllAsync(query => { if (approved.HasValue) query = query.Where(comment => comment.IsApproved == approved); if (newsItemId > 0) query = query.Where(comment => comment.NewsItemId == newsItemId); if (customerId > 0) query = query.Where(comment => comment.CustomerId == customerId); if (storeId > 0) query = query.Where(comment => comment.StoreId == storeId); if (fromUtc.HasValue) query = query.Where(comment => fromUtc.Value <= comment.CreatedOnUtc); if (toUtc.HasValue) query = query.Where(comment => toUtc.Value >= comment.CreatedOnUtc); if (!string.IsNullOrEmpty(commentText)) query = query.Where( c => c.CommentText.Contains(commentText) || c.CommentTitle.Contains(commentText)); query = query.OrderBy(nc => nc.CreatedOnUtc); return query; }); } /// /// Gets a news comment /// /// News comment identifier /// /// A task that represents the asynchronous operation /// The task result contains the news comment /// public virtual async Task GetNewsCommentByIdAsync(int newsCommentId) { return await _newsCommentRepository.GetByIdAsync(newsCommentId, cache => default, useShortTermCache: true); } /// /// Get news comments by identifiers /// /// News comment identifiers /// /// A task that represents the asynchronous operation /// The task result contains the news comments /// public virtual async Task> GetNewsCommentsByIdsAsync(int[] commentIds) { return await _newsCommentRepository.GetByIdsAsync(commentIds); } /// /// Get the count of news comments /// /// News item /// Store identifier; pass 0 to load all records /// A value indicating whether to count only approved or not approved comments; pass null to get number of all comments /// /// A task that represents the asynchronous operation /// The task result contains the number of news comments /// public virtual async Task GetNewsCommentsCountAsync(NewsItem newsItem, int storeId = 0, bool? isApproved = null) { var query = _newsCommentRepository.Table.Where(comment => comment.NewsItemId == newsItem.Id); if (storeId > 0) query = query.Where(comment => comment.StoreId == storeId); if (isApproved.HasValue) query = query.Where(comment => comment.IsApproved == isApproved.Value); var cacheKey = _staticCacheManager.PrepareKeyForDefaultCache(NopNewsDefaults.NewsCommentsNumberCacheKey, newsItem, storeId, isApproved); return await _staticCacheManager.GetAsync(cacheKey, async () => await query.CountAsync()); } /// /// Deletes a news comment /// /// News comment /// A task that represents the asynchronous operation public virtual async Task DeleteNewsCommentAsync(NewsComment newsComment) { await _newsCommentRepository.DeleteAsync(newsComment); } /// /// Deletes a news comments /// /// News comments /// A task that represents the asynchronous operation public virtual async Task DeleteNewsCommentsAsync(IList newsComments) { ArgumentNullException.ThrowIfNull(newsComments); foreach (var newsComment in newsComments) await DeleteNewsCommentAsync(newsComment); } /// /// Inserts a news comment /// /// News comment /// A task that represents the asynchronous operation public virtual async Task InsertNewsCommentAsync(NewsComment comment) { await _newsCommentRepository.InsertAsync(comment); } /// /// Update a news comment /// /// News comment /// A task that represents the asynchronous operation public virtual async Task UpdateNewsCommentAsync(NewsComment comment) { await _newsCommentRepository.UpdateAsync(comment); } #endregion #endregion }