From fe49c90e58d064e06a53aafdd206b40fbba4fc68 Mon Sep 17 00:00:00 2001 From: "ai.astrakhantsev" Date: Thu, 6 Oct 2022 14:37:03 +0500 Subject: [PATCH] #6539681 file service refactor --- AsbCloudApp/AsbCloudApp.csproj | 4 + .../IFileRepository.cs | 3 +- .../IFileStorageRepository.cs | 4 +- .../Services/FileService.cs | 156 +++++++++++++-- AsbCloudApp/Services/IFileService.cs | 181 ------------------ AsbCloudInfrastructure/DependencyInjection.cs | 3 +- .../Repository/FileRepository.cs | 2 +- .../Repository/FileStorageRepository.cs | 2 +- .../DrillingProgram/DrillingProgramService.cs | 4 +- .../Services/WellFinalDocumentsService.cs | 4 +- .../DrillingProgramServiceTest.cs | 4 +- .../ServicesTests/FileServiceTest.cs | 7 +- .../WellFinalDocumentsServiceTest.cs | 4 +- AsbCloudWebApi/Controllers/FileController.cs | 4 +- .../Controllers/ReportController.cs | 4 +- 15 files changed, 170 insertions(+), 216 deletions(-) rename AsbCloudApp/{Services => Repositories}/IFileRepository.cs (98%) rename AsbCloudApp/{Services => Repositories}/IFileStorageRepository.cs (94%) rename {AsbCloudInfrastructure => AsbCloudApp}/Services/FileService.cs (59%) delete mode 100644 AsbCloudApp/Services/IFileService.cs diff --git a/AsbCloudApp/AsbCloudApp.csproj b/AsbCloudApp/AsbCloudApp.csproj index 5c826cb1..7a10181c 100644 --- a/AsbCloudApp/AsbCloudApp.csproj +++ b/AsbCloudApp/AsbCloudApp.csproj @@ -21,4 +21,8 @@ + + + + diff --git a/AsbCloudApp/Services/IFileRepository.cs b/AsbCloudApp/Repositories/IFileRepository.cs similarity index 98% rename from AsbCloudApp/Services/IFileRepository.cs rename to AsbCloudApp/Repositories/IFileRepository.cs index e01891cc..e36d1001 100644 --- a/AsbCloudApp/Services/IFileRepository.cs +++ b/AsbCloudApp/Repositories/IFileRepository.cs @@ -1,10 +1,11 @@ using AsbCloudApp.Data; +using AsbCloudApp.Services; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudApp.Services +namespace AsbCloudApp.Repositories { /// /// Сервис доступа к файлам diff --git a/AsbCloudApp/Services/IFileStorageRepository.cs b/AsbCloudApp/Repositories/IFileStorageRepository.cs similarity index 94% rename from AsbCloudApp/Services/IFileStorageRepository.cs rename to AsbCloudApp/Repositories/IFileStorageRepository.cs index 6659e976..9d821861 100644 --- a/AsbCloudApp/Services/IFileStorageRepository.cs +++ b/AsbCloudApp/Repositories/IFileStorageRepository.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; -namespace AsbCloudApp.Services +namespace AsbCloudApp.Repositories { /// /// Репозиторий хранения фалов @@ -26,7 +26,7 @@ namespace AsbCloudApp.Services /// /// /// - void MoveFile (string srcFilePath, string filePath); + void MoveFile(string srcFilePath, string filePath); /// /// Копирование файла diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudApp/Services/FileService.cs similarity index 59% rename from AsbCloudInfrastructure/Services/FileService.cs rename to AsbCloudApp/Services/FileService.cs index 5cb60fcd..b27c4383 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudApp/Services/FileService.cs @@ -1,10 +1,5 @@ using AsbCloudApp.Data; -using AsbCloudApp.Exceptions; -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using AsbCloudInfrastructure.Repository; -using Mapster; -using Microsoft.EntityFrameworkCore; +using AsbCloudApp.Repositories; using System; using System.Collections.Generic; using System.IO; @@ -12,19 +7,37 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Services +namespace AsbCloudApp.Services { - public class FileService : IFileService + /// + /// Сервис доступа к файлам + /// + public class FileService { private readonly IFileRepository fileRepository; private readonly IFileStorageRepository fileStorageRepository; + /// + /// Сервис доступа к файлам + /// + /// + /// public FileService(IFileRepository fileRepository, IFileStorageRepository fileStorageRepository) { this.fileRepository = fileRepository; this.fileStorageRepository = fileStorageRepository; } + /// + /// переместить файл + /// + /// + /// + /// + /// + /// + /// + /// public async Task MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFilePath, CancellationToken token = default) { @@ -49,6 +62,16 @@ namespace AsbCloudInfrastructure.Services return await GetInfoAsync(fileId, token); } + /// + /// Сохранить файл + /// + /// + /// + /// + /// + /// + /// + /// public async Task SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token) { @@ -72,12 +95,12 @@ namespace AsbCloudInfrastructure.Services return await GetInfoAsync(fileId, token); } - private string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId) - { - return Path.Combine(fileStorageRepository.RootPath, $"{idWell}", - $"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}"); - } - + /// + /// Инфо о файле + /// + /// + /// + /// public async Task GetInfoAsync(int idFile, CancellationToken token) { @@ -92,9 +115,21 @@ namespace AsbCloudInfrastructure.Services return dto; } + /// + /// удалить файл + /// + /// + /// + /// public Task DeleteAsync(int idFile, CancellationToken token) => DeleteAsync(new int[] { idFile }, token); + /// + /// удалить файлы + /// + /// + /// + /// public async Task DeleteAsync(IEnumerable ids, CancellationToken token) { if (ids is null || !ids.Any()) @@ -114,6 +149,11 @@ namespace AsbCloudInfrastructure.Services return files.Any() ? 1 : 0; } + /// + /// получить путь для скачивания + /// + /// + /// public async Task GetUrl(int idFile) { var fileInfo = await fileRepository.GetOrDefaultAsync(idFile, CancellationToken.None).ConfigureAwait(false); @@ -121,16 +161,41 @@ namespace AsbCloudInfrastructure.Services return GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name)); } + /// + /// получить путь для скачивания + /// + /// + /// public string GetUrl(FileInfoDto fileInfo) => GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name)); + /// + /// получить путь для скачивания + /// + /// + /// + /// + /// + /// public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) => Path.Combine(fileStorageRepository.RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}"); + /// + /// пометить метку файла как удаленную + /// + /// + /// + /// public Task MarkFileMarkAsDeletedAsync(int idMark, CancellationToken token) => fileRepository.MarkFileMarkAsDeletedAsync(new int[] { idMark }, token); + /// + /// Инфо о файле + /// + /// + /// + /// public async Task> GetInfoByIdsAsync(IEnumerable idsFile, CancellationToken token) { var result = await fileRepository.GetInfoByIdsAsync(idsFile, token).ConfigureAwait(false); @@ -147,38 +212,101 @@ namespace AsbCloudInfrastructure.Services return result; } + /// + /// Получить список файлов в контейнере + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public async Task> GetInfosAsync(int idWell, int idCategory, string companyName = default, string fileName = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default) => await fileRepository.GetInfosAsync(idWell, idCategory, companyName, fileName, begin, end, skip, take, token) .ConfigureAwait(false); + /// + /// Пометить файл как удаленный + /// + /// + /// + /// public async Task MarkAsDeletedAsync(int idFile, CancellationToken token = default) => await fileRepository.MarkAsDeletedAsync(idFile, token) .ConfigureAwait(false); + /// + /// добавить метку на файл + /// + /// + /// + /// + /// public async Task CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token) => await fileRepository.CreateFileMarkAsync(fileMarkDto, idUser, token) .ConfigureAwait(false); + /// + /// Получить запись по id + /// + /// + /// + /// public async Task GetOrDefaultAsync(int id, CancellationToken token) => await fileRepository.GetOrDefaultAsync(id, token) .ConfigureAwait(false); + /// + /// получить инфо о файле по метке + /// + /// + /// + /// public async Task GetByMarkId(int idMark, CancellationToken token) => await fileRepository.GetByMarkId(idMark, token) .ConfigureAwait(false); + /// + /// получить инфо о файле по метке + /// + /// + /// + /// public async Task MarkFileMarkAsDeletedAsync(IEnumerable idsMarks, CancellationToken token) => await fileRepository.MarkFileMarkAsDeletedAsync(idsMarks, token) .ConfigureAwait(false); + /// + /// Получение файлов по скважине + /// + /// + /// + /// public async Task> GetInfosByWellIdAsync(int idWell, CancellationToken token) => await fileRepository.GetInfosByWellIdAsync(idWell, token) .ConfigureAwait(false); + /// + /// Получить файлы определенной категории + /// + /// + /// + /// + /// public async Task> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token) => await fileRepository.GetInfosByCategoryAsync(idWell, idCategory, token) .ConfigureAwait(false); + + private string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId) + { + return Path.Combine(fileStorageRepository.RootPath, $"{idWell}", + $"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}"); + } } } diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs deleted file mode 100644 index a87397f7..00000000 --- a/AsbCloudApp/Services/IFileService.cs +++ /dev/null @@ -1,181 +0,0 @@ -using AsbCloudApp.Data; -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudApp.Services -{ - //TODO: refactor IFileService - - /// - /// Сервис доступа к файлам - /// - public interface IFileService - { - /// - /// Сохранить файл - /// - /// - /// - /// - /// - /// - /// - /// - Task SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default); - - /// - /// Инфо о файле - /// - /// - /// - /// - Task GetInfoAsync(int idFile, - CancellationToken token); - - /// - /// удалить файл - /// - /// - /// - /// - Task DeleteAsync(int id, CancellationToken token); - - /// - /// удалить файлы - /// - /// - /// - /// - Task DeleteAsync(IEnumerable ids, CancellationToken token); - - /// - /// получить путь для скачивания - /// - /// - /// - string GetUrl(FileInfoDto fileInfo); - - /// - /// получить путь для скачивания - /// - /// - /// - Task GetUrl(int idFile); - - /// - /// получить путь для скачивания - /// - /// - /// - /// - /// - /// - string GetUrl(int idWell, int idCategory, int idFile, string dotExtention); - - /// - /// пометить метку файла как удаленную - /// - /// - /// - /// - Task MarkFileMarkAsDeletedAsync(int idMark, CancellationToken token); - - /// - /// переместить файл - /// - /// - /// - /// - /// - /// - /// - /// - Task MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFileFullName, CancellationToken token = default); - - /// - /// Инфо о файле - /// - /// - /// - /// - Task> GetInfoByIdsAsync(IEnumerable idsFile, CancellationToken token); - - /// - /// Получить список файлов в контейнере - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - Task> GetInfosAsync(int idWell, - int idCategory, string companyName = default, string fileName = default, DateTime begin = default, - DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default); - - /// - /// Пометить файл как удаленный - /// - /// - /// - /// - Task MarkAsDeletedAsync(int idFile, CancellationToken token = default); - - /// - /// добавить метку на файл - /// - /// - /// - /// - /// - Task CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token); - - /// - /// Получить запись по id - /// - /// - /// - /// - Task GetOrDefaultAsync(int id, CancellationToken token); - - /// - /// получить инфо о файле по метке - /// - /// - /// - /// - Task GetByMarkId(int idMark, CancellationToken token); - - /// - /// пометить метки файлов как удаленные - /// - /// - /// - /// - Task MarkFileMarkAsDeletedAsync(IEnumerable idsMarks, CancellationToken token); - - /// - /// Получение файлов по скважине - /// - /// - /// - /// - Task> GetInfosByWellIdAsync(int idWell, CancellationToken token); - - /// - /// Получить файлы определенной категории - /// - /// - /// - /// - /// - Task> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token); - } -} diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 5c1bf56f..ab3ba9e2 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Data.Subsystems; +using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudApp.Services.Subsystems; using AsbCloudDb.Model; @@ -105,7 +106,7 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Repository/FileRepository.cs b/AsbCloudInfrastructure/Repository/FileRepository.cs index 2726c2b7..0cca5b8b 100644 --- a/AsbCloudInfrastructure/Repository/FileRepository.cs +++ b/AsbCloudInfrastructure/Repository/FileRepository.cs @@ -1,5 +1,5 @@ using AsbCloudApp.Data; -using AsbCloudApp.Services; +using AsbCloudApp.Repositories; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; diff --git a/AsbCloudInfrastructure/Repository/FileStorageRepository.cs b/AsbCloudInfrastructure/Repository/FileStorageRepository.cs index 6cb38cfb..7466f3a3 100644 --- a/AsbCloudInfrastructure/Repository/FileStorageRepository.cs +++ b/AsbCloudInfrastructure/Repository/FileStorageRepository.cs @@ -1,5 +1,5 @@ using AsbCloudApp.Exceptions; -using AsbCloudApp.Services; +using AsbCloudApp.Repositories; using System.IO; using System.Threading; using System.Threading.Tasks; diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs index 2780e7b1..ca0827b0 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs @@ -20,7 +20,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private static readonly Dictionary drillingProgramCreateErrors = new Dictionary(); private readonly IAsbCloudDbContext context; - private readonly IFileService fileService; + private readonly FileService fileService; private readonly IUserService userService; private readonly IWellService wellService; private readonly IConfiguration configuration; @@ -50,7 +50,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram public DrillingProgramService( IAsbCloudDbContext context, - IFileService fileService, + FileService fileService, IUserService userService, IWellService wellService, IConfiguration configuration, diff --git a/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs b/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs index 4d5f6427..62d82522 100644 --- a/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs +++ b/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs @@ -22,7 +22,7 @@ namespace AsbCloudInfrastructure.Services public class WellFinalDocumentsService : IWellFinalDocumentsService { private readonly IAsbCloudDbContext context; - private readonly IFileService fileService; + private readonly FileService fileService; private readonly IUserService userService; private readonly IWellService wellService; private readonly IConfiguration configuration; @@ -32,7 +32,7 @@ namespace AsbCloudInfrastructure.Services private const int FileServiceThrewException = -1; public WellFinalDocumentsService(IAsbCloudDbContext context, - IFileService fileService, + FileService fileService, IUserService userService, IWellService wellService, IConfiguration configuration, diff --git a/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs index 949c8c46..fa72df35 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/DrillingProgramServiceTest.cs @@ -78,7 +78,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests new RelationCompanyWell { IdCompany = 3003, IdWell = 3002, }, }; - private readonly Mock fileServiceMock; + private readonly Mock fileServiceMock; private readonly Mock userServiceMock; private readonly Mock wellServiceMock; private readonly Mock configurationMock; @@ -97,7 +97,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests db.RelationCompaniesWells.AddRange(relationsCompanyWell); db.SaveChanges(); - fileServiceMock = new Mock(); + fileServiceMock = new Mock(); userServiceMock = new Mock(); wellServiceMock = new Mock(); configurationMock = new Mock(); diff --git a/AsbCloudWebApi.Tests/ServicesTests/FileServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/FileServiceTest.cs index 828f19ae..025cc7bb 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/FileServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/FileServiceTest.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Repository; @@ -20,7 +21,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests { public class FileServiceTest { - private IFileService fileService; + private FileService fileService; private static UserDto Author = new UserDto { Id = 1, @@ -166,9 +167,9 @@ namespace AsbCloudWebApi.Tests.ServicesTests } [Fact] - public async Task SaveAsync_returns_FileInfoa() + public async Task SaveAsync_returns_FileInfo() { - using var stream = new MemoryStream(); + using var stream = new MemoryStream(Array.Empty()); var data = await fileService.SaveAsync(90, 1, 10040, "test.txt", stream, CancellationToken.None); Assert.NotNull(data); } diff --git a/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs index ac53a5b9..1718b92d 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/WellFinalDocumentsServiceTest.cs @@ -17,7 +17,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests { private readonly AsbCloudDbContext context; private WellFinalDocumentsService service; - private readonly Mock fileServiceMock; + private readonly Mock fileServiceMock; private readonly Mock userServiceMock; private readonly Mock wellServiceMock; private readonly Mock configurationMock; @@ -44,7 +44,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests context = TestHelpter.MakeTestContext(); context.SaveChanges(); - fileServiceMock = new Mock(); + fileServiceMock = new Mock(); userServiceMock = new Mock(); userServiceMock.Setup(x => x.GetAllAsync(CancellationToken.None)).Returns(Task.Run(() => users.Select(x => (UserExtendedDto)x))); diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index a7711247..378b5a50 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -20,10 +20,10 @@ namespace AsbCloudWebApi.Controllers [Authorize] public class FileController : ControllerBase { - private readonly IFileService fileService; + private readonly FileService fileService; private readonly IWellService wellService; - public FileController(IFileService fileService, IWellService wellService) + public FileController(FileService fileService, IWellService wellService) { this.fileService = fileService; this.wellService = wellService; diff --git a/AsbCloudWebApi/Controllers/ReportController.cs b/AsbCloudWebApi/Controllers/ReportController.cs index 03897582..d3df1092 100644 --- a/AsbCloudWebApi/Controllers/ReportController.cs +++ b/AsbCloudWebApi/Controllers/ReportController.cs @@ -18,12 +18,12 @@ namespace AsbCloudWebApi.Controllers public class ReportController : ControllerBase { private readonly IReportService reportService; - private readonly IFileService fileService; + private readonly FileService fileService; private readonly IWellService wellService; private readonly IHubContext reportsHubContext; public ReportController(IReportService reportService, IWellService wellService, - IFileService fileService, IHubContext reportsHubContext) + FileService fileService, IHubContext reportsHubContext) { this.reportService = reportService; this.fileService = fileService;