From eaf0885675c7f7bd2209e197a7ad56de74c99fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Fri, 8 Sep 2023 10:38:51 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20=D0=BA?= =?UTF-8?q?=D0=B0=D1=82=D0=B0=D0=BB=D0=BE=D0=B3=D0=B0=20=D0=B8=D0=BD=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IManualCatalogService.cs | 30 ++-- .../Services/ManualCatalogService.cs | 145 +++++++----------- 2 files changed, 67 insertions(+), 108 deletions(-) diff --git a/AsbCloudApp/Services/IManualCatalogService.cs b/AsbCloudApp/Services/IManualCatalogService.cs index 598de63e..a38bd49b 100644 --- a/AsbCloudApp/Services/IManualCatalogService.cs +++ b/AsbCloudApp/Services/IManualCatalogService.cs @@ -1,8 +1,6 @@ -using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Data.Manuals; namespace AsbCloudApp.Services; @@ -14,42 +12,39 @@ public interface IManualCatalogService /// /// Сохранение файла /// - /// - /// + /// + /// /// /// /// /// - Task SaveFileAsync(int? idCategory, int? idFolder, string name, Stream stream, - CancellationToken cancellationToken); + Task SaveFileAsync(int idDirectory, int idAuthor, string name, Stream stream, CancellationToken cancellationToken); /// - /// Добавление новой папки + /// Добавление директории /// /// /// - /// /// /// - Task AddFolderAsync(string name, int? idParent, int idCategory, - CancellationToken cancellationToken); + Task AddDirectoryAsync(string name, int? idParent, CancellationToken cancellationToken); /// - /// Обновление папки + /// Обновление директории /// /// /// /// /// - Task UpdateFolderAsync(int id, string name, CancellationToken cancellationToken); + Task UpdateDirectoryAsync(int id, string name, CancellationToken cancellationToken); /// - /// Удаление папки + /// Удаление директории /// /// /// /// - Task DeleteFolderAsync(int id, CancellationToken cancellationToken); + Task DeleteDirectoryAsync(int id, CancellationToken cancellationToken); /// /// Удаление файла @@ -66,11 +61,4 @@ public interface IManualCatalogService /// /// Task<(Stream stream, string fileName)?> GetFileAsync(int id, CancellationToken cancellationToken); - - /// - /// Получение каталога - /// - /// - /// - Task> GetCatalogAsync(CancellationToken cancellationToken); } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/ManualCatalogService.cs b/AsbCloudInfrastructure/Services/ManualCatalogService.cs index 545a114e..66d989f6 100644 --- a/AsbCloudInfrastructure/Services/ManualCatalogService.cs +++ b/AsbCloudInfrastructure/Services/ManualCatalogService.cs @@ -8,13 +8,14 @@ using AsbCloudApp.Data.Manuals; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Services; -using AsbCloudDb.Model; using Microsoft.Extensions.Configuration; namespace AsbCloudInfrastructure.Services; public class ManualCatalogService : IManualCatalogService { + private const int IdFileCategory = 30000; + private readonly IEnumerable validExtensions = new[] { ".pdf", @@ -23,28 +24,24 @@ public class ManualCatalogService : IManualCatalogService private readonly string directoryFiles; private readonly IFileStorageRepository fileStorageRepository; - private readonly IManualFolderRepository manualFolderRepository; - private readonly IManualRepository manualRepository; - private readonly IFileCategoryRepository fileCategoryRepository; + private readonly IManualDirectoryRepository manualDirectoryRepository; + private readonly ICrudRepository manualRepository; public ManualCatalogService(IFileStorageRepository fileStorageRepository, - IManualFolderRepository manualFolderRepository, - IManualRepository manualRepository, - IFileCategoryRepository fileCategoryRepository, + IManualDirectoryRepository manualDirectoryRepository, + ICrudRepository manualRepository, IConfiguration configuration) { this.fileStorageRepository = fileStorageRepository; - this.manualFolderRepository = manualFolderRepository; + this.manualDirectoryRepository = manualDirectoryRepository; this.manualRepository = manualRepository; - this.fileCategoryRepository = fileCategoryRepository; directoryFiles = configuration.GetValue("DirectoryManualFiles"); if (string.IsNullOrWhiteSpace(directoryFiles)) directoryFiles = "manuals"; } - public async Task SaveFileAsync(int? idCategory, int? idFolder, string name, Stream stream, - CancellationToken cancellationToken) + public async Task SaveFileAsync(int idDirectory, int idAuthor, string name, Stream stream, CancellationToken cancellationToken) { var extension = Path.GetExtension(name); @@ -53,74 +50,65 @@ public class ManualCatalogService : IManualCatalogService $"Невозможно загрузить файл с расширением '{extension}'. Допустимые форматы файлов: {string.Join(", ", validExtensions)}", extension); - var path = await BuildFilePathAsync(idCategory, idFolder, name, cancellationToken); + var path = await BuildFilePathAsync(idDirectory, name, cancellationToken); - await fileStorageRepository.SaveFileAsync(path, - stream, - cancellationToken); + await fileStorageRepository.SaveFileAsync(path, stream, cancellationToken); var manual = new ManualDto { Name = name, DateDownload = DateTime.UtcNow, - IdFolder = idFolder, - IdCategory = idCategory + IdDirectory = idDirectory, + IdCategory = IdFileCategory, + IdAuthor = idAuthor }; return await manualRepository.InsertAsync(manual, cancellationToken); } - public async Task AddFolderAsync(string name, int? idParent, int idCategory, - CancellationToken cancellationToken) + public async Task AddDirectoryAsync(string name, int? idParent, CancellationToken cancellationToken) { - if (idParent.HasValue) - { - var parent = await manualFolderRepository.GetOrDefaultAsync(idParent.Value, cancellationToken) - ?? throw new ArgumentInvalidException("Родительской папки не существует", nameof(idParent)); + if (idParent.HasValue && !await manualDirectoryRepository.IsExistsAsync(idParent.Value, cancellationToken)) + throw new ArgumentInvalidException("Родительской директории не существует", nameof(idParent)); - if (parent.IdCategory != idCategory) - throw new ArgumentInvalidException("Категория родительской папки не соответствует текущей категории", - nameof(idCategory)); - } - - var manualFolder = new ManualFolderDto + var directory = new ManualDirectoryDto { Name = name, IdParent = idParent, - IdCategory = idCategory, }; - if (await IsExistFolderAsync(manualFolder, cancellationToken)) - throw new ArgumentInvalidException("Папка с таким названием уже существует", name); - - return await manualFolderRepository.InsertAsync(manualFolder, cancellationToken); + if (await IsExistDirectoryAsync(directory, cancellationToken)) + throw new ArgumentInvalidException("Директория с таким названием уже существует", name); + + 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) - ?? throw new ArgumentInvalidException($"Папки с Id: {id} не сущесвует", nameof(id)); + var directory = await manualDirectoryRepository.GetOrDefaultAsync(id, cancellationToken) + ?? throw new ArgumentInvalidException($"Директории с Id: {id} не существует", nameof(id)); - folder.Name = name; + directory.Name = name; - if (await IsExistFolderAsync(folder, cancellationToken)) - throw new ArgumentInvalidException("Папка с таким названием уже существует", name); + if (await IsExistDirectoryAsync(directory, cancellationToken)) + throw new ArgumentInvalidException("Директория с таким названием уже существует", name); - await manualFolderRepository.UpdateAsync(folder, cancellationToken); + await manualDirectoryRepository.UpdateAsync(directory, cancellationToken); } - - public async Task DeleteFolderAsync(int id, CancellationToken cancellationToken) + public async Task DeleteDirectoryAsync(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; - 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); - return await manualFolderRepository.DeleteAsync(folder.Id, cancellationToken); + return await manualDirectoryRepository.DeleteAsync(directory.Id, cancellationToken); } public async Task DeleteFileAsync(int id, CancellationToken cancellationToken) @@ -130,9 +118,8 @@ public class ManualCatalogService : IManualCatalogService if (manual is null) return 0; - var filePath = await BuildFilePathAsync(manual.IdCategory, manual.IdFolder, manual.Name, - cancellationToken); - + var filePath = await BuildFilePathAsync(manual.IdDirectory, manual.Name, cancellationToken); + fileStorageRepository.DeleteFile(filePath); return await manualRepository.DeleteAsync(manual.Id, cancellationToken); @@ -145,58 +132,42 @@ public class ManualCatalogService : IManualCatalogService if (manual is null) return null; - var path = await BuildFilePathAsync(manual.IdCategory, manual.IdFolder, manual.Name, cancellationToken); - - var fileStream = new FileStream(Path.GetFullPath(path), FileMode.Open); - + var path = await BuildFilePathAsync(manual.IdDirectory, manual.Name, cancellationToken); + var fileStream = new FileStream(path, FileMode.Open); return (fileStream, manual.Name); } - public async Task> GetCatalogAsync(CancellationToken cancellationToken) + private async Task IsExistDirectoryAsync(ManualDirectoryDto directory, CancellationToken cancellationToken) { - var catalogItems = new List(); - - var categories = await fileCategoryRepository.GetAllAsync(FileCategory.IdFileCategoryTypeManuals, + var existingDirectory = await manualDirectoryRepository.GetOrDefaultAsync(directory.Name, directory.IdParent, cancellationToken); - foreach (var category in categories) - { - catalogItems.Add(new CatalogItemManualDto() - { - Category = category, - ManualsWithoutFolder = await manualRepository.GetManualsWithoutFolderAsync(category.Id, cancellationToken), - Folders = await manualFolderRepository.GetTreeAsync(category.Id, cancellationToken) - }); - } - - return catalogItems; + return existingDirectory is not null && directory.Id != existingDirectory.Id; } - private async Task IsExistFolderAsync(ManualFolderDto folder, CancellationToken cancellationToken) + private async Task BuildDirectoryPathAsync(int idDirectory, CancellationToken cancellationToken) { - var existingFolder = await manualFolderRepository.GetOrDefaultAsync(folder.Name, folder.IdParent, - folder.IdCategory, - cancellationToken); + var directiories = await manualDirectoryRepository.GetAllAsync(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 BuildFilePathAsync(int? idCategory, int? idFolder, string name, - CancellationToken cancellationToken) - { - if (idFolder.HasValue) + var pathSegments = new List { directory.Id }; + + while (directory.IdParent.HasValue) { - var folder = await manualFolderRepository.GetOrDefaultAsync(idFolder.Value, cancellationToken) - ?? throw new ArgumentInvalidException($"Папки с Id: {idFolder} не сущесвует", nameof(idFolder)); + directory = directiories.FirstOrDefault(d => d.Id == directory.IdParent.Value); - return fileStorageRepository.MakeFilePath(directoryFiles, Path.Combine(folder.IdCategory.ToString(), - folder.IdParent.ToString() ?? string.Empty, - folder.Id.ToString()), name); + pathSegments.Insert(0, directory.Id); } - if (!idCategory.HasValue) - throw new ArgumentInvalidException("Не указан идентификатор категории", nameof(idCategory)); + return string.Join("/", pathSegments); + } - return fileStorageRepository.MakeFilePath(directoryFiles, idCategory.Value.ToString(), name); + private async Task BuildFilePathAsync(int idDirectory, string name, CancellationToken cancellationToken) + { + var directoryPath = await BuildDirectoryPathAsync(idDirectory, cancellationToken); + + return fileStorageRepository.MakeFilePath(directoryFiles, IdFileCategory.ToString(), Path.Combine(directoryPath, name)); } } \ No newline at end of file