Browse Source

Refactor document app service interface.

pull/625/head
Halil ibrahim Kalkan 8 years ago
parent
commit
04392b2b47
  1. 15
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs
  2. 19
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs
  3. 15
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs
  4. 23
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs
  5. 5
      modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs
  6. 93
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs
  7. 64
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs
  8. 7
      modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Documents/DocumentConsts.cs
  9. 1
      modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs
  10. 37
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs

15
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; }
}
}

19
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; }
}
}

15
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; }
}
}

23
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<DocumentWithDetailsDto> GetAsync(GetDocumentInput input);
Task<DocumentWithDetailsDto> GetByNameAsync(
string projectShortName,
string documentName,
string version,
bool normalize);
Task<DocumentWithDetailsDto> GetDefaultAsync(GetDefaultDocumentInput input);
Task<DocumentWithDetailsDto> GetDefaultAsync(
string projectShortName,
string version,
bool normalize);
Task<NavigationWithDetailsDto> GetNavigationDocumentAsync(
string projectShortName,
string version,
bool normalize);
Task<List<VersionInfoDto>> GetVersions(
string projectShortName
);
Task<NavigationWithDetailsDto> GetNavigationDocumentAsync(GetNavigationDocumentInput input);
}
}

5
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<ListResultDto<ProjectDto>> GetListAsync();
Task<ProjectDto> GetByShortNameAsync(string shortName);
Task<List<VersionInfoDto>> GetVersionsAsync(Guid projectId);
}
}

93
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<List<VersionInfoDto>> _distributedCache;
private readonly IDocumentStoreFactory _documentStoreFactory;
public DocumentAppService(
IProjectRepository projectRepository,
IDistributedCache<List<VersionInfoDto>> distributedCache,
IDocumentStoreFactory documentStoreFactory)
{
_projectRepository = projectRepository;
_distributedCache = distributedCache;
_documentStoreFactory = documentStoreFactory;
}
public async Task<DocumentWithDetailsDto> GetByNameAsync(
string projectShortName,
string documentName,
string version,
bool normalize)
public async Task<DocumentWithDetailsDto> 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<DocumentWithDetailsDto> GetDefaultAsync(
string projectShortName,
string version,
bool normalize)
public async Task<DocumentWithDetailsDto> 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<NavigationWithDetailsDto> GetNavigationDocumentAsync(
string projectShortName,
string version,
bool normalize)
public virtual async Task<NavigationWithDetailsDto> 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<DocumentWithDetailsDto, NavigationWithDetailsDto>(documentDto);
@ -73,8 +52,7 @@ namespace Volo.Docs.Documents
protected virtual async Task<DocumentWithDetailsDto> 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<List<VersionInfoDto>> 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<List<VersionInfoDto>> GetVersionsFromCache(string projectShortName)
{
return await _distributedCache.GetAsync(projectShortName);
}
private async Task SetVersionsToCache(string projectShortName, List<VersionInfoDto> versions)
{
await _distributedCache.SetAsync(
projectShortName,
versions,
new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromDays(1)
}
);
}
}
}

64
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<List<VersionInfoDto>> _distributedCache;
private readonly IDocumentStoreFactory _documentStoreFactory;
public ProjectAppService(IProjectRepository projectRepository)
public ProjectAppService(
IProjectRepository projectRepository,
IDistributedCache<List<VersionInfoDto>> distributedCache,
IDocumentStoreFactory documentStoreFactory)
{
_projectRepository = projectRepository;
_distributedCache = distributedCache;
_documentStoreFactory = documentStoreFactory;
}
public async Task<ListResultDto<ProjectDto>> GetListAsync()
@ -30,5 +41,54 @@ namespace Volo.Docs.Projects
return ObjectMapper.Map<Project, ProjectDto>(project);
}
public async Task<List<VersionInfoDto>> 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<List<VersionInfoDto>> GetVersionsFromCache(string projectShortName)
{
return await _distributedCache.GetAsync(projectShortName);
}
private async Task SetVersionsToCache(string projectShortName, List<VersionInfoDto> versions)
{
await _distributedCache.SetAsync(
projectShortName,
versions,
new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromDays(1)
}
);
}
}
}

7
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;
}
}

1
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;
}
}

37
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)

Loading…
Cancel
Save