From 5c99d4ff570ef75fb0bc1f72db264db5f5030755 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 11 Mar 2024 14:50:43 +0500 Subject: [PATCH 1/2] ProcessMapPlanBaseController.Parse() accepts single file --- .../ProcessMaps/ProcessMapPlanBaseController.cs | 9 +++++---- AsbCloudWebApi/Extensions.cs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs index 09d735fe..34668ac8 100644 --- a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs @@ -15,6 +15,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Requests.ParserOptions; using AsbCloudInfrastructure.Services.Parser; using AsbCloudApp.Data.ProcessMaps; +using System.ComponentModel.DataAnnotations; namespace AsbCloudWebApi.Controllers.ProcessMaps; @@ -205,19 +206,19 @@ public abstract class ProcessMapPlanBaseController : ControllerBase /// Импорт РТК из excel (xlsx) файла /// /// - /// + /// /// /// [HttpPost("parse")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task>> Parse(int idWell, - [FromForm] IFormFileCollection files, + [Required] IFormFile file, CancellationToken token) { await AssertUserHasAccessToWell(idWell, token); - var stream = files.GetExcelFile(); + var stream = file.GetExcelFile(); try { @@ -226,7 +227,7 @@ public abstract class ProcessMapPlanBaseController : ControllerBase } catch (FileFormatException ex) { - return this.ValidationBadRequest(nameof(files), ex.Message); + return this.ValidationBadRequest(nameof(file), ex.Message); } } diff --git a/AsbCloudWebApi/Extensions.cs b/AsbCloudWebApi/Extensions.cs index 59c72e37..26cea7ba 100644 --- a/AsbCloudWebApi/Extensions.cs +++ b/AsbCloudWebApi/Extensions.cs @@ -113,4 +113,18 @@ public static class Extensions return file.OpenReadStream(); } + + /// + /// Получение Excel + /// + /// + /// + /// + public static Stream GetExcelFile(this IFormFile file) + { + if (Path.GetExtension(file.FileName).ToLower() != ".xlsx") + throw new ArgumentInvalidException(nameof(file), "Требуется .xlsx файл."); + + return file.OpenReadStream(); + } } \ No newline at end of file From 63638219a218eaec4524af94df6b7f9a995440d8 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 11 Mar 2024 16:26:58 +0500 Subject: [PATCH 2/2] ProcessMapPlanBaseController.Parse() sets IdWell for result dtos --- .../ProcessMapPlanBaseController.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs index 34668ac8..45da7f11 100644 --- a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs @@ -53,12 +53,13 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpPost] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task InsertRange([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) + public async Task InsertRange([FromRoute][Range(0,int.MaxValue)] int idWell, IEnumerable dtos, CancellationToken token) { - if (idWell == 0 || dtos.Any(d => d.IdWell != idWell)) - return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell"); - var idUser = await AssertUserHasAccessToWell(idWell, token); + + foreach (var dto in dtos) + dto.IdWell = idWell; + var result = await repository.InsertRange(idUser, dtos, token); return Ok(result); } @@ -75,11 +76,11 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task ClearAndInsertRange([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) { - if (idWell == 0 || dtos.Any(d => d.IdWell != idWell)) - return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell"); - var idUser = await AssertUserHasAccessToWell(idWell, token); + foreach (var dto in dtos) + dto.IdWell = idWell; + var request = new ProcessMapPlanBaseRequestWithWell(idWell); var result = await repository.ClearAndInsertRange(idUser, request, dtos, token); return Ok(result); @@ -189,15 +190,14 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] public async Task UpdateOrInsertRange([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) { - var first = dtos.FirstOrDefault(); - if (first is null) + if (!dtos.Any()) return NoContent(); - - if (idWell == 0 || dtos.Any(d => d.IdWell != idWell)) - return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell"); - + var idUser = await AssertUserHasAccessToWell(idWell, token); + foreach (var dto in dtos) + dto.IdWell = idWell; + var result = await repository.UpdateOrInsertRange(idUser, dtos, token); return Ok(result); } @@ -223,6 +223,9 @@ public abstract class ProcessMapPlanBaseController : ControllerBase try { var dto = parserService.Parse(stream, IParserOptionsRequest.Empty()); + foreach (var item in dto.Item) + item.Item.IdWell = idWell; + return Ok(dto); } catch (FileFormatException ex)