From 04392b2b47e7c12d0b544785cbba479a0e4ccbc3 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Wed, 5 Dec 2018 19:04:38 +0300 Subject: [PATCH] Refactor document app service interface. --- .../Docs/Documents/GetDefaultDocumentInput.cs | 15 +++ .../Volo/Docs/Documents/GetDocumentInput.cs | 19 ++++ .../Documents/GetNavigationDocumentInput.cs | 15 +++ .../Docs/Documents/IDocumentAppService.cs | 23 +---- .../Volo/Docs/Projects/IProjectAppService.cs | 5 + .../Volo/Docs/Documents/DocumentAppService.cs | 93 +++---------------- .../Volo/Docs/Projects/ProjectAppService.cs | 64 ++++++++++++- .../Volo/Docs/Documents/DocumentConsts.cs | 7 ++ .../Volo/Docs/Projects/ProjectConsts.cs | 1 + .../Pages/Documents/Project/Index.cshtml.cs | 37 ++++++-- 10 files changed, 165 insertions(+), 114 deletions(-) create mode 100644 modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs create mode 100644 modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs create mode 100644 modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs create mode 100644 modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentConsts.cs diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs new file mode 100644 index 0000000000..ea52f1c7cc --- /dev/null +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs @@ -0,0 +1,15 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Volo.Docs.Projects; + +namespace Volo.Docs.Documents +{ + public class GetDefaultDocumentInput + { + public Guid ProjectId { get; set; } + + [Required] + [StringLength(ProjectConsts.MaxVersionNameLength)] + public string Version { get; set; } + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs new file mode 100644 index 0000000000..c189eab90d --- /dev/null +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Volo.Docs.Projects; + +namespace Volo.Docs.Documents +{ + public class GetDocumentInput + { + public Guid ProjectId { get; set; } + + [Required] + [StringLength(DocumentConsts.MaxNameLength)] + public string Name { get; set; } + + [Required] + [StringLength(ProjectConsts.MaxVersionNameLength)] + public string Version { get; set; } + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs new file mode 100644 index 0000000000..e049e83255 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs @@ -0,0 +1,15 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Volo.Docs.Projects; + +namespace Volo.Docs.Documents +{ + public class GetNavigationDocumentInput + { + public Guid ProjectId { get; set; } + + [Required] + [StringLength(ProjectConsts.MaxVersionNameLength)] + public string Version { get; set; } + } +} \ No newline at end of file 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 1c4f37ec00..c374f142cc 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,4 +1,3 @@ -using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Services; @@ -6,26 +5,10 @@ namespace Volo.Docs.Documents { public interface IDocumentAppService : IApplicationService { - //TODO: Create input DTOs for methods and add validation annotations. + Task GetAsync(GetDocumentInput input); - Task GetByNameAsync( - string projectShortName, - string documentName, - string version, - bool normalize); + Task GetDefaultAsync(GetDefaultDocumentInput input); - Task GetDefaultAsync( - string projectShortName, - string version, - bool normalize); - - Task GetNavigationDocumentAsync( - string projectShortName, - string version, - bool normalize); - - Task> GetVersions( - string projectShortName - ); + Task GetNavigationDocumentAsync(GetNavigationDocumentInput input); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs index 6884bdc90e..a052365d35 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs @@ -1,6 +1,9 @@ +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; +using Volo.Docs.Documents; namespace Volo.Docs.Projects { @@ -9,5 +12,7 @@ namespace Volo.Docs.Projects Task> GetListAsync(); Task GetByShortNameAsync(string shortName); + + Task> GetVersionsAsync(Guid projectId); } } \ No newline at end of file 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 61fed0ee2d..8174868051 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 @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; -using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Application.Services; -using Volo.Abp.Caching; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -12,59 +7,43 @@ namespace Volo.Docs.Documents public class DocumentAppService : ApplicationService, IDocumentAppService { private readonly IProjectRepository _projectRepository; - private readonly IDistributedCache> _distributedCache; private readonly IDocumentStoreFactory _documentStoreFactory; public DocumentAppService( IProjectRepository projectRepository, - IDistributedCache> distributedCache, IDocumentStoreFactory documentStoreFactory) { _projectRepository = projectRepository; - _distributedCache = distributedCache; _documentStoreFactory = documentStoreFactory; } - public async Task GetByNameAsync( - string projectShortName, - string documentName, - string version, - bool normalize) + public async Task GetAsync(GetDocumentInput input) { - var project = await _projectRepository.GetByShortNameAsync(projectShortName); + var project = await _projectRepository.GetAsync(input.ProjectId); return await GetDocumentWithDetailsDto( project, - documentName, - version, - normalize + input.Name, + input.Version ); } - public async Task GetDefaultAsync( - string projectShortName, - string version, - bool normalize) + public async Task GetDefaultAsync(GetDefaultDocumentInput input) { - var project = await _projectRepository.GetByShortNameAsync(projectShortName); + var project = await _projectRepository.GetAsync(input.ProjectId); return await GetDocumentWithDetailsDto( project, project.DefaultDocumentName, - version, - normalize + input.Version ); } - public virtual async Task GetNavigationDocumentAsync( - string projectShortName, - string version, - bool normalize) + public virtual async Task GetNavigationDocumentAsync(GetNavigationDocumentInput input) { - var project = await _projectRepository.GetByShortNameAsync(projectShortName); + var project = await _projectRepository.GetAsync(input.ProjectId); var documentDto = await GetDocumentWithDetailsDto( project, project.NavigationDocumentName, - version, - normalize + input.Version ); return ObjectMapper.Map(documentDto); @@ -73,8 +52,7 @@ namespace Volo.Docs.Documents protected virtual async Task GetDocumentWithDetailsDto( Project project, string documentName, - string version, - bool normalize) + string version) { var documentStore = _documentStoreFactory.Create(project.DocumentStoreType); var document = await documentStore.Find(project, documentName, version); @@ -84,54 +62,5 @@ namespace Volo.Docs.Documents return dto; } - - public async Task> GetVersions(string projectShortName) - { - //TODO: What if there is no version? - - var project = await _projectRepository.GetByShortNameAsync(projectShortName); - var documentStore = _documentStoreFactory.Create(project.DocumentStoreType); - - //TODO: Why not use GetOrAddAsync - var versions = await GetVersionsFromCache(projectShortName); - if (versions == null) - { - versions = await documentStore.GetVersions(project); - await SetVersionsToCache(projectShortName, versions); - } - - if (!project.MinimumVersion.IsNullOrEmpty()) - { - var minVersionIndex = versions.FindIndex(v => v.Name == project.MinimumVersion); - if (minVersionIndex > -1) - { - versions = versions.GetRange(0, minVersionIndex + 1); - } - } - - if (!string.IsNullOrEmpty(project.LatestVersionBranchName)) - { - versions.First().Name = project.LatestVersionBranchName; - } - - return versions; - } - - private async Task> GetVersionsFromCache(string projectShortName) - { - return await _distributedCache.GetAsync(projectShortName); - } - - private async Task SetVersionsToCache(string projectShortName, List versions) - { - await _distributedCache.SetAsync( - projectShortName, - versions, - new DistributedCacheEntryOptions - { - SlidingExpiration = TimeSpan.FromDays(1) - } - ); - } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs index 494064d925..ddd2325d14 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs @@ -1,18 +1,29 @@ +using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; -using Volo.Abp.Domain.Entities; +using Volo.Abp.Caching; +using Volo.Docs.Documents; namespace Volo.Docs.Projects { public class ProjectAppService : ApplicationService, IProjectAppService { private readonly IProjectRepository _projectRepository; + private readonly IDistributedCache> _distributedCache; + private readonly IDocumentStoreFactory _documentStoreFactory; - public ProjectAppService(IProjectRepository projectRepository) + public ProjectAppService( + IProjectRepository projectRepository, + IDistributedCache> distributedCache, + IDocumentStoreFactory documentStoreFactory) { _projectRepository = projectRepository; + _distributedCache = distributedCache; + _documentStoreFactory = documentStoreFactory; } public async Task> GetListAsync() @@ -30,5 +41,54 @@ namespace Volo.Docs.Projects return ObjectMapper.Map(project); } + + public async Task> GetVersionsAsync(Guid id) + { + //TODO: What if there is no version? + + var project = await _projectRepository.GetAsync(id); + var documentStore = _documentStoreFactory.Create(project.DocumentStoreType); + + //TODO: Why not use GetOrAddAsync + var versions = await GetVersionsFromCache(project.ShortName); + if (versions == null) + { + versions = await documentStore.GetVersions(project); + await SetVersionsToCache(project.ShortName, versions); + } + + if (!project.MinimumVersion.IsNullOrEmpty()) + { + var minVersionIndex = versions.FindIndex(v => v.Name == project.MinimumVersion); + if (minVersionIndex > -1) + { + versions = versions.GetRange(0, minVersionIndex + 1); + } + } + + if (!string.IsNullOrEmpty(project.LatestVersionBranchName)) + { + versions.First().Name = project.LatestVersionBranchName; + } + + return versions; + } + + private async Task> GetVersionsFromCache(string projectShortName) + { + return await _distributedCache.GetAsync(projectShortName); + } + + private async Task SetVersionsToCache(string projectShortName, List versions) + { + await _distributedCache.SetAsync( + projectShortName, + versions, + new DistributedCacheEntryOptions + { + SlidingExpiration = TimeSpan.FromDays(1) + } + ); + } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentConsts.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentConsts.cs new file mode 100644 index 0000000000..04371e3dff --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentConsts.cs @@ -0,0 +1,7 @@ +namespace Volo.Docs.Documents +{ + public static class DocumentConsts + { + public const int MaxNameLength = 255; + } +} diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs index 717158a166..57f3c17112 100644 --- a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs @@ -7,5 +7,6 @@ public const int MaxDefaultDocumentNameLength = 128; public const int MaxNavigationDocumentNameLength = 128; public const int MaxLatestVersionBranchNameLength = 128; + public const int MaxVersionNameLength = 128; } } diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs index 59f0524308..582813f7b0 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs @@ -60,17 +60,21 @@ namespace Volo.Docs.Pages.Documents.Project SetPageParams(project); await SetVersionAsync(project); - - await SetDocumentAsync(); - - await SetNavigationAsync(); + await SetDocumentAsync(project); + await SetNavigationAsync(project); } - private async Task SetNavigationAsync() + private async Task SetNavigationAsync(ProjectDto project) { try { - Navigation = await _documentAppService.GetNavigationDocumentAsync(ProjectName, Version, false); + Navigation = await _documentAppService.GetNavigationDocumentAsync( + new GetNavigationDocumentInput + { + ProjectId = project.Id, + Version = Version + } + ); } catch (DocumentNotFoundException) //TODO: What if called on a remote service which may return 404 { @@ -95,7 +99,7 @@ namespace Volo.Docs.Pages.Documents.Project private async Task SetVersionAsync(ProjectDto project) { - var versionInfoDtos = await _documentAppService.GetVersions(project.ShortName); + var versionInfoDtos = await _projectAppService.GetVersionsAsync(project.Id); Versions = versionInfoDtos.Select(v => new VersionInfo(v.DisplayName, v.Name)).ToList(); @@ -168,17 +172,30 @@ namespace Volo.Docs.Pages.Documents.Project Document.Version; } - private async Task SetDocumentAsync() + private async Task SetDocumentAsync(ProjectDto project) { try { if (DocumentNameWithExtension.IsNullOrWhiteSpace()) { - Document = await _documentAppService.GetDefaultAsync(ProjectName, Version, true); + Document = await _documentAppService.GetDefaultAsync( + new GetDefaultDocumentInput + { + ProjectId = project.Id, + Version = Version + } + ); } else { - Document = await _documentAppService.GetByNameAsync(ProjectName, DocumentNameWithExtension, Version, true); + Document = await _documentAppService.GetAsync( + new GetDocumentInput + { + ProjectId = project.Id, + Name = DocumentNameWithExtension, + Version = Version + } + ); } } catch (DocumentNotFoundException)