diff --git a/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMaps/ProcessMapPlanBaseController.cs index 09d735fe..45da7f11 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; @@ -52,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); } @@ -74,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); @@ -188,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); } @@ -205,28 +206,31 @@ 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 { var dto = parserService.Parse(stream, IParserOptionsRequest.Empty()); + foreach (var item in dto.Item) + item.Item.IdWell = idWell; + return Ok(dto); } 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