Browse Source

Created extension point for the navigation document: INavigationTreePostProcessor.

pull/4266/head
Halil İbrahim Kalkan 6 years ago
parent
commit
f4ca36be1a
  1. 5
      modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationModule.cs
  2. 74
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs
  3. 9
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/INavigationTreePostProcessor.cs
  4. 14
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/NavigationTreePostProcessorContext.cs
  5. 20
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/NullNavigationTreePostProcessor.cs

5
modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationModule.cs

@ -1,7 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.AutoMapper;
using Volo.Abp.Caching;
using Volo.Abp.Modularity;
using Volo.Docs.Documents;
namespace Volo.Docs
{
@ -15,10 +17,13 @@ namespace Volo.Docs
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAutoMapperObjectMapper<DocsApplicationModule>();
Configure<AbpAutoMapperOptions>(options =>
{
options.AddProfile<DocsApplicationAutoMapperProfile>(validate: true);
});
context.Services.TryAddSingleton<INavigationTreePostProcessor>(NullNavigationTreePostProcessor.Instance);
}
}
}

74
modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs

@ -18,6 +18,8 @@ namespace Volo.Docs.Documents
{
public class DocumentAppService : DocsAppServiceBase, IDocumentAppService
{
public INavigationTreePostProcessor NavigationTreePostProcessor { get; set; }
private readonly IProjectRepository _projectRepository;
private readonly IDocumentRepository _documentRepository;
private readonly IDocumentSourceFactory _documentStoreFactory;
@ -54,6 +56,8 @@ namespace Volo.Docs.Documents
_cacheTimeout = GetCacheTimeout();
_documentResourceAbsoluteExpiration = GetDocumentResourceAbsoluteExpirationTimeout();
_documentResourceSlidingExpiration = GetDocumentResourceSlidingExpirationTimeout();
NavigationTreePostProcessor = NullNavigationTreePostProcessor.Instance;
}
public virtual async Task<DocumentWithDetailsDto> GetAsync(GetDocumentInput input)
@ -91,9 +95,11 @@ namespace Volo.Docs.Documents
input.Version
);
if (!JsonConvertExtensions.TryDeserializeObject<NavigationNode>(navigationDocument.Content, out var navigationNode))
if (!JsonConvertExtensions.TryDeserializeObject<NavigationNode>(navigationDocument.Content,
out var navigationNode))
{
throw new UserFriendlyException($"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
throw new UserFriendlyException(
$"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
}
var leafs = navigationNode.Items.GetAllNodes(x => x.Items)
@ -102,7 +108,9 @@ namespace Volo.Docs.Documents
foreach (var leaf in leafs)
{
var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, leaf.Path, input.LanguageCode, input.Version);
var cacheKey =
CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, leaf.Path, input.LanguageCode,
input.Version);
var documentUpdateInfo = await DocumentUpdateCache.GetAsync(cacheKey);
if (documentUpdateInfo != null)
{
@ -112,13 +120,22 @@ namespace Volo.Docs.Documents
}
}
await NavigationTreePostProcessor.ProcessAsync(
new NavigationTreePostProcessorContext(
navigationDocument,
navigationNode
)
);
return navigationNode;
}
public async Task<DocumentResourceDto> GetResourceAsync(GetDocumentResourceInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
var cacheKey = CacheKeyGenerator.GenerateDocumentResourceCacheKey(project, input.Name, input.LanguageCode, input.Version);
var cacheKey =
CacheKeyGenerator.GenerateDocumentResourceCacheKey(project, input.Name, input.LanguageCode,
input.Version);
input.Version = string.IsNullOrWhiteSpace(input.Version) ? project.LatestVersionBranchName : input.Version;
async Task<DocumentResource> GetResourceAsync()
@ -133,25 +150,26 @@ namespace Volo.Docs.Documents
}
return ObjectMapper.Map<DocumentResource, DocumentResourceDto>(
await ResourceCache.GetOrAddAsync(
cacheKey,
GetResourceAsync,
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = _documentResourceAbsoluteExpiration,
SlidingExpiration = _documentResourceSlidingExpiration
}
)
);
await ResourceCache.GetOrAddAsync(
cacheKey,
GetResourceAsync,
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = _documentResourceAbsoluteExpiration,
SlidingExpiration = _documentResourceSlidingExpiration
}
)
);
}
public async Task<List<DocumentSearchOutput>> SearchAsync(DocumentSearchInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
var esDocs = await _documentFullSearch.SearchAsync(input.Context, project.Id, input.LanguageCode, input.Version);
var esDocs =
await _documentFullSearch.SearchAsync(input.Context, project.Id, input.LanguageCode, input.Version);
return esDocs.Select(esDoc => new DocumentSearchOutput//TODO: auto map
return esDocs.Select(esDoc => new DocumentSearchOutput //TODO: auto map
{
Name = esDoc.Name,
FileName = esDoc.FileName,
@ -184,9 +202,11 @@ namespace Volo.Docs.Documents
input.Version
);
if (!JsonConvertExtensions.TryDeserializeObject<DocumentParametersDto>(document.Content, out var documentParameters))
if (!JsonConvertExtensions.TryDeserializeObject<DocumentParametersDto>(document.Content,
out var documentParameters))
{
throw new UserFriendlyException($"Cannot validate document parameters file '{project.ParametersDocumentName}' for the project {project.Name}.");
throw new UserFriendlyException(
$"Cannot validate document parameters file '{project.ParametersDocumentName}' for the project {project.Name}.");
}
return documentParameters;
@ -225,7 +245,8 @@ namespace Volo.Docs.Documents
return await GetDocumentAsync(documentName, project, languageCode, version, document);
}
var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, document.Name, document.LanguageCode, document.Version);
var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, document.Name,
document.LanguageCode, document.Version);
await DocumentUpdateCache.SetAsync(cacheKey, new DocumentUpdateInfo
{
Name = document.Name,
@ -241,23 +262,28 @@ namespace Volo.Docs.Documents
{
var documentDto = ObjectMapper.Map<Document, DocumentWithDetailsDto>(document);
documentDto.Project = ObjectMapper.Map<Project, ProjectDto>(project);
documentDto.Contributors = ObjectMapper.Map<List<DocumentContributor>, List<DocumentContributorDto>>(document.Contributors);
documentDto.Contributors =
ObjectMapper.Map<List<DocumentContributor>, List<DocumentContributorDto>>(document.Contributors);
return documentDto;
}
private async Task<DocumentWithDetailsDto> GetDocumentAsync(string documentName, Project project, string languageCode, string version, Document oldDocument = null)
private async Task<DocumentWithDetailsDto> GetDocumentAsync(string documentName, Project project,
string languageCode, string version, Document oldDocument = null)
{
Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the source...");
var source = _documentStoreFactory.Create(project.DocumentStoreType);
var sourceDocument = await source.GetDocumentAsync(project, documentName, languageCode, version, oldDocument?.LastSignificantUpdateTime);
var sourceDocument = await source.GetDocumentAsync(project, documentName, languageCode, version,
oldDocument?.LastSignificantUpdateTime);
await _documentRepository.DeleteAsync(project.Id, sourceDocument.Name, sourceDocument.LanguageCode, sourceDocument.Version);
await _documentRepository.DeleteAsync(project.Id, sourceDocument.Name, sourceDocument.LanguageCode,
sourceDocument.Version);
await _documentRepository.InsertAsync(sourceDocument, true);
Logger.LogInformation($"Document retrieved: {documentName}");
var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, sourceDocument.Name, sourceDocument.LanguageCode, sourceDocument.Version);
var cacheKey = CacheKeyGenerator.GenerateDocumentUpdateInfoCacheKey(project, sourceDocument.Name,
sourceDocument.LanguageCode, sourceDocument.Version);
await DocumentUpdateCache.SetAsync(cacheKey, new DocumentUpdateInfo
{
Name = sourceDocument.Name,

9
modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/INavigationTreePostProcessor.cs

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Docs.Documents
{
public interface INavigationTreePostProcessor
{
Task ProcessAsync(NavigationTreePostProcessorContext context);
}
}

14
modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/NavigationTreePostProcessorContext.cs

@ -0,0 +1,14 @@
namespace Volo.Docs.Documents
{
public class NavigationTreePostProcessorContext
{
public DocumentWithDetailsDto Document { get; }
public NavigationNode RootNode { get; }
public NavigationTreePostProcessorContext(DocumentWithDetailsDto document, NavigationNode rootNode)
{
Document = document;
RootNode = rootNode;
}
}
}

20
modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/NullNavigationTreePostProcessor.cs

@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Docs.Documents
{
public sealed class NullNavigationTreePostProcessor : INavigationTreePostProcessor
{
public static NullNavigationTreePostProcessor Instance { get; } = new NullNavigationTreePostProcessor();
private NullNavigationTreePostProcessor()
{
}
public Task ProcessAsync(NavigationTreePostProcessorContext context)
{
return Task.CompletedTask;
}
}
}
Loading…
Cancel
Save