#6539681 file service refactor

This commit is contained in:
ai.astrakhantsev 2022-10-06 14:37:03 +05:00
parent b6ce4cad77
commit fe49c90e58
15 changed files with 170 additions and 216 deletions

View File

@ -21,4 +21,8 @@
<ItemGroup>
<None Include="D:\Source\AsbCloudApp\Services\.editorconfig" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsbCloudDb\AsbCloudDb.csproj" />
</ItemGroup>
</Project>

View File

@ -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
{
/// <summary>
/// Сервис доступа к файлам

View File

@ -2,7 +2,7 @@
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Services
namespace AsbCloudApp.Repositories
{
/// <summary>
/// Репозиторий хранения фалов
@ -26,7 +26,7 @@ namespace AsbCloudApp.Services
/// </summary>
/// <param name="srcFilePath"></param>
/// <param name="filePath"></param>
void MoveFile (string srcFilePath, string filePath);
void MoveFile(string srcFilePath, string filePath);
/// <summary>
/// Копирование файла

View File

@ -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
/// <summary>
/// Сервис доступа к файлам
/// </summary>
public class FileService
{
private readonly IFileRepository fileRepository;
private readonly IFileStorageRepository fileStorageRepository;
/// <summary>
/// Сервис доступа к файлам
/// </summary>
/// <param name="fileRepository"></param>
/// <param name="fileStorageRepository"></param>
public FileService(IFileRepository fileRepository, IFileStorageRepository fileStorageRepository)
{
this.fileRepository = fileRepository;
this.fileStorageRepository = fileStorageRepository;
}
/// <summary>
/// переместить файл
/// </summary>
/// <param name="idWell"></param>
/// <param name="idUser"></param>
/// <param name="idCategory"></param>
/// <param name="destinationFileName"></param>
/// <param name="srcFilePath"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<FileInfoDto> 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);
}
/// <summary>
/// Сохранить файл
/// </summary>
/// <param name="idWell"></param>
/// <param name="idUser"></param>
/// <param name="idCategory"></param>
/// <param name="fileFullName"></param>
/// <param name="fileStream"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<FileInfoDto> 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)}");
}
/// <summary>
/// Инфо о файле
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<FileInfoDto> GetInfoAsync(int idFile,
CancellationToken token)
{
@ -92,9 +115,21 @@ namespace AsbCloudInfrastructure.Services
return dto;
}
/// <summary>
/// удалить файл
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
public Task<int> DeleteAsync(int idFile, CancellationToken token)
=> DeleteAsync(new int[] { idFile }, token);
/// <summary>
/// удалить файлы
/// </summary>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
{
if (ids is null || !ids.Any())
@ -114,6 +149,11 @@ namespace AsbCloudInfrastructure.Services
return files.Any() ? 1 : 0;
}
/// <summary>
/// получить путь для скачивания
/// </summary>
/// <param name="idFile"></param>
/// <returns></returns>
public async Task<string> 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));
}
/// <summary>
/// получить путь для скачивания
/// </summary>
/// <param name="fileInfo"></param>
/// <returns></returns>
public string GetUrl(FileInfoDto fileInfo) =>
GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
/// <summary>
/// получить путь для скачивания
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="idFile"></param>
/// <param name="dotExtention"></param>
/// <returns></returns>
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
Path.Combine(fileStorageRepository.RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
/// <summary>
/// пометить метку файла как удаленную
/// </summary>
/// <param name="idMark"></param>
/// <param name="token"></param>
/// <returns></returns>
public Task<int> MarkFileMarkAsDeletedAsync(int idMark,
CancellationToken token)
=> fileRepository.MarkFileMarkAsDeletedAsync(new int[] { idMark }, token);
/// <summary>
/// Инфо о файле
/// </summary>
/// <param name="idsFile"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
{
var result = await fileRepository.GetInfoByIdsAsync(idsFile, token).ConfigureAwait(false);
@ -147,38 +212,101 @@ namespace AsbCloudInfrastructure.Services
return result;
}
/// <summary>
/// Получить список файлов в контейнере
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="companyName"></param>
/// <param name="fileName"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<PaginationContainer<FileInfoDto>> 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);
/// <summary>
/// Пометить файл как удаленный
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default)
=> await fileRepository.MarkAsDeletedAsync(idFile, token)
.ConfigureAwait(false);
/// <summary>
/// добавить метку на файл
/// </summary>
/// <param name="fileMarkDto"></param>
/// <param name="idUser"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
=> await fileRepository.CreateFileMarkAsync(fileMarkDto, idUser, token)
.ConfigureAwait(false);
/// <summary>
/// Получить запись по id
/// </summary>
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<FileInfoDto> GetOrDefaultAsync(int id, CancellationToken token)
=> await fileRepository.GetOrDefaultAsync(id, token)
.ConfigureAwait(false);
/// <summary>
/// получить инфо о файле по метке
/// </summary>
/// <param name="idMark"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<FileInfoDto> GetByMarkId(int idMark, CancellationToken token)
=> await fileRepository.GetByMarkId(idMark, token)
.ConfigureAwait(false);
/// <summary>
/// получить инфо о файле по метке
/// </summary>
/// <param name="idMark"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token)
=> await fileRepository.MarkFileMarkAsDeletedAsync(idsMarks, token)
.ConfigureAwait(false);
/// <summary>
/// Получение файлов по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token)
=> await fileRepository.GetInfosByWellIdAsync(idWell, token)
.ConfigureAwait(false);
/// <summary>
/// Получить файлы определенной категории
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<IEnumerable<FileInfoDto>> 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)}");
}
}
}

View File

@ -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
/// <summary>
/// Сервис доступа к файлам
/// </summary>
public interface IFileService
{
/// <summary>
/// Сохранить файл
/// </summary>
/// <param name="idWell"></param>
/// <param name="idUser"></param>
/// <param name="idCategory"></param>
/// <param name="fileFullName"></param>
/// <param name="fileStream"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default);
/// <summary>
/// Инфо о файле
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> GetInfoAsync(int idFile,
CancellationToken token);
/// <summary>
/// удалить файл
/// </summary>
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteAsync(int id, CancellationToken token);
/// <summary>
/// удалить файлы
/// </summary>
/// <param name="ids"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token);
/// <summary>
/// получить путь для скачивания
/// </summary>
/// <param name="fileInfo"></param>
/// <returns></returns>
string GetUrl(FileInfoDto fileInfo);
/// <summary>
/// получить путь для скачивания
/// </summary>
/// <param name="idFile"></param>
/// <returns></returns>
Task<string> GetUrl(int idFile);
/// <summary>
/// получить путь для скачивания
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="idFile"></param>
/// <param name="dotExtention"></param>
/// <returns></returns>
string GetUrl(int idWell, int idCategory, int idFile, string dotExtention);
/// <summary>
/// пометить метку файла как удаленную
/// </summary>
/// <param name="idMark"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkFileMarkAsDeletedAsync(int idMark, CancellationToken token);
/// <summary>
/// переместить файл
/// </summary>
/// <param name="idWell"></param>
/// <param name="idUser"></param>
/// <param name="idCategory"></param>
/// <param name="destinationFileName"></param>
/// <param name="srcFileFullName"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory, string destinationFileName, string srcFileFullName, CancellationToken token = default);
/// <summary>
/// Инфо о файле
/// </summary>
/// <param name="idsFile"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token);
/// <summary>
/// Получить список файлов в контейнере
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="companyName"></param>
/// <param name="fileName"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<PaginationContainer<FileInfoDto>> 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);
/// <summary>
/// Пометить файл как удаленный
/// </summary>
/// <param name="idFile"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default);
/// <summary>
/// добавить метку на файл
/// </summary>
/// <param name="fileMarkDto"></param>
/// <param name="idUser"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token);
/// <summary>
/// Получить запись по id
/// </summary>
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> GetOrDefaultAsync(int id, CancellationToken token);
/// <summary>
/// получить инфо о файле по метке
/// </summary>
/// <param name="idMark"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<FileInfoDto> GetByMarkId(int idMark, CancellationToken token);
/// <summary>
/// пометить метки файлов как удаленные
/// </summary>
/// <param name="idsMarks"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token);
/// <summary>
/// Получение файлов по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token);
/// <summary>
/// Получить файлы определенной категории
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token);
}
}

View File

@ -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<IDrillingProgramService, DrillingProgramService>();
services.AddTransient<IDrillParamsService, DrillParamsService>();
services.AddTransient<IEventService, EventService>();
services.AddTransient<IFileService, FileService>();
services.AddTransient<FileService>();
services.AddTransient<IMeasureService, MeasureService>();
services.AddTransient<IMessageService, MessageService>();
services.AddTransient<IOperationsStatService, OperationsStatService>();

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudApp.Repositories;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using AsbCloudApp.Repositories;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

View File

@ -20,7 +20,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
private static readonly Dictionary<string, DrillingProgramCreateError> drillingProgramCreateErrors = new Dictionary<string, DrillingProgramCreateError>();
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,

View File

@ -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,

View File

@ -78,7 +78,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
new RelationCompanyWell { IdCompany = 3003, IdWell = 3002, },
};
private readonly Mock<IFileService> fileServiceMock;
private readonly Mock<FileService> fileServiceMock;
private readonly Mock<IUserService> userServiceMock;
private readonly Mock<IWellService> wellServiceMock;
private readonly Mock<IConfiguration> configurationMock;
@ -97,7 +97,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
db.RelationCompaniesWells.AddRange(relationsCompanyWell);
db.SaveChanges();
fileServiceMock = new Mock<IFileService>();
fileServiceMock = new Mock<FileService>();
userServiceMock = new Mock<IUserService>();
wellServiceMock = new Mock<IWellService>();
configurationMock = new Mock<IConfiguration>();

View File

@ -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<byte>());
var data = await fileService.SaveAsync(90, 1, 10040, "test.txt", stream, CancellationToken.None);
Assert.NotNull(data);
}

View File

@ -17,7 +17,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
{
private readonly AsbCloudDbContext context;
private WellFinalDocumentsService service;
private readonly Mock<IFileService> fileServiceMock;
private readonly Mock<FileService> fileServiceMock;
private readonly Mock<IUserService> userServiceMock;
private readonly Mock<IWellService> wellServiceMock;
private readonly Mock<IConfiguration> configurationMock;
@ -44,7 +44,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
context = TestHelpter.MakeTestContext();
context.SaveChanges();
fileServiceMock = new Mock<IFileService>();
fileServiceMock = new Mock<FileService>();
userServiceMock = new Mock<IUserService>();
userServiceMock.Setup(x => x.GetAllAsync(CancellationToken.None)).Returns(Task.Run(() => users.Select(x => (UserExtendedDto)x)));

View File

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

View File

@ -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<ReportsHub> reportsHubContext;
public ReportController(IReportService reportService, IWellService wellService,
IFileService fileService, IHubContext<ReportsHub> reportsHubContext)
FileService fileService, IHubContext<ReportsHub> reportsHubContext)
{
this.reportService = reportService;
this.fileService = fileService;