From b471efc59e3ccedb1e8acea2dbcba4267d943ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Sep 2021 15:55:10 +0500 Subject: [PATCH] File author is optional (for drillingProgram) --- AsbCloudApp/Services/IFileService.cs | 2 +- AsbCloudDb/Model/FileInfo.cs | 2 +- .../Services/DrillingProgramService.cs | 9 +++++---- AsbCloudInfrastructure/Services/FileService.cs | 2 +- AsbCloudWebApi/Controllers/DrillingProgramController.cs | 2 ++ 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index 77f9d3ca..ad9c201b 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -11,7 +11,7 @@ namespace AsbCloudApp.Services { string RootPath { get; } - Task SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default); + Task SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default); Task> GetInfosAsync(int idWell, int idCategory, string companyName = default, string fileName = default, DateTime begin = default, DateTime end = default, diff --git a/AsbCloudDb/Model/FileInfo.cs b/AsbCloudDb/Model/FileInfo.cs index 4b1525c9..b939368b 100644 --- a/AsbCloudDb/Model/FileInfo.cs +++ b/AsbCloudDb/Model/FileInfo.cs @@ -17,7 +17,7 @@ namespace AsbCloudDb.Model public int IdWell { get; set; } [Column("id_author"), Comment("Id пользователя, загрузившего файл")] - public int IdAuthor { get; set; } + public int? IdAuthor { get; set; } [Column("id_category"), Comment("id категории файла")] public int IdCategory { get; set; } diff --git a/AsbCloudInfrastructure/Services/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgramService.cs index 82ad8f46..d5e9843b 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgramService.cs @@ -39,14 +39,14 @@ namespace AsbCloudInfrastructure.Services var matchFiles = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgram, token) .ConfigureAwait(false); - if (matchFiles is not null) { + if (matchFiles is not null && matchFiles.Any()) { matchFiles = matchFiles.OrderByDescending(f => f.UploadDate); var matchFilesIterator = matchFiles.GetEnumerator(); matchFilesIterator.MoveNext(); var matchFile = matchFilesIterator.Current; while (matchFilesIterator.MoveNext()) await fileService.DeletedAsync(matchFilesIterator.Current.Id, token) - .ConfigureAwait(false); + .ConfigureAwait(false); if (filesInfos.All(f => f.UploadDate <= matchFile.UploadDate)) return matchFile; @@ -61,8 +61,9 @@ namespace AsbCloudInfrastructure.Services var stream = new MemoryStream(1024 * 1024); UniteExcelFiles(fileNames, stream); - stream.Seek(0, SeekOrigin.Begin); - return await fileService.SaveAsync(idWell, 0, idFileCategoryDrillingProgramItems, resultFileName, stream, token) + var buffer = stream.ToArray(); + var fileStream = new MemoryStream(buffer); + return await fileService.SaveAsync(idWell, null, idFileCategoryDrillingProgram, resultFileName, fileStream, token) .ConfigureAwait(false); } diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index e758a828..615fcc08 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -29,7 +29,7 @@ namespace AsbCloudInfrastructure.Services .ThenInclude(c => c.CompanyType); } - public async Task SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default) + public async Task SaveAsync(int idWell, int? idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default) { //save info to db var fileInfo = new AsbCloudDb.Model.FileInfo() diff --git a/AsbCloudWebApi/Controllers/DrillingProgramController.cs b/AsbCloudWebApi/Controllers/DrillingProgramController.cs index aac3df82..42497429 100644 --- a/AsbCloudWebApi/Controllers/DrillingProgramController.cs +++ b/AsbCloudWebApi/Controllers/DrillingProgramController.cs @@ -27,6 +27,8 @@ namespace AsbCloudWebApi.Controllers { var fileInfo = await drillingProgramService.GetAsync(idWell, token) .ConfigureAwait(false); + if (fileInfo is null) + return NoContent(); var relativePath = fileService.GetFileName(fileInfo); return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name); }