From dc19829948531a2d4ceb1c44f5a900866b187982 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: Thu, 1 Feb 2024 16:37:03 +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 --- .../Interfaces/IControllerWithParser.cs | 13 +++ .../Controllers/Interfaces/IHasExcelParser.cs | 11 --- .../Interfaces/IHasExcelParserWithOptions.cs | 10 -- .../TrajectoryEditableController.cs | 98 ++++++++++--------- .../TrajectoryFactManualController.cs | 8 +- .../Trajectory/TrajectoryPlanController.cs | 8 +- AsbCloudWebApi/Extentions.cs | 14 +-- 7 files changed, 76 insertions(+), 86 deletions(-) create mode 100644 AsbCloudWebApi/Controllers/Interfaces/IControllerWithParser.cs delete mode 100644 AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs delete mode 100644 AsbCloudWebApi/Controllers/Interfaces/IHasExcelParserWithOptions.cs diff --git a/AsbCloudWebApi/Controllers/Interfaces/IControllerWithParser.cs b/AsbCloudWebApi/Controllers/Interfaces/IControllerWithParser.cs new file mode 100644 index 00000000..d11ac201 --- /dev/null +++ b/AsbCloudWebApi/Controllers/Interfaces/IControllerWithParser.cs @@ -0,0 +1,13 @@ +using System.IO; +using AsbCloudApp.Data; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers.Interfaces; + +public interface IControllerWithParser + where TDto : class, IId +{ + ActionResult> Parse(Stream file, TOptions options); + + IActionResult GetTemplate(); +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs b/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs deleted file mode 100644 index f78d17da..00000000 --- a/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParser.cs +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index 96e95eef..00000000 --- a/AsbCloudWebApi/Controllers/Interfaces/IHasExcelParserWithOptions.cs +++ /dev/null @@ -1,10 +0,0 @@ -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 19f834e0..7a04e7fc 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryEditableController.cs @@ -10,6 +10,7 @@ using System.IO; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; +using AsbCloudApp.Requests.ParserOptions; using AsbCloudInfrastructure.Services; using AsbCloudWebApi.Controllers.Interfaces; @@ -22,35 +23,33 @@ namespace AsbCloudWebApi.Controllers.Trajectory [ApiController] [Authorize] public abstract class TrajectoryEditableController : TrajectoryController, - IHasExcelParser + IControllerWithParser where TDto : TrajectoryGeoDto { - private readonly ParserServiceFactory parserServiceFactory; - private readonly TrajectoryExportService trajectoryExportService; - private readonly ITrajectoryEditableRepository trajectoryRepository; + private readonly IParserService parserService; + private readonly ITrajectoryEditableRepository trajectoryRepository; protected TrajectoryEditableController(IWellService wellService, ParserServiceFactory parserServiceFactory, TrajectoryExportService trajectoryExportService, - ITrajectoryEditableRepository trajectoryRepository) + ITrajectoryEditableRepository trajectoryRepository, + int idParserService) : base( wellService, trajectoryExportService, trajectoryRepository) { - this.trajectoryExportService = trajectoryExportService; - this.trajectoryRepository = trajectoryRepository; - this.parserServiceFactory = parserServiceFactory; + parserService = parserServiceFactory.Create(idParserService); + this.trajectoryRepository = trajectoryRepository; } - protected abstract int IdParserService { get; } - - ActionResult> IHasExcelParser.Parse(Stream file) + ActionResult> IControllerWithParser.Parse(Stream file, + IParserOptionsRequest options) { try { - var parserService = parserServiceFactory.GetParser(IdParserService); - return Ok(parserService.Parse(file)); + var parserResult = parserService.Parse(file, options); + return Ok(parserResult); } catch (FileFormatException ex) { @@ -68,7 +67,7 @@ namespace AsbCloudWebApi.Controllers.Trajectory [ProducesResponseType(StatusCodes.Status204NoContent)] public IActionResult GetTemplate() { - var stream = trajectoryExportService.GetTemplateFile(); + var stream = parserService.GetTemplateFile(); return File(stream, "application/octet-stream", fileName); } @@ -94,46 +93,19 @@ namespace AsbCloudWebApi.Controllers.Trajectory if (!await CanUserAccessToWellAsync(idWell, token)) return Forbid(); - return this.ParseExcelFile(files); + return this.ParseExcelFile(files, IParserOptionsRequest.Empty()); } - /// - /// Добавить одну новую строчку координат для плановой траектории - /// - /// - /// - /// - /// количество успешно записанных строк в БД - [HttpPost] - [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public async Task AddAsync(int idWell, [FromBody] TDto row, - CancellationToken token) - { - if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) - return Forbid(); - var idUser = User.GetUserId(); - if (!idUser.HasValue) - return Forbid(); - row.IdUser = idUser.Value; - row.IdWell = idWell; - var result = await trajectoryRepository.AddAsync(row, token); - return Ok(result); - } - /// - /// Добавить массив строчек координат для плановой траектории + /// Добавление /// /// /// - /// /// - /// количество успешно записанных строк в БД - [HttpPost("range/{deleteBeforeInsert:bool}")] + /// + [HttpPost] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public async Task AddRangeAsync(int idWell, - [FromBody] IEnumerable dtos, - bool deleteBeforeInsert, - CancellationToken token) + public async Task InsertRangeAsync(int idWell, [FromBody] IEnumerable dtos, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token)) return Forbid(); @@ -149,9 +121,39 @@ namespace AsbCloudWebApi.Controllers.Trajectory dto.IdWell = idWell; } - if (deleteBeforeInsert) - await trajectoryRepository.DeleteByIdWellAsync(idWell, token); + var result = await trajectoryRepository.AddRangeAsync(dtos, token); + return Ok(result); + } + /// + /// Удалить все по скважине и добавить новые + /// + /// + /// + /// + /// + [HttpPost("replace")] + [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] + public async Task ClearAndInsertRangeAsync(int idWell, [FromBody] IEnumerable dtos, CancellationToken token) + { + //TODO: это вся радость требует рефакторинга. + //Удаление с добавлением новых записей должно происходить в рамках одной транзакции, да и вообще должно быть реализовано на уровне репозиторий. + //Рефакторинг будет когда доберёмся до журнала изменений для траекторий. + if (!await CanUserAccessToWellAsync(idWell, token)) + return Forbid(); + + var idUser = User.GetUserId(); + + if (!idUser.HasValue) + return Forbid(); + + foreach (var dto in dtos) + { + dto.IdUser = idUser.Value; + dto.IdWell = idWell; + } + + await trajectoryRepository.DeleteByIdWellAsync(idWell, token); var result = await trajectoryRepository.AddRangeAsync(dtos, token); return Ok(result); } diff --git a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs index 081704ff..97b84b07 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryFactManualController.cs @@ -20,9 +20,11 @@ public class TrajectoryFactManualController : TrajectoryEditableController trajectoryRepository) - : base(wellService, parserServiceFactory, trajectoryExportService, trajectoryRepository) + : base(wellService, + parserServiceFactory, + trajectoryExportService, + trajectoryRepository, + ParserServiceFactory.IdTrajectoryFactManualParserService) { } - - 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 2d525c16..cf3f648d 100644 --- a/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs +++ b/AsbCloudWebApi/Controllers/Trajectory/TrajectoryPlanController.cs @@ -27,13 +27,15 @@ namespace AsbCloudWebApi.Controllers.Trajectory ParserServiceFactory parserServiceFactory, ITrajectoryEditableRepository trajectoryRepository, TrajectoryService trajectoryVisualizationService) - : base(wellService, parserServiceFactory, trajectoryExportService, trajectoryRepository) + : base(wellService, + parserServiceFactory, + trajectoryExportService, + trajectoryRepository, + ParserServiceFactory.IdTrajectoryPlanParserService) { this.trajectoryVisualizationService = trajectoryVisualizationService; } - protected override int IdParserService => ParserServiceFactory.IdTrajectoryPlanParserService; - /// /// Получение координат для визуализации траектории (плановой и фактической) /// diff --git a/AsbCloudWebApi/Extentions.cs b/AsbCloudWebApi/Extentions.cs index ab111444..d6ce96a4 100644 --- a/AsbCloudWebApi/Extentions.cs +++ b/AsbCloudWebApi/Extentions.cs @@ -87,17 +87,9 @@ namespace Microsoft.AspNetCore.Mvc TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter))); return options; } - - public static ActionResult> ParseExcelFile(this IHasExcelParser controller, - IFormFileCollection files) - where TDto : class, IId - { - using var file = GetExcelFile(files); - return controller.Parse(file); - } - - public static ActionResult> ParseExcelFileWithOptions( - this IHasExcelParserWithOptions controller, + + public static ActionResult> ParseExcelFile( + this IControllerWithParser controller, IFormFileCollection files, TOptions options) where TDto : class, IId