Browse Source

Refactor application layer

pull/625/head
Halil ibrahim Kalkan 8 years ago
parent
commit
96dcfe94ed
  1. 21
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs
  2. 54
      modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs
  3. 1
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs

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

@ -17,9 +17,10 @@ namespace Volo.Docs.Documents
_documentStoreFactory = documentStoreFactory;
}
public async Task<DocumentWithDetailsDto> GetAsync(GetDocumentInput input)
public virtual async Task<DocumentWithDetailsDto> GetAsync(GetDocumentInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
return await GetDocumentWithDetailsDto(
project,
input.Name,
@ -27,9 +28,10 @@ namespace Volo.Docs.Documents
);
}
public async Task<DocumentWithDetailsDto> GetDefaultAsync(GetDefaultDocumentInput input)
public virtual async Task<DocumentWithDetailsDto> GetDefaultAsync(GetDefaultDocumentInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
return await GetDocumentWithDetailsDto(
project,
project.DefaultDocumentName,
@ -40,6 +42,7 @@ namespace Volo.Docs.Documents
public virtual async Task<DocumentWithDetailsDto> GetNavigationDocumentAsync(GetNavigationDocumentInput input)
{
var project = await _projectRepository.GetAsync(input.ProjectId);
return await GetDocumentWithDetailsDto(
project,
project.NavigationDocumentName,
@ -52,13 +55,17 @@ namespace Volo.Docs.Documents
string documentName,
string version)
{
var documentStore = _documentStoreFactory.Create(project.DocumentStoreType);
var document = await documentStore.FindDocument(project, documentName, version);
var store = _documentStoreFactory.Create(project.DocumentStoreType);
var document = await store.FindDocument(project, documentName, version);
var dto = ObjectMapper.Map<Document, DocumentWithDetailsDto>(document);
dto.Project = ObjectMapper.Map<Project, ProjectDto>(project);
return CreateDocumentWithDetailsDto(project, document);
}
return dto;
protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document)
{
var documentDto = ObjectMapper.Map<Document, DocumentWithDetailsDto>(document);
documentDto.Project = ObjectMapper.Map<Project, ProjectDto>(project);
return documentDto;
}
}
}

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

@ -13,16 +13,16 @@ namespace Volo.Docs.Projects
public class ProjectAppService : ApplicationService, IProjectAppService
{
private readonly IProjectRepository _projectRepository;
private readonly IDistributedCache<List<VersionInfo>> _distributedCache;
private readonly IDistributedCache<List<VersionInfo>> _versionCache;
private readonly IDocumentStoreFactory _documentStoreFactory;
public ProjectAppService(
IProjectRepository projectRepository,
IDistributedCache<List<VersionInfo>> distributedCache,
IDistributedCache<List<VersionInfo>> versionCache,
IDocumentStoreFactory documentStoreFactory)
{
_projectRepository = projectRepository;
_distributedCache = distributedCache;
_versionCache = versionCache;
_documentStoreFactory = documentStoreFactory;
}
@ -44,17 +44,28 @@ namespace Volo.Docs.Projects
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)
var versions = await _versionCache.GetOrAddAsync(
project.ShortName,
() => GetVersionsAsync(project),
() => new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromDays(2)
}
);
return ObjectMapper.Map<List<VersionInfo>, List<VersionInfoDto>>(versions);
}
protected virtual async Task<List<VersionInfo>> GetVersionsAsync(Project project)
{
var store = _documentStoreFactory.Create(project.DocumentStoreType);
var versions = await store.GetVersions(project);
if (!versions.Any())
{
versions = await documentStore.GetVersions(project);
await SetVersionsToCache(project.ShortName, versions);
return versions;
}
if (!project.MinimumVersion.IsNullOrEmpty())
@ -66,29 +77,12 @@ namespace Volo.Docs.Projects
}
}
if (!string.IsNullOrEmpty(project.LatestVersionBranchName))
if (versions.Any() && !string.IsNullOrEmpty(project.LatestVersionBranchName))
{
versions.First().Name = project.LatestVersionBranchName;
}
return ObjectMapper.Map<List<VersionInfo>, List<VersionInfoDto>>(versions);
}
private async Task<List<VersionInfo>> GetVersionsFromCache(string projectShortName)
{
return await _distributedCache.GetAsync(projectShortName);
}
private async Task SetVersionsToCache(string projectShortName, List<VersionInfo> versions)
{
await _distributedCache.SetAsync(
projectShortName,
versions,
new DistributedCacheEntryOptions
{
SlidingExpiration = TimeSpan.FromDays(1)
}
);
return versions;
}
}
}

1
modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs

@ -91,6 +91,7 @@ namespace Volo.Docs.GitHub.Documents
}
catch (Exception ex)
{
//TODO: It may not be a good idea to hide the error!
Logger.LogError(ex.Message, ex);
return new List<VersionInfo>();
}

Loading…
Cancel
Save