From bbec7d4aa2d4fce1e2087c77f58a360565e0c763 Mon Sep 17 00:00:00 2001 From: Salih Date: Thu, 22 Jun 2023 13:56:42 +0300 Subject: [PATCH] Add paging to docs module search result --- .../Docs/Documents/IDocumentAppService.cs | 3 +- .../Volo/Docs/Documents/DocumentAppService.cs | 11 +-- .../Elastic/ElasticDocumentFullSearch.cs | 4 +- .../FullSearch/Elastic/EsDocumentResult.cs | 15 ++++ .../FullSearch/Elastic/IDocumentFullSearch.cs | 2 +- .../Docs/Documents/DocsDocumentController.cs | 3 +- .../Pages/Documents/Search.cshtml | 78 ++++++++++--------- .../Pages/Documents/Search.cshtml.cs | 26 ++++++- 8 files changed, 95 insertions(+), 47 deletions(-) create mode 100644 modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/EsDocumentResult.cs diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs index 653d823ff6..bd901194e6 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; namespace Volo.Docs.Documents @@ -16,7 +17,7 @@ namespace Volo.Docs.Documents Task GetResourceAsync(GetDocumentResourceInput input); - Task> SearchAsync(DocumentSearchInput input); + Task> SearchAsync(DocumentSearchInput input); Task FullSearchEnabledAsync(); diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs index 228648f2fb..8eefc42d6d 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Volo.Abp; +using Volo.Abp.Application.Dtos; using Volo.Abp.Caching; using Volo.Docs.Caching; using Volo.Docs.Documents.FullSearch.Elastic; @@ -176,16 +177,16 @@ namespace Volo.Docs.Documents ); } - public async Task> SearchAsync(DocumentSearchInput input) + public async Task> SearchAsync(DocumentSearchInput input) { var project = await _projectRepository.GetAsync(input.ProjectId); input.Version = GetProjectVersionPrefixIfExist(project) + input.Version; - var esDocs = - await _documentFullSearch.SearchAsync(input.Context, project.Id, input.LanguageCode, input.Version); + var esDocResult = + await _documentFullSearch.SearchAsync(input.Context, project.Id, input.LanguageCode, input.Version, input.SkipCount, input.MaxResultCount); - return esDocs.Select(esDoc => new DocumentSearchOutput //TODO: auto map + return new PagedResultDto(esDocResult.TotalCount,esDocResult.EsDocuments.Select(esDoc => new DocumentSearchOutput //TODO: auto map { Name = esDoc.Name, FileName = esDoc.FileName, @@ -194,7 +195,7 @@ namespace Volo.Docs.Documents Highlight = esDoc.Highlight }).Where(x => x.FileName != project.NavigationDocumentName && x.FileName != project.ParametersDocumentName) - .ToList(); + .ToList()); } public async Task FullSearchEnabledAsync() diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/ElasticDocumentFullSearch.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/ElasticDocumentFullSearch.cs index 6a6c8cc7e4..e5f8401b20 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/ElasticDocumentFullSearch.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/ElasticDocumentFullSearch.cs @@ -138,7 +138,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic .DeleteByQueryAsync(request, cancellationToken)); } - public virtual async Task> SearchAsync(string context, Guid projectId, string languageCode, + public virtual async Task SearchAsync(string context, Guid projectId, string languageCode, string version, int? skipCount = null, int? maxResultCount = null, CancellationToken cancellationToken = default) { @@ -212,7 +212,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic docs.Add(doc); } - return docs; + return new EsDocumentResult { EsDocuments = docs, TotalCount = response.Total }; } protected virtual void HandleError(IElasticsearchResponse response) diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/EsDocumentResult.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/EsDocumentResult.cs new file mode 100644 index 0000000000..269dc0e92d --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/EsDocumentResult.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace Volo.Docs.Documents.FullSearch.Elastic; + +public class EsDocumentResult +{ + public EsDocumentResult() + { + EsDocuments = new List(); + } + + public long TotalCount { get; set; } + + public List EsDocuments { get; set; } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/IDocumentFullSearch.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/IDocumentFullSearch.cs index 2c7cf1f3e6..77537b4e38 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/IDocumentFullSearch.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/FullSearch/Elastic/IDocumentFullSearch.cs @@ -19,7 +19,7 @@ namespace Volo.Docs.Documents.FullSearch.Elastic Task DeleteAllByProjectIdAsync(Guid projectId, CancellationToken cancellationToken = default); - Task> SearchAsync(string context, Guid projectId, string languageCode, + Task SearchAsync(string context, Guid projectId, string languageCode, string version, int? skipCount = null, int? maxResultCount = null, CancellationToken cancellationToken = default); diff --git a/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs b/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs index 9316f8eb97..2fd22c34a8 100644 --- a/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs +++ b/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Volo.Abp; +using Volo.Abp.Application.Dtos; using Volo.Abp.AspNetCore.Mvc; namespace Volo.Docs.Documents @@ -49,7 +50,7 @@ namespace Volo.Docs.Documents [HttpPost] [Route("search")] - public Task> SearchAsync(DocumentSearchInput input) + public Task> SearchAsync(DocumentSearchInput input) { return DocumentAppService.SearchAsync(input); } diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml index 82b1a80492..7056af34cb 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml @@ -41,45 +41,53 @@ -
-
-
@L["SearchResults"]
-
-
- @foreach (var docs in Model.SearchOutputs) - { -
-
-
- @functions - { - string RemoveFileExtensionFromPath(string path) + @if (!Model.KeyWord.IsNullOrWhiteSpace()) + { +
+
+
@L["SearchResults"]
+
+
+ @foreach (var docs in Model.SearchOutputs) + { +
+
+
+ @functions { - if (path == null) + string RemoveFileExtensionFromPath(string path) { - return null; + if (path == null) + { + return null; + } + + return path.EndsWith("." + @Model.Project.Format) + ? path.Left(path.Length - Model.Project.Format.Length - 1) + : path; } - - return path.EndsWith("." + @Model.Project.Format) - ? path.Left(path.Length - Model.Project.Format.Length - 1) - : path; } - } -
- - @RemoveFileExtensionFromPath(docs.Name) - -
-
- @foreach (var highlight in docs.Highlight) - { -

@Html.Raw(highlight)

- } -
-
+
+ + @RemoveFileExtensionFromPath(docs.Name) + +
+
+ @foreach (var highlight in docs.Highlight) + { +

@Html.Raw(highlight)

+ } +
+
+
-
- } + } + @if (Model.PagerModel != null) + { + + } +
-
+ } +
\ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml.cs index d1f7b50b70..64df5e8dc2 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Search.cshtml.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using System.Web; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Pagination; using Volo.Docs.Documents; using Volo.Docs.GitHub.Documents.Version; using Volo.Docs.HtmlConverting; @@ -24,6 +25,10 @@ namespace Volo.Docs.Pages.Documents [BindProperty(SupportsGet = true)] public string LanguageCode { get; set; } [BindProperty(SupportsGet = true)] public string KeyWord { get; set; } + + [BindProperty(SupportsGet = true)] public int CurrentPage { get; set; } = 1; + + public PagerModel PagerModel { get; set; } public ProjectDto Project { get; set; } @@ -49,6 +54,11 @@ namespace Volo.Docs.Pages.Documents return RedirectToPage("Index"); } + if (keyword.IsNullOrWhiteSpace()) + { + return Page(); + } + KeyWord = keyword; Project = await _projectAppService.GetAsync(ProjectName); @@ -72,13 +82,25 @@ namespace Volo.Docs.Pages.Documents } } - SearchOutputs = await _documentAppService.SearchAsync(new DocumentSearchInput + var pagedSearchOutputs = await _documentAppService.SearchAsync(new DocumentSearchInput { ProjectId = Project.Id, Context = KeyWord, LanguageCode = LanguageCode, - Version = Version + Version = Version, + MaxResultCount = 10, + SkipCount = (CurrentPage - 1) * 10 }); + + SearchOutputs = pagedSearchOutputs.Items.ToList(); + + PagerModel = new PagerModel(pagedSearchOutputs.TotalCount, 10, CurrentPage, 10, Url.Page("Search", new + { + ProjectName, + Version, + LanguageCode, + KeyWord + })); var highlightTag1 = Guid.NewGuid().ToString(); var highlightTag2 = Guid.NewGuid().ToString();