Browse Source

refactor docs routing

pull/19419/head
Salih 3 years ago
parent
commit
4f12074a13
  1. 42
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs

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

@ -49,8 +49,6 @@ namespace Volo.Docs.Pages.Documents.Project
public ProjectDto Project { get; set; } public ProjectDto Project { get; set; }
public LanguageConfig LanguageConfig { get; set; } public LanguageConfig LanguageConfig { get; set; }
public ListResultDto<VersionInfoDto> Versions { get; set; }
public List<SelectListItem> LanguageSelectListItems { get; set; } public List<SelectListItem> LanguageSelectListItems { get; set; }
@ -154,7 +152,10 @@ namespace Volo.Docs.Pages.Documents.Project
await SetProjectsAsync(); await SetProjectsAsync();
} }
await SetVersionAsync(); if (!await TrySetVersionAsync())
{
return RedirectToDefaultVersion();
}
await SetLanguageList(); await SetLanguageList();
if (IsDefaultDocument()) if (IsDefaultDocument())
@ -166,18 +167,13 @@ namespace Volo.Docs.Pages.Documents.Project
{ {
return RedirectToDefaultLanguage(); return RedirectToDefaultLanguage();
} }
if (!CheckVersion())
{
return RedirectToDefaultVersion();
}
if (IsDocumentCultureDifferentThanCurrent()) if (IsDocumentCultureDifferentThanCurrent())
{ {
return ReloadPageWithCulture(); return ReloadPageWithCulture();
} }
if (await TrySetDocumentAsync()) if (!await TrySetDocumentAsync())
{ {
DocumentFound = false; DocumentFound = false;
Response.StatusCode = 404; Response.StatusCode = 404;
@ -246,11 +242,6 @@ namespace Volo.Docs.Pages.Documents.Project
{ {
return LanguageConfig.Languages.Any(l => l.Code.Equals(LanguageCode, StringComparison.OrdinalIgnoreCase)); return LanguageConfig.Languages.Any(l => l.Code.Equals(LanguageCode, StringComparison.OrdinalIgnoreCase));
} }
private bool CheckVersion()
{
return Version == DocsAppConsts.Latest || Versions.Items.Any(v => v.Name.Equals(Version, StringComparison.OrdinalIgnoreCase));
}
private IActionResult ReloadPageWithCulture() private IActionResult ReloadPageWithCulture()
{ {
@ -286,7 +277,7 @@ namespace Volo.Docs.Pages.Documents.Project
private IActionResult RedirectToDefaultLanguage() private IActionResult RedirectToDefaultLanguage()
{ {
return RedirectToPage(BuildDocumentUrl( return Redirect(BuildDocumentUrl(
projectName : ProjectName, projectName : ProjectName,
version : (LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Version), version : (LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Version),
languageCode : DefaultLanguageCode, languageCode : DefaultLanguageCode,
@ -296,7 +287,7 @@ namespace Volo.Docs.Pages.Documents.Project
private IActionResult RedirectToDefaultVersion() private IActionResult RedirectToDefaultVersion()
{ {
return RedirectToPage(BuildDocumentUrl( return Redirect(BuildDocumentUrl(
projectName : ProjectName, projectName : ProjectName,
version : DocsAppConsts.Latest, version : DocsAppConsts.Latest,
languageCode : LanguageCode, languageCode : LanguageCode,
@ -306,7 +297,7 @@ namespace Volo.Docs.Pages.Documents.Project
private IActionResult RedirectToDefaultDocument() private IActionResult RedirectToDefaultDocument()
{ {
return RedirectToPage(BuildDocumentUrl ( return Redirect(BuildDocumentUrl (
projectName : ProjectName, projectName : ProjectName,
version : (LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Version), version : (LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Version),
documentName : "", documentName : "",
@ -335,7 +326,7 @@ namespace Volo.Docs.Pages.Documents.Project
return BuildDocumentUrl(project.ShortName, LanguageCode, Version, null); return BuildDocumentUrl(project.ShortName, LanguageCode, Version, null);
} }
private async Task SetVersionAsync() private async Task<bool> TrySetVersionAsync()
{ {
//TODO: Needs refactoring //TODO: Needs refactoring
if (string.IsNullOrWhiteSpace(Version)) if (string.IsNullOrWhiteSpace(Version))
@ -343,8 +334,8 @@ namespace Volo.Docs.Pages.Documents.Project
Version = DocsAppConsts.Latest; Version = DocsAppConsts.Latest;
} }
Versions = await _projectAppService.GetVersionsAsync(Project.ShortName); var output = await _projectAppService.GetVersionsAsync(Project.ShortName);
var versions = Versions.Items var versions = output.Items
.Select(v => new VersionInfoViewModel(v.DisplayName, v.Name)) .Select(v => new VersionInfoViewModel(v.DisplayName, v.Name))
.ToList(); .ToList();
@ -372,8 +363,7 @@ namespace Volo.Docs.Pages.Documents.Project
} }
else else
{ {
LatestVersionInfo.IsSelected = true; return false;
Version = LatestVersionInfo.Version;
} }
} }
} }
@ -398,6 +388,8 @@ namespace Volo.Docs.Pages.Documents.Project
}).ToList(); }).ToList();
IsLatestVersion = Version == LatestVersionInfo.Version; IsLatestVersion = Version == LatestVersionInfo.Version;
return true;
} }
private VersionInfoViewModel GetLatestVersionInfo(List<VersionInfoViewModel> versions) private VersionInfoViewModel GetLatestVersionInfo(List<VersionInfoViewModel> versions)
@ -492,8 +484,6 @@ namespace Volo.Docs.Pages.Documents.Project
private async Task<bool> TrySetDocumentAsync() private async Task<bool> TrySetDocumentAsync()
{ {
var sb = new StringBuilder();
var documentPath = DocumentName ?? ""; var documentPath = DocumentName ?? "";
if (Request.GetDisplayUrl().EndsWith("/index", StringComparison.OrdinalIgnoreCase)) if (Request.GetDisplayUrl().EndsWith("/index", StringComparison.OrdinalIgnoreCase))
@ -503,11 +493,11 @@ namespace Volo.Docs.Pages.Documents.Project
var documentNames = new[] { DocumentName, documentPath.EnsureEndsWith('/') + "Index", documentPath.EnsureEndsWith('/') + "index" }.Distinct().ToArray(); var documentNames = new[] { DocumentName, documentPath.EnsureEndsWith('/') + "Index", documentPath.EnsureEndsWith('/') + "index" }.Distinct().ToArray();
var languages = new[] { LanguageCode, DefaultLanguageCode }.Distinct().ToArray(); var languages = new[] { LanguageCode, DefaultLanguageCode }.Distinct().ToArray();
var sb = new StringBuilder();
foreach (var documentName in documentNames) foreach (var documentName in documentNames)
{ {
DocumentName = documentName; DocumentName = documentName;
DocumentNameWithExtension = sb.Append(DocumentName).Append('.').Append(Project.Format).ToString(); DocumentNameWithExtension = sb.Clear().Append(DocumentName).Append('.').Append(Project.Format).ToString();
foreach (var language in languages) foreach (var language in languages)
{ {
try try

Loading…
Cancel
Save