Browse Source

refactor doc app service

pull/9161/head
Alper Ebicoglu 5 years ago
parent
commit
1d9f322629
  1. 99
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs

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

@ -144,7 +144,9 @@ namespace Volo.Docs.Documents
input.Version = string.IsNullOrWhiteSpace(input.Version) ? project.LatestVersionBranchName : input.Version;
input.Version = GetProjectVersionPrefixIfExist(project) + input.Version;
var cacheKey = CacheKeyGenerator.GenerateDocumentResourceCacheKey(project, input.Name, input.LanguageCode,input.Version);
var cacheKey =
CacheKeyGenerator.GenerateDocumentResourceCacheKey(project, input.Name, input.LanguageCode,
input.Version);
async Task<DocumentResource> GetResourceAsync()
{
@ -180,13 +182,15 @@ namespace Volo.Docs.Documents
await _documentFullSearch.SearchAsync(input.Context, project.Id, input.LanguageCode, input.Version);
return esDocs.Select(esDoc => new DocumentSearchOutput //TODO: auto map
{
Name = esDoc.Name,
FileName = esDoc.FileName,
Version = esDoc.Version,
LanguageCode = esDoc.LanguageCode,
Highlight = esDoc.Highlight
}).Where(x => x.FileName != project.NavigationDocumentName && x.FileName != project.ParametersDocumentName).ToList();
{
Name = esDoc.Name,
FileName = esDoc.FileName,
Version = esDoc.Version,
LanguageCode = esDoc.LanguageCode,
Highlight = esDoc.Highlight
}).Where(x =>
x.FileName != project.NavigationDocumentName && x.FileName != project.ParametersDocumentName)
.ToList();
}
public async Task<bool> FullSearchEnabledAsync()
@ -205,34 +209,63 @@ namespace Volo.Docs.Documents
foreach (var document in documents)
{
var version = GetProjectVersionPrefixIfExist(project) + document.Version;
var navigationDocument = await GetDocumentWithDetailsDtoAsync(
project,
project.NavigationDocumentName,
document.LanguageCode,
version
);
if (!DocsJsonSerializerHelper.TryDeserialize<NavigationNode>(navigationDocument.Content,
out var navigationNode))
try
{
throw new UserFriendlyException(
$"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
await AddDocumentToUrls(prefix, project, document, documentUrls);
}
navigationNode.Items?.ForEach(node =>
catch (Exception ex)
{
documentUrls.AddIfNotContains(
GetDocumentLinks(node, documentUrls, prefix, project.ShortName, document)
);
});
Logger.LogException(ex);
}
}
}
return documentUrls;
}
private List<string> GetDocumentLinks(NavigationNode node, List<string> documentUrls, string prefix,
private async Task AddDocumentToUrls(string prefix, Project project, Document document,
List<string> documentUrls)
{
var navigationNodes = await GetNavigationNodesAsync(prefix, project, document);
AddDocumentUrls(prefix, navigationNodes, documentUrls, project, document);
}
private void AddDocumentUrls(string prefix,
List<NavigationNode> navigationNodes,
List<string> documentUrls,
Project project,
Document document)
{
navigationNodes?.ForEach(node =>
{
documentUrls.AddIfNotContains(
GetDocumentLinks(node, documentUrls, prefix, project.ShortName, document)
);
});
}
private async Task<List<NavigationNode>> GetNavigationNodesAsync(string prefix, Project project,
Document document)
{
var version = GetProjectVersionPrefixIfExist(project) + document.Version;
var navigationDocument = await GetDocumentWithDetailsDtoAsync(
project,
project.NavigationDocumentName,
document.LanguageCode,
version
);
if (!DocsJsonSerializerHelper.TryDeserialize<NavigationNode>(navigationDocument.Content,
out var navigationNode))
{
throw new UserFriendlyException(
$"Cannot validate navigation file '{project.NavigationDocumentName}' for the project {project.Name}.");
}
return navigationNode.Items;
}
private List<string> GetDocumentLinks(NavigationNode node, List<string> documentUrls, string prefix,
string shortName, Document document)
{
if (!IsExternalLink(node.Path))
@ -253,7 +286,8 @@ namespace Volo.Docs.Documents
private string NormalizePath(string prefix, string path, string shortName, Document document)
{
var pathWithoutFileExtension = RemoveFileExtensionFromPath(path, document.Format);
var normalizedPath = prefix + document.LanguageCode + "/" + shortName + "/" + document.Version + "/" + pathWithoutFileExtension;
var normalizedPath = prefix + document.LanguageCode + "/" + shortName + "/" + document.Version + "/" +
pathWithoutFileExtension;
return normalizedPath;
}
@ -301,7 +335,8 @@ namespace Volo.Docs.Documents
input.Version
);
if (!DocsJsonSerializerHelper.TryDeserialize<DocumentParametersDto>(document.Content,out var documentParameters))
if (!DocsJsonSerializerHelper.TryDeserialize<DocumentParametersDto>(document.Content,
out var documentParameters))
{
throw new UserFriendlyException(
$"Cannot validate document parameters file '{project.ParametersDocumentName}' for the project {project.Name}.");
@ -363,7 +398,8 @@ 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;
}
@ -374,7 +410,8 @@ namespace Volo.Docs.Documents
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);

Loading…
Cancel
Save