From 96a8c3fbd38c421ce09a9fe3234cb66deff15855 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: Wed, 31 Jan 2024 17:24:00 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Interfaces/IHasExcelParser.cs | 11 +++++ .../Interfaces/IHasExcelParserWithOptions.cs | 10 ++++ .../TrajectoryEditableController.cs | 30 ++++++++---- .../TrajectoryFactManualController.cs | 8 +--- .../Trajectory/TrajectoryPlanController.cs | 10 +--- AsbCloudWebApi/Extentions.cs | 46 ++++++++++--------- 6 files changed, 70 insertions(+), 45 deletions(-) create mode 100644 AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs create mode 100644 AsbCloudWebApi/Controllers/Interfaces/IHasExcelParserWithOptions.cs diff --git a/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs b/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs new file mode 100644 index 00000000..f78d17da --- /dev/null +++ b/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs @@ -0,0 +1,11 @@ +using System.IO; +using AsbCloudApp.Data; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers.Interfaces; + +public interface IHasExcelParser + where TDto : class, IId +{ + ActionResult> Parse(Stream file); +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParserWithOptions.cs b/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParserWithOptions.cs new file mode 100644 index 00000000..96e95eef --- /dev/null +++ b/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParserWithOptions.cs @@ -0,0 +1,10 @@ +using System.IO; +using AsbCloudApp.Data; + +namespace AsbCloudWebApi.Controllers.Interfaces; + +public interface IHasExcelParserWithOptions + where TDto : class, IId +{ + ParserResultDto Parse(Stream file, TOptions options); +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs index acd7a7d2..19f834e0 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs @@ -6,11 +6,12 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; +using System.IO; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; -using AsbCloudApp.Requests.Import; using AsbCloudInfrastructure.Services; +using AsbCloudWebApi.Controllers.Interfaces; namespace AsbCloudWebApi.Controllers.Trajectory { @@ -20,14 +21,15 @@ namespace AsbCloudWebApi.Controllers.Trajectory /// [ApiController] [Authorize] - public abstract class TrajectoryEditableController : TrajectoryController + public abstract class TrajectoryEditableController : TrajectoryController, + IHasExcelParser where TDto : TrajectoryGeoDto { private readonly ParserServiceFactory parserServiceFactory; private readonly TrajectoryExportService trajectoryExportService; private readonly ITrajectoryEditableRepository trajectoryRepository; - protected TrajectoryEditableController(IWellService wellService, + protected TrajectoryEditableController(IWellService wellService, ParserServiceFactory parserServiceFactory, TrajectoryExportService trajectoryExportService, ITrajectoryEditableRepository trajectoryRepository) @@ -41,7 +43,20 @@ namespace AsbCloudWebApi.Controllers.Trajectory this.parserServiceFactory = parserServiceFactory; } - protected abstract TrajectoryParserRequest ParserOptions { get; } + protected abstract int IdParserService { get; } + + ActionResult> IHasExcelParser.Parse(Stream file) + { + try + { + var parserService = parserServiceFactory.GetParser(IdParserService); + return Ok(parserService.Parse(file)); + } + catch (FileFormatException ex) + { + return this.ValidationBadRequest("files", ex.Message); + } + } /// /// Возвращает excel шаблон для заполнения строк траектории @@ -79,12 +94,9 @@ namespace AsbCloudWebApi.Controllers.Trajectory if (!await CanUserAccessToWellAsync(idWell, token)) return Forbid(); - var parserService = parserServiceFactory.Create(ParserOptions.IdParserService); - - return this.ParseExcelFile(files, ParserOptions, parserService); + return this.ParseExcelFile(files); } - - + /// /// Добавить одну новую строчку координат для плановой траектории /// diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs index 65ee4b9e..081704ff 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs @@ -1,6 +1,5 @@ using AsbCloudApp.Data.Trajectory; using AsbCloudApp.Repositories; -using AsbCloudApp.Requests.Import; using AsbCloudApp.Services; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.Trajectory.Export; @@ -25,10 +24,5 @@ public class TrajectoryFactManualController : TrajectoryEditableController new() - { - IdParserService = ParserServiceFactory.IdTrajectoryFactManualParserService, - SheetName = "Фактическая траектория", - HeaderRowsCount = 2 - }; + protected override int IdParserService => ParserServiceFactory.IdTrajectoryFactManualParserService; } \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs index 87c4ac20..2d525c16 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs @@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Requests.Import; using AsbCloudInfrastructure.Services; namespace AsbCloudWebApi.Controllers.Trajectory @@ -32,13 +31,8 @@ namespace AsbCloudWebApi.Controllers.Trajectory { this.trajectoryVisualizationService = trajectoryVisualizationService; } - - protected override TrajectoryParserRequest ParserOptions => new() - { - IdParserService = ParserServiceFactory.IdTrajectoryPlanParserService, - SheetName = "Плановая траектория", - HeaderRowsCount = 2 - }; + + protected override int IdParserService => ParserServiceFactory.IdTrajectoryPlanParserService; /// /// Получение координат для визуализации траектории (плановой и фактической) diff --git a/AsbCloudWebApi/Extentions.cs b/AsbCloudWebApi/Extentions.cs index 650fed2e..ab111444 100644 --- a/AsbCloudWebApi/Extentions.cs +++ b/AsbCloudWebApi/Extentions.cs @@ -8,8 +8,9 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Security.Claims; using AsbCloudApp.Data; -using AsbCloudApp.Requests.Import; -using AsbCloudApp.Services; +using AsbCloudApp.Exceptions; +using AsbCloudApp.Requests.ParserOptions; +using AsbCloudWebApi.Controllers.Interfaces; using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.Mvc @@ -87,32 +88,35 @@ namespace Microsoft.AspNetCore.Mvc return options; } - public static ActionResult> ParseExcelFile(this ControllerBase controller, - IFormFileCollection files, - TOptions options, - IParserService parserService) + public static ActionResult> ParseExcelFile(this IHasExcelParser controller, + IFormFileCollection files) where TDto : class, IId - where TOptions : ParserOptionsRequestBase + { + using var file = GetExcelFile(files); + return controller.Parse(file); + } + + public static ActionResult> ParseExcelFileWithOptions( + this IHasExcelParserWithOptions controller, + IFormFileCollection files, + TOptions options) + where TDto : class, IId + where TOptions : class, IParserOptionsRequest + { + using var file = GetExcelFile(files); + return controller.Parse(file, options); + } + + private static Stream GetExcelFile(IFormFileCollection files) { if (files.Count < 1) - return controller.ValidationBadRequest(nameof(files), "Нет файла"); + throw new ArgumentInvalidException(nameof(files), "Нет файла"); var file = files[0]; if (Path.GetExtension(file.FileName).ToLower() != ".xlsx") - return controller.ValidationBadRequest(nameof(files), "Требуется .xlsx файл."); + throw new ArgumentInvalidException(nameof(files), "Требуется .xlsx файл."); - using var stream = file.OpenReadStream(); - - try - { - var items = parserService.Parse(stream, options); - - return controller.Ok(items); - } - catch (FileFormatException ex) - { - return controller.ValidationBadRequest(nameof(files), ex.Message); - } + return file.OpenReadStream(); } } } \ No newline at end of file