using Nop.Core; using Nop.Core.Domain.Topics; using Nop.Services.Localization; using Nop.Services.Seo; using Nop.Services.Topics; using Nop.Web.Models.Topics; namespace Nop.Web.Factories; /// /// Represents the topic model factory /// public partial class TopicModelFactory : ITopicModelFactory { #region Fields protected readonly ILocalizationService _localizationService; protected readonly IStoreContext _storeContext; protected readonly ITopicService _topicService; protected readonly ITopicTemplateService _topicTemplateService; protected readonly IUrlRecordService _urlRecordService; #endregion #region Ctor public TopicModelFactory(ILocalizationService localizationService, IStoreContext storeContext, ITopicService topicService, ITopicTemplateService topicTemplateService, IUrlRecordService urlRecordService) { _localizationService = localizationService; _storeContext = storeContext; _topicService = topicService; _topicTemplateService = topicTemplateService; _urlRecordService = urlRecordService; } #endregion #region Methods /// /// Prepare the topic model /// /// Topic /// /// A task that represents the asynchronous operation /// The task result contains the topic model /// public virtual async Task PrepareTopicModelAsync(Topic topic) { ArgumentNullException.ThrowIfNull(topic); return new TopicModel { Id = topic.Id, SystemName = topic.SystemName, IncludeInSitemap = topic.IncludeInSitemap, IsPasswordProtected = topic.IsPasswordProtected, Title = topic.IsPasswordProtected ? string.Empty : await _localizationService.GetLocalizedAsync(topic, x => x.Title), Body = topic.IsPasswordProtected ? string.Empty : await _localizationService.GetLocalizedAsync(topic, x => x.Body), MetaKeywords = await _localizationService.GetLocalizedAsync(topic, x => x.MetaKeywords), MetaDescription = await _localizationService.GetLocalizedAsync(topic, x => x.MetaDescription), MetaTitle = await _localizationService.GetLocalizedAsync(topic, x => x.MetaTitle), SeName = await _urlRecordService.GetSeNameAsync(topic), TopicTemplateId = topic.TopicTemplateId }; } /// /// Get the topic model by topic system name /// /// Topic system name /// /// A task that represents the asynchronous operation /// The task result contains the topic model /// public virtual async Task PrepareTopicModelBySystemNameAsync(string systemName) { //load by store var store = await _storeContext.GetCurrentStoreAsync(); var topic = await _topicService.GetTopicBySystemNameAsync(systemName, store.Id); if (topic == null) return null; return await PrepareTopicModelAsync(topic); } /// /// Get topic template view path /// /// Topic template identifier /// /// A task that represents the asynchronous operation /// The task result contains the view path /// public virtual async Task PrepareTemplateViewPathAsync(int topicTemplateId) { var template = (await _topicTemplateService.GetTopicTemplateByIdAsync(topicTemplateId) ?? (await _topicTemplateService.GetAllTopicTemplatesAsync()).FirstOrDefault()) ?? throw new Exception("No default template could be loaded"); return template.ViewPath; } #endregion }