forked from ddrilling/AsbCloudServer
Поправлен сервис каталога инструкций
This commit is contained in:
parent
380776c3a9
commit
eaf0885675
@ -1,8 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using AsbCloudApp.Data.Manuals;
|
|
||||||
|
|
||||||
namespace AsbCloudApp.Services;
|
namespace AsbCloudApp.Services;
|
||||||
|
|
||||||
@ -14,42 +12,39 @@ public interface IManualCatalogService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Сохранение файла
|
/// Сохранение файла
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idCategory"></param>
|
/// <param name="idDirectory"></param>
|
||||||
/// <param name="idFolder"></param>
|
/// <param name="idAuthor"></param>
|
||||||
/// <param name="name"></param>
|
/// <param name="name"></param>
|
||||||
/// <param name="stream"></param>
|
/// <param name="stream"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> SaveFileAsync(int? idCategory, int? idFolder, string name, Stream stream,
|
Task<int> SaveFileAsync(int idDirectory, int idAuthor, string name, Stream stream, CancellationToken cancellationToken);
|
||||||
CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавление новой папки
|
/// Добавление директории
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name"></param>
|
/// <param name="name"></param>
|
||||||
/// <param name="idParent"></param>
|
/// <param name="idParent"></param>
|
||||||
/// <param name="idCategory"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> AddFolderAsync(string name, int? idParent, int idCategory,
|
Task<int> AddDirectoryAsync(string name, int? idParent, CancellationToken cancellationToken);
|
||||||
CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Обновление папки
|
/// Обновление директории
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <param name="name"></param>
|
/// <param name="name"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task UpdateFolderAsync(int id, string name, CancellationToken cancellationToken);
|
Task UpdateDirectoryAsync(int id, string name, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление папки
|
/// Удаление директории
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> DeleteFolderAsync(int id, CancellationToken cancellationToken);
|
Task<int> DeleteDirectoryAsync(int id, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Удаление файла
|
/// Удаление файла
|
||||||
@ -66,11 +61,4 @@ public interface IManualCatalogService
|
|||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<(Stream stream, string fileName)?> GetFileAsync(int id, CancellationToken cancellationToken);
|
Task<(Stream stream, string fileName)?> GetFileAsync(int id, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Получение каталога
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task<IEnumerable<CatalogItemManualDto>> GetCatalogAsync(CancellationToken cancellationToken);
|
|
||||||
}
|
}
|
@ -8,13 +8,14 @@ using AsbCloudApp.Data.Manuals;
|
|||||||
using AsbCloudApp.Exceptions;
|
using AsbCloudApp.Exceptions;
|
||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using AsbCloudDb.Model;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services;
|
namespace AsbCloudInfrastructure.Services;
|
||||||
|
|
||||||
public class ManualCatalogService : IManualCatalogService
|
public class ManualCatalogService : IManualCatalogService
|
||||||
{
|
{
|
||||||
|
private const int IdFileCategory = 30000;
|
||||||
|
|
||||||
private readonly IEnumerable<string> validExtensions = new[]
|
private readonly IEnumerable<string> validExtensions = new[]
|
||||||
{
|
{
|
||||||
".pdf",
|
".pdf",
|
||||||
@ -23,28 +24,24 @@ public class ManualCatalogService : IManualCatalogService
|
|||||||
|
|
||||||
private readonly string directoryFiles;
|
private readonly string directoryFiles;
|
||||||
private readonly IFileStorageRepository fileStorageRepository;
|
private readonly IFileStorageRepository fileStorageRepository;
|
||||||
private readonly IManualFolderRepository manualFolderRepository;
|
private readonly IManualDirectoryRepository manualDirectoryRepository;
|
||||||
private readonly IManualRepository manualRepository;
|
private readonly ICrudRepository<ManualDto> manualRepository;
|
||||||
private readonly IFileCategoryRepository fileCategoryRepository;
|
|
||||||
|
|
||||||
public ManualCatalogService(IFileStorageRepository fileStorageRepository,
|
public ManualCatalogService(IFileStorageRepository fileStorageRepository,
|
||||||
IManualFolderRepository manualFolderRepository,
|
IManualDirectoryRepository manualDirectoryRepository,
|
||||||
IManualRepository manualRepository,
|
ICrudRepository<ManualDto> manualRepository,
|
||||||
IFileCategoryRepository fileCategoryRepository,
|
|
||||||
IConfiguration configuration)
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
this.fileStorageRepository = fileStorageRepository;
|
this.fileStorageRepository = fileStorageRepository;
|
||||||
this.manualFolderRepository = manualFolderRepository;
|
this.manualDirectoryRepository = manualDirectoryRepository;
|
||||||
this.manualRepository = manualRepository;
|
this.manualRepository = manualRepository;
|
||||||
this.fileCategoryRepository = fileCategoryRepository;
|
|
||||||
directoryFiles = configuration.GetValue<string>("DirectoryManualFiles");
|
directoryFiles = configuration.GetValue<string>("DirectoryManualFiles");
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(directoryFiles))
|
if (string.IsNullOrWhiteSpace(directoryFiles))
|
||||||
directoryFiles = "manuals";
|
directoryFiles = "manuals";
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> SaveFileAsync(int? idCategory, int? idFolder, string name, Stream stream,
|
public async Task<int> SaveFileAsync(int idDirectory, int idAuthor, string name, Stream stream, CancellationToken cancellationToken)
|
||||||
CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
var extension = Path.GetExtension(name);
|
var extension = Path.GetExtension(name);
|
||||||
|
|
||||||
@ -53,74 +50,65 @@ public class ManualCatalogService : IManualCatalogService
|
|||||||
$"Невозможно загрузить файл с расширением '{extension}'. Допустимые форматы файлов: {string.Join(", ", validExtensions)}",
|
$"Невозможно загрузить файл с расширением '{extension}'. Допустимые форматы файлов: {string.Join(", ", validExtensions)}",
|
||||||
extension);
|
extension);
|
||||||
|
|
||||||
var path = await BuildFilePathAsync(idCategory, idFolder, name, cancellationToken);
|
var path = await BuildFilePathAsync(idDirectory, name, cancellationToken);
|
||||||
|
|
||||||
await fileStorageRepository.SaveFileAsync(path,
|
await fileStorageRepository.SaveFileAsync(path, stream, cancellationToken);
|
||||||
stream,
|
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
var manual = new ManualDto
|
var manual = new ManualDto
|
||||||
{
|
{
|
||||||
Name = name,
|
Name = name,
|
||||||
DateDownload = DateTime.UtcNow,
|
DateDownload = DateTime.UtcNow,
|
||||||
IdFolder = idFolder,
|
IdDirectory = idDirectory,
|
||||||
IdCategory = idCategory
|
IdCategory = IdFileCategory,
|
||||||
|
IdAuthor = idAuthor
|
||||||
};
|
};
|
||||||
|
|
||||||
return await manualRepository.InsertAsync(manual, cancellationToken);
|
return await manualRepository.InsertAsync(manual, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> AddFolderAsync(string name, int? idParent, int idCategory,
|
public async Task<int> AddDirectoryAsync(string name, int? idParent, CancellationToken cancellationToken)
|
||||||
CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
if (idParent.HasValue)
|
if (idParent.HasValue && !await manualDirectoryRepository.IsExistsAsync(idParent.Value, cancellationToken))
|
||||||
{
|
throw new ArgumentInvalidException("Родительской директории не существует", nameof(idParent));
|
||||||
var parent = await manualFolderRepository.GetOrDefaultAsync(idParent.Value, cancellationToken)
|
|
||||||
?? throw new ArgumentInvalidException("Родительской папки не существует", nameof(idParent));
|
|
||||||
|
|
||||||
if (parent.IdCategory != idCategory)
|
var directory = new ManualDirectoryDto
|
||||||
throw new ArgumentInvalidException("Категория родительской папки не соответствует текущей категории",
|
|
||||||
nameof(idCategory));
|
|
||||||
}
|
|
||||||
|
|
||||||
var manualFolder = new ManualFolderDto
|
|
||||||
{
|
{
|
||||||
Name = name,
|
Name = name,
|
||||||
IdParent = idParent,
|
IdParent = idParent,
|
||||||
IdCategory = idCategory,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (await IsExistFolderAsync(manualFolder, cancellationToken))
|
if (await IsExistDirectoryAsync(directory, cancellationToken))
|
||||||
throw new ArgumentInvalidException("Папка с таким названием уже существует", name);
|
throw new ArgumentInvalidException("Директория с таким названием уже существует", name);
|
||||||
|
|
||||||
return await manualFolderRepository.InsertAsync(manualFolder, cancellationToken);
|
return await manualDirectoryRepository.InsertAsync(directory, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateFolderAsync(int id, string name, CancellationToken cancellationToken)
|
public async Task UpdateDirectoryAsync(int id, string name, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var folder = await manualFolderRepository.GetOrDefaultAsync(id, cancellationToken)
|
var directory = await manualDirectoryRepository.GetOrDefaultAsync(id, cancellationToken)
|
||||||
?? throw new ArgumentInvalidException($"Папки с Id: {id} не сущесвует", nameof(id));
|
?? throw new ArgumentInvalidException($"Директории с Id: {id} не существует", nameof(id));
|
||||||
|
|
||||||
folder.Name = name;
|
directory.Name = name;
|
||||||
|
|
||||||
if (await IsExistFolderAsync(folder, cancellationToken))
|
if (await IsExistDirectoryAsync(directory, cancellationToken))
|
||||||
throw new ArgumentInvalidException("Папка с таким названием уже существует", name);
|
throw new ArgumentInvalidException("Директория с таким названием уже существует", name);
|
||||||
|
|
||||||
await manualFolderRepository.UpdateAsync(folder, cancellationToken);
|
await manualDirectoryRepository.UpdateAsync(directory, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteDirectoryAsync(int id, CancellationToken cancellationToken)
|
||||||
public async Task<int> DeleteFolderAsync(int id, CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
var folder = await manualFolderRepository.GetOrDefaultAsync(id, cancellationToken);
|
var directory = await manualDirectoryRepository.GetOrDefaultAsync(id, cancellationToken);
|
||||||
|
|
||||||
if (folder is null)
|
if (directory is null)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
var path = Path.Combine(directoryFiles, folder.IdCategory.ToString(), folder.Id.ToString());
|
var path = fileStorageRepository.MakeFilePath(directoryFiles, IdFileCategory.ToString(),
|
||||||
|
await BuildDirectoryPathAsync(id, cancellationToken));
|
||||||
|
|
||||||
fileStorageRepository.DeleteDirectory(path);
|
fileStorageRepository.DeleteDirectory(path);
|
||||||
|
|
||||||
return await manualFolderRepository.DeleteAsync(folder.Id, cancellationToken);
|
return await manualDirectoryRepository.DeleteAsync(directory.Id, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> DeleteFileAsync(int id, CancellationToken cancellationToken)
|
public async Task<int> DeleteFileAsync(int id, CancellationToken cancellationToken)
|
||||||
@ -130,9 +118,8 @@ public class ManualCatalogService : IManualCatalogService
|
|||||||
if (manual is null)
|
if (manual is null)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
var filePath = await BuildFilePathAsync(manual.IdCategory, manual.IdFolder, manual.Name,
|
var filePath = await BuildFilePathAsync(manual.IdDirectory, manual.Name, cancellationToken);
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
fileStorageRepository.DeleteFile(filePath);
|
fileStorageRepository.DeleteFile(filePath);
|
||||||
|
|
||||||
return await manualRepository.DeleteAsync(manual.Id, cancellationToken);
|
return await manualRepository.DeleteAsync(manual.Id, cancellationToken);
|
||||||
@ -145,58 +132,42 @@ public class ManualCatalogService : IManualCatalogService
|
|||||||
if (manual is null)
|
if (manual is null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var path = await BuildFilePathAsync(manual.IdCategory, manual.IdFolder, manual.Name, cancellationToken);
|
var path = await BuildFilePathAsync(manual.IdDirectory, manual.Name, cancellationToken);
|
||||||
|
var fileStream = new FileStream(path, FileMode.Open);
|
||||||
var fileStream = new FileStream(Path.GetFullPath(path), FileMode.Open);
|
|
||||||
|
|
||||||
return (fileStream, manual.Name);
|
return (fileStream, manual.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<CatalogItemManualDto>> GetCatalogAsync(CancellationToken cancellationToken)
|
private async Task<bool> IsExistDirectoryAsync(ManualDirectoryDto directory, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var catalogItems = new List<CatalogItemManualDto>();
|
var existingDirectory = await manualDirectoryRepository.GetOrDefaultAsync(directory.Name, directory.IdParent,
|
||||||
|
|
||||||
var categories = await fileCategoryRepository.GetAllAsync(FileCategory.IdFileCategoryTypeManuals,
|
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
|
|
||||||
foreach (var category in categories)
|
return existingDirectory is not null && directory.Id != existingDirectory.Id;
|
||||||
{
|
|
||||||
catalogItems.Add(new CatalogItemManualDto()
|
|
||||||
{
|
|
||||||
Category = category,
|
|
||||||
ManualsWithoutFolder = await manualRepository.GetManualsWithoutFolderAsync(category.Id, cancellationToken),
|
|
||||||
Folders = await manualFolderRepository.GetTreeAsync(category.Id, cancellationToken)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return catalogItems;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> IsExistFolderAsync(ManualFolderDto folder, CancellationToken cancellationToken)
|
private async Task<string> BuildDirectoryPathAsync(int idDirectory, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var existingFolder = await manualFolderRepository.GetOrDefaultAsync(folder.Name, folder.IdParent,
|
var directiories = await manualDirectoryRepository.GetAllAsync(cancellationToken);
|
||||||
folder.IdCategory,
|
|
||||||
cancellationToken);
|
|
||||||
|
|
||||||
return existingFolder is not null && folder.Id != existingFolder.Id;
|
var directory = directiories.FirstOrDefault(d => d.Id == idDirectory)
|
||||||
}
|
?? throw new ArgumentInvalidException($"Директории с Id: {idDirectory} не существует", nameof(idDirectory));
|
||||||
|
|
||||||
private async Task<string> BuildFilePathAsync(int? idCategory, int? idFolder, string name,
|
var pathSegments = new List<int> { directory.Id };
|
||||||
CancellationToken cancellationToken)
|
|
||||||
{
|
while (directory.IdParent.HasValue)
|
||||||
if (idFolder.HasValue)
|
|
||||||
{
|
{
|
||||||
var folder = await manualFolderRepository.GetOrDefaultAsync(idFolder.Value, cancellationToken)
|
directory = directiories.FirstOrDefault(d => d.Id == directory.IdParent.Value);
|
||||||
?? throw new ArgumentInvalidException($"Папки с Id: {idFolder} не сущесвует", nameof(idFolder));
|
|
||||||
|
|
||||||
return fileStorageRepository.MakeFilePath(directoryFiles, Path.Combine(folder.IdCategory.ToString(),
|
pathSegments.Insert(0, directory.Id);
|
||||||
folder.IdParent.ToString() ?? string.Empty,
|
|
||||||
folder.Id.ToString()), name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!idCategory.HasValue)
|
return string.Join("/", pathSegments);
|
||||||
throw new ArgumentInvalidException("Не указан идентификатор категории", nameof(idCategory));
|
}
|
||||||
|
|
||||||
return fileStorageRepository.MakeFilePath(directoryFiles, idCategory.Value.ToString(), name);
|
private async Task<string> BuildFilePathAsync(int idDirectory, string name, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var directoryPath = await BuildDirectoryPathAsync(idDirectory, cancellationToken);
|
||||||
|
|
||||||
|
return fileStorageRepository.MakeFilePath(directoryFiles, IdFileCategory.ToString(), Path.Combine(directoryPath, name));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user