#4303: add search function.
This commit is contained in:
		| @ -8,20 +8,29 @@ using Oqtane.Shared; | ||||
| using Oqtane.Migrations.Framework; | ||||
| using Oqtane.Documentation; | ||||
| using System.Linq; | ||||
| using Oqtane.Interfaces; | ||||
| using System.Collections.Generic; | ||||
| using System; | ||||
|  | ||||
| // ReSharper disable ConvertToUsingDeclaration | ||||
|  | ||||
| namespace Oqtane.Modules.HtmlText.Manager | ||||
| { | ||||
|     [PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")] | ||||
|     public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable | ||||
|     public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, IModuleSearch | ||||
|     { | ||||
|         private readonly IServiceProvider _serviceProvider; | ||||
|         private readonly IHtmlTextRepository _htmlText; | ||||
|         private readonly IDBContextDependencies _DBContextDependencies; | ||||
|         private readonly ISqlRepository _sqlRepository; | ||||
|  | ||||
|         public HtmlTextManager(IHtmlTextRepository htmlText, IDBContextDependencies DBContextDependencies, ISqlRepository sqlRepository) | ||||
|         public HtmlTextManager( | ||||
|             IServiceProvider serviceProvider, | ||||
|             IHtmlTextRepository htmlText, | ||||
|             IDBContextDependencies DBContextDependencies, | ||||
|             ISqlRepository sqlRepository) | ||||
|         { | ||||
|             _serviceProvider = serviceProvider; | ||||
|             _htmlText = htmlText; | ||||
|             _DBContextDependencies = DBContextDependencies; | ||||
|             _sqlRepository = sqlRepository; | ||||
| @ -39,6 +48,27 @@ namespace Oqtane.Modules.HtmlText.Manager | ||||
|             return content; | ||||
|         } | ||||
|  | ||||
|         public IList<SearchDocument> GetSearchDocuments(Module module, DateTime startDate) | ||||
|         { | ||||
|             var searchDocuments = new List<SearchDocument>(); | ||||
|  | ||||
|             var htmltexts = _htmlText.GetHtmlTexts(module.ModuleId); | ||||
|             if (htmltexts != null && htmltexts.Any(i => i.CreatedOn >= startDate)) | ||||
|             { | ||||
|                 var htmltext = htmltexts.OrderByDescending(item => item.CreatedOn).First(); | ||||
|  | ||||
|                 searchDocuments.Add(new SearchDocument | ||||
|                 { | ||||
|                     Title = module.Title, | ||||
|                     Description = string.Empty, | ||||
|                     Body = SearchUtils.Clean(htmltext.Content, true), | ||||
|                     ModifiedTime = htmltext.ModifiedOn | ||||
|                 }); | ||||
|             } | ||||
|  | ||||
|             return searchDocuments; | ||||
|         } | ||||
|  | ||||
|         public void ImportModule(Module module, string content, string version) | ||||
|         { | ||||
|             content = WebUtility.HtmlDecode(content); | ||||
|  | ||||
| @ -0,0 +1,43 @@ | ||||
| using System; | ||||
| using System.Net; | ||||
| using System.Threading.Tasks; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Oqtane.Controllers; | ||||
| using Oqtane.Documentation; | ||||
| using Oqtane.Enums; | ||||
| using Oqtane.Infrastructure; | ||||
| using Oqtane.Modules.SearchResults.Services; | ||||
| using Oqtane.Shared; | ||||
|  | ||||
| namespace Oqtane.Modules.SearchResults.Controllers | ||||
| { | ||||
|     [Route(ControllerRoutes.ApiRoute)] | ||||
|     [PrivateApi("Mark SearchResults classes as private, since it's not very useful in the public docs")] | ||||
|     public class SearchResultsController : ModuleControllerBase | ||||
|     { | ||||
|         private readonly ISearchResultsService _searchResultsService; | ||||
|  | ||||
|         public SearchResultsController(ISearchResultsService searchResultsService, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor) | ||||
|         { | ||||
|             _searchResultsService = searchResultsService; | ||||
|         } | ||||
|  | ||||
|         [HttpPost] | ||||
|         [Authorize(Policy = PolicyNames.ViewModule)] | ||||
|         public async Task<Models.SearchResults> Post([FromBody] Models.SearchQuery searchQuery) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 return await _searchResultsService.SearchAsync(AuthEntityId(EntityNames.Module), searchQuery); | ||||
|             } | ||||
|             catch(Exception ex) | ||||
|             { | ||||
|                 _logger.Log(LogLevel.Error, this, LogFunction.Other, ex, "Fetch search results failed.", searchQuery); | ||||
|                 HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,36 @@ | ||||
| using System.Threading.Tasks; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Oqtane.Documentation; | ||||
| using Oqtane.Infrastructure; | ||||
| using Oqtane.Models; | ||||
| using Oqtane.Services; | ||||
|  | ||||
| namespace Oqtane.Modules.SearchResults.Services | ||||
| { | ||||
|     [PrivateApi("Mark SearchResults classes as private, since it's not very useful in the public docs")] | ||||
|     public class ServerSearchResultsService : ISearchResultsService, ITransientService | ||||
|     { | ||||
|         private readonly ILogManager _logger; | ||||
|         private readonly IHttpContextAccessor _accessor; | ||||
|         private readonly Alias _alias; | ||||
|         private readonly ISearchService _searchService; | ||||
|  | ||||
|         public ServerSearchResultsService( | ||||
|             ITenantManager tenantManager, | ||||
|             ILogManager logger, | ||||
|             IHttpContextAccessor accessor, | ||||
|             ISearchService searchService) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             _accessor = accessor; | ||||
|             _alias = tenantManager.GetAlias(); | ||||
|             _searchService = searchService; | ||||
|         } | ||||
|  | ||||
|         public async Task<Models.SearchResults> SearchAsync(int moduleId, SearchQuery searchQuery) | ||||
|         { | ||||
|             var results = await _searchService.SearchAsync(searchQuery); | ||||
|             return results; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										24
									
								
								Oqtane.Server/Modules/SearchResults/Startup/ServerStartup.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Oqtane.Server/Modules/SearchResults/Startup/ServerStartup.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | ||||
| using Microsoft.AspNetCore.Builder; | ||||
| using Microsoft.AspNetCore.Hosting; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Oqtane.Infrastructure; | ||||
| using Oqtane.Modules.SearchResults.Services; | ||||
|  | ||||
| namespace Oqtane.Modules.SearchResults.Startup | ||||
| { | ||||
|     public class ServerStartup : IServerStartup | ||||
|     { | ||||
|         public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public void ConfigureMvc(IMvcBuilder mvcBuilder) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public void ConfigureServices(IServiceCollection services) | ||||
|         { | ||||
|             services.AddTransient<ISearchResultsService, ServerSearchResultsService>(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Ben
					Ben