Browse Source

docs language refactor

pull/1192/head
Yunus Emre Kalkan 7 years ago
parent
commit
688cd23f7e
  1. 2
      modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs
  2. 2
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentWithDetailsDto.cs
  3. 7
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs
  4. 2
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs
  5. 4
      modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs
  6. 42
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs
  7. 47
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs
  8. 0
      modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConfig.cs
  9. 0
      modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConfigElement.cs
  10. 12
      modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Projects/DocsProjectController.cs
  11. 2
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs
  12. 7
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml
  13. 82
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs

2
modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs

@ -28,7 +28,7 @@ namespace VoloDocs.Web.Pages
{ {
projectName = Projects[0].ShortName, projectName = Projects[0].ShortName,
version = DocsAppConsts.Latest, version = DocsAppConsts.Latest,
languageCode = await _projectAppService.GetDefaultLanguageCode(Projects[0].ShortName), languageCode = "",
documentName = Projects[0].DefaultDocumentName documentName = Projects[0].DefaultDocumentName
}); });
} }

2
modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentWithDetailsDto.cs

@ -25,8 +25,6 @@ namespace Volo.Docs.Documents
public string FileName { get; set; } public string FileName { get; set; }
public string CurrentLanguageCode { get; set; }
public ProjectDto Project { get; set; } public ProjectDto Project { get; set; }
public List<DocumentContributorDto> Contributors { get; set; } public List<DocumentContributorDto> Contributors { get; set; }

7
modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs

@ -1,6 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Docs.Documents;
namespace Volo.Docs.Projects namespace Volo.Docs.Projects
{ {
@ -9,9 +10,11 @@ namespace Volo.Docs.Projects
Task<ListResultDto<ProjectDto>> GetListAsync(); Task<ListResultDto<ProjectDto>> GetListAsync();
Task<ProjectDto> GetAsync(string shortName); Task<ProjectDto> GetAsync(string shortName);
Task<string> GetDefaultLanguageCode(string shortName);
Task<ListResultDto<VersionInfoDto>> GetVersionsAsync(string shortName); Task<ListResultDto<VersionInfoDto>> GetVersionsAsync(string shortName);
Task<string> GetDefaultLanguageCode(string shortName, string version);
Task<LanguageConfig> GetLanguageListAsync(string shortName, string version);
} }
} }

2
modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs

@ -26,7 +26,5 @@ namespace Volo.Docs.Projects
public string DocumentStoreType { get; set; } public string DocumentStoreType { get; set; }
public Dictionary<string, object> ExtraProperties { get; set; } public Dictionary<string, object> ExtraProperties { get; set; }
public Dictionary<string, string> Languages { get; set; }
} }
} }

4
modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs

@ -9,10 +9,10 @@ namespace Volo.Docs
{ {
public DocsApplicationAutoMapperProfile() public DocsApplicationAutoMapperProfile()
{ {
CreateMap<Project, ProjectDto>().Ignore(x=>x.Languages); CreateMap<Project, ProjectDto>();
CreateMap<VersionInfo, VersionInfoDto>(); CreateMap<VersionInfo, VersionInfoDto>();
CreateMap<Document, DocumentWithDetailsDto>() CreateMap<Document, DocumentWithDetailsDto>()
.Ignore(x => x.Project).Ignore(x => x.Contributors).Ignore(x => x.CurrentLanguageCode); .Ignore(x => x.Project).Ignore(x => x.Contributors);
CreateMap<DocumentContributor, DocumentContributorDto>(); CreateMap<DocumentContributor, DocumentContributorDto>();
CreateMap<DocumentResource, DocumentResourceDto>(); CreateMap<DocumentResource, DocumentResourceDto>();
} }

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

@ -111,11 +111,9 @@ namespace Volo.Docs.Documents
{ {
Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store..."); Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store...");
var store = _documentStoreFactory.Create(project.DocumentStoreType); var store = _documentStoreFactory.Create(project.DocumentStoreType);
var languages = await GetLanguageListAsync(store, project, version); var document = await store.GetDocumentAsync(project, documentName, languageCode, version);
var language = GetLanguageByCode(languages, languageCode);
var document = await store.GetDocumentAsync(project, documentName, language.Code, version);
Logger.LogInformation($"Document retrieved: {documentName}"); Logger.LogInformation($"Document retrieved: {documentName}");
return CreateDocumentWithDetailsDto(project, document, languages, language.Code); return CreateDocumentWithDetailsDto(project, document);
} }
if (Debugger.IsAttached) if (Debugger.IsAttached)
@ -135,45 +133,11 @@ namespace Volo.Docs.Documents
); );
} }
protected virtual LanguageConfigElement GetLanguageByCode(LanguageConfig languageCodes, string languageCode) protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document)
{
var language = languageCodes.Languages.FirstOrDefault(l => l.Code == languageCode);
return language ??
languageCodes.Languages.FirstOrDefault(l => l.IsDefault) ??
languageCodes.Languages.First();
}
protected virtual async Task<LanguageConfig> GetLanguageListAsync(IDocumentStore store, Project project, string version)
{
async Task<LanguageConfig> GetLanguagesAsync()
{
return await store.GetLanguageListAsync(project, version);
}
return await LanguageCache.GetOrAddAsync(
project.ShortName,
GetLanguagesAsync,
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24)
}
);
}
protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document, LanguageConfig languages, string languageCode)
{ {
var documentDto = ObjectMapper.Map<Document, DocumentWithDetailsDto>(document); var documentDto = ObjectMapper.Map<Document, DocumentWithDetailsDto>(document);
documentDto.Project = ObjectMapper.Map<Project, ProjectDto>(project); 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);
documentDto.Project.Languages = new Dictionary<string, string>();
foreach (var language in languages.Languages)
{
documentDto.Project.Languages.Add(language.Code, language.DisplayName);
}
documentDto.CurrentLanguageCode = languageCode;
return documentDto; return documentDto;
} }
} }

47
modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.Caching; using Volo.Abp.Caching;
@ -17,15 +16,18 @@ namespace Volo.Docs.Projects
private readonly IProjectRepository _projectRepository; private readonly IProjectRepository _projectRepository;
private readonly IDistributedCache<List<VersionInfo>> _versionCache; private readonly IDistributedCache<List<VersionInfo>> _versionCache;
private readonly IDocumentStoreFactory _documentStoreFactory; private readonly IDocumentStoreFactory _documentStoreFactory;
protected IDistributedCache<LanguageConfig> LanguageCache { get; }
public ProjectAppService( public ProjectAppService(
IProjectRepository projectRepository, IProjectRepository projectRepository,
IDistributedCache<List<VersionInfo>> versionCache, IDistributedCache<List<VersionInfo>> versionCache,
IDocumentStoreFactory documentStoreFactory) IDocumentStoreFactory documentStoreFactory,
IDistributedCache<LanguageConfig> languageCache)
{ {
_projectRepository = projectRepository; _projectRepository = projectRepository;
_versionCache = versionCache; _versionCache = versionCache;
_documentStoreFactory = documentStoreFactory; _documentStoreFactory = documentStoreFactory;
LanguageCache = languageCache;
} }
public async Task<ListResultDto<ProjectDto>> GetListAsync() public async Task<ListResultDto<ProjectDto>> GetListAsync()
@ -44,15 +46,6 @@ namespace Volo.Docs.Projects
return ObjectMapper.Map<Project, ProjectDto>(project); return ObjectMapper.Map<Project, ProjectDto>(project);
} }
public async Task<string> GetDefaultLanguageCode(string shortName)
{
var project = await _projectRepository.GetByShortNameAsync(shortName);
var store = _documentStoreFactory.Create(project.DocumentStoreType);
var languageList = await store.GetLanguageListAsync(project, project.LatestVersionBranchName);
return (languageList.Languages.FirstOrDefault(l => l.IsDefault) ?? languageList.Languages.First()).Code;
}
public async Task<ListResultDto<VersionInfoDto>> GetVersionsAsync(string shortName) public async Task<ListResultDto<VersionInfoDto>> GetVersionsAsync(string shortName)
{ {
var project = await _projectRepository.GetByShortNameAsync(shortName); var project = await _projectRepository.GetByShortNameAsync(shortName);
@ -99,5 +92,37 @@ namespace Volo.Docs.Projects
return versions; return versions;
} }
public async Task<LanguageConfig> GetLanguageListAsync(string shortName, string version)
{
return await GetLanguageListInternalAsync(shortName, version);
}
public async Task<string> GetDefaultLanguageCode(string shortName, string version)
{
var languageList = await GetLanguageListInternalAsync(shortName, version);
return (languageList.Languages.FirstOrDefault(l => l.IsDefault) ?? languageList.Languages.First()).Code;
}
private async Task<LanguageConfig> GetLanguageListInternalAsync(string shortName, string version)
{
var project = await _projectRepository.GetByShortNameAsync(shortName);
var store = _documentStoreFactory.Create(project.DocumentStoreType);
async Task<LanguageConfig> GetLanguagesAsync()
{
return await store.GetLanguageListAsync(project, version);
}
return await LanguageCache.GetOrAddAsync(
project.ShortName,
GetLanguagesAsync,
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24)
}
);
}
} }
} }

0
modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs → modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConfig.cs

0
modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs → modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConfigElement.cs

12
modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Projects/DocsProjectController.cs

@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Docs.Documents;
namespace Volo.Docs.Projects namespace Volo.Docs.Projects
{ {
@ -36,9 +37,9 @@ namespace Volo.Docs.Projects
[HttpGet] [HttpGet]
[Route("{shortName}/defaultLanguage")] [Route("{shortName}/defaultLanguage")]
public Task<string> GetDefaultLanguageCode(string shortName) public Task<string> GetDefaultLanguageCode(string shortName,string version)
{ {
return ProjectAppService.GetDefaultLanguageCode(shortName); return ProjectAppService.GetDefaultLanguageCode(shortName, version);
} }
[HttpGet] [HttpGet]
@ -47,5 +48,12 @@ namespace Volo.Docs.Projects
{ {
return ProjectAppService.GetVersionsAsync(shortName); return ProjectAppService.GetVersionsAsync(shortName);
} }
[HttpGet]
[Route("{shortName}/{versiion}/languageList")]
public Task<LanguageConfig> GetLanguageListAsync(string shortName, string version)
{
return ProjectAppService.GetLanguageListAsync(shortName, version);
}
} }
} }

2
modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs

@ -27,7 +27,7 @@ namespace Volo.Docs.Pages.Documents
{ {
projectName = listResult.Items[0].ShortName, projectName = listResult.Items[0].ShortName,
version = DocsAppConsts.Latest, version = DocsAppConsts.Latest,
languageCode = await _projectAppService.GetDefaultLanguageCode(listResult.Items[0].ShortName), languageCode = "",
documentName = listResult.Items[0].DefaultDocumentName documentName = listResult.Items[0].DefaultDocumentName
}); });
} }

7
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml

@ -144,7 +144,6 @@
@if (Model.Document != null) @if (Model.Document != null)
{ {
<div class="col-md-7 docs-content bg-white"> <div class="col-md-7 docs-content bg-white">
<div class="docs-link-btns"> <div class="docs-link-btns">
<div class="float-left"> <div class="float-left">
@(L["ShareOn"].Value + " :") @(L["ShareOn"].Value + " :")
@ -191,6 +190,12 @@
<div class="docs-text-field"> <div class="docs-text-field">
<div data-spy="scroll" data-target="#docs-sticky-index" data-offset="0"> <div data-spy="scroll" data-target="#docs-sticky-index" data-offset="0">
<article class="docs-body"> <article class="docs-body">
@if (Model.DocumentLanguageIsDifferent)
{
<abp-alert alert-type="Warning" dismissible="true" class="mb-0">
Document in the language you wanted is not found. Document in the default language is shown.
</abp-alert>
}
@Html.Raw(Model.Document.Content) @Html.Raw(Model.Document.Content)
</article> </article>
</div> </div>

82
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs

@ -27,8 +27,12 @@ namespace Volo.Docs.Pages.Documents.Project
[BindProperty(SupportsGet = true)] [BindProperty(SupportsGet = true)]
public string LanguageCode { get; set; } public string LanguageCode { get; set; }
public string DefaultLanguageCode { get; set; }
public ProjectDto Project { get; set; } public ProjectDto Project { get; set; }
public LanguageConfig LanguageConfig { get; set; }
public List<SelectListItem> LanguageSelectListItems { get; set; } public List<SelectListItem> LanguageSelectListItems { get; set; }
public string DocumentNameWithExtension { get; private set; } public string DocumentNameWithExtension { get; private set; }
@ -43,6 +47,8 @@ namespace Volo.Docs.Pages.Documents.Project
public VersionInfoViewModel LatestVersionInfo { get; private set; } public VersionInfoViewModel LatestVersionInfo { get; private set; }
public bool DocumentLanguageIsDifferent { get; set; }
private readonly IDocumentAppService _documentAppService; private readonly IDocumentAppService _documentAppService;
private readonly IDocumentToHtmlConverterFactory _documentToHtmlConverterFactory; private readonly IDocumentToHtmlConverterFactory _documentToHtmlConverterFactory;
private readonly IProjectAppService _projectAppService; private readonly IProjectAppService _projectAppService;
@ -57,15 +63,24 @@ namespace Volo.Docs.Pages.Documents.Project
_projectAppService = projectAppService; _projectAppService = projectAppService;
} }
public async Task OnGetAsync() public async Task<IActionResult> OnGetAsync()
{ {
await SetProjectAsync(); await SetProjectAsync();
await SetProjectsAsync(); await SetProjectsAsync();
await SetVersionAsync(); await SetVersionAsync();
await SetLanguageList();
if (!CheckLanguage())
{
return RedirectToDefaultLanguage();
}
await SetDocumentAsync(); await SetDocumentAsync();
await SetNavigationAsync(); await SetNavigationAsync();
SetLanguageSelectListItems(); SetLanguageSelectListItems();
AddLanguageCodePrefixToLinks(); AddLanguageCodePrefixToLinks();
return Page();
} }
private async Task SetProjectAsync() private async Task SetProjectAsync()
@ -73,6 +88,32 @@ namespace Volo.Docs.Pages.Documents.Project
Project = await _projectAppService.GetAsync(ProjectName); Project = await _projectAppService.GetAsync(ProjectName);
} }
private async Task SetLanguageList()
{
LanguageConfig = await _projectAppService.GetLanguageListAsync(ProjectName, Version);
SetDefaultLanguageCode();
}
private void SetDefaultLanguageCode()
{
DefaultLanguageCode = (LanguageConfig.Languages.FirstOrDefault(l => l.IsDefault) ?? LanguageConfig.Languages.First()).Code;
}
private bool CheckLanguage()
{
return LanguageConfig.Languages.Any(l => l.Code == LanguageCode);
}
private IActionResult RedirectToDefaultLanguage()
{
return RedirectToPage(new
{
projectName = ProjectName,
version = Version,
languageCode = DefaultLanguageCode
});
}
private async Task SetProjectsAsync() private async Task SetProjectsAsync()
{ {
var projects = await _projectAppService.GetListAsync(); var projects = await _projectAppService.GetListAsync();
@ -153,8 +194,6 @@ namespace Volo.Docs.Pages.Documents.Project
return; return;
} }
LanguageCode = Document.CurrentLanguageCode;
Navigation.ConvertItems(); Navigation.ConvertItems();
} }
@ -221,21 +260,28 @@ namespace Volo.Docs.Pages.Documents.Project
} }
); );
} }
} }
catch (DocumentNotFoundException) catch (DocumentNotFoundException)
{ {
Document = await _documentAppService.GetDefaultAsync( if (LanguageCode != DefaultLanguageCode)
new GetDefaultDocumentInput {
{ Document = await _documentAppService.GetAsync(
ProjectId = Project.Id, new GetDocumentInput
LanguageCode = LanguageCode, {
Version = Version ProjectId = Project.Id,
} Name = DocumentNameWithExtension,
); LanguageCode = DefaultLanguageCode,
} Version = Version
}
);
LanguageCode = Document.CurrentLanguageCode; DocumentLanguageIsDifferent = true;
}
else
{
throw;
}
}
ConvertDocumentContentToHtml(); ConvertDocumentContentToHtml();
} }
@ -244,13 +290,13 @@ namespace Volo.Docs.Pages.Documents.Project
{ {
LanguageSelectListItems = new List<SelectListItem>(); LanguageSelectListItems = new List<SelectListItem>();
foreach (var language in Document.Project.Languages) foreach (var language in LanguageConfig.Languages)
{ {
LanguageSelectListItems.Add( LanguageSelectListItems.Add(
new SelectListItem( new SelectListItem(
language.Value, language.DisplayName,
"/documents/" + language.Key + "/" + Project.ShortName + "/" + Version + "/" + DocumentName, "/documents/" + language.Code + "/" + Project.ShortName + "/" + Version + "/" + DocumentName,
language.Key == LanguageCode language.Code == LanguageCode
) )
); );
} }

Loading…
Cancel
Save