Merge branch 'dev' into feature/integration_tests

This commit is contained in:
ngfrolov 2024-03-13 10:20:37 +05:00
commit b2ad36a303
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 35 additions and 17 deletions

View File

@ -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<TDto> : ControllerBase
[HttpPost]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> InsertRange([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
public async Task<IActionResult> InsertRange([FromRoute][Range(0,int.MaxValue)] int idWell, IEnumerable<TDto> 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<TDto> : ControllerBase
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> ClearAndInsertRange([FromRoute] int idWell, IEnumerable<TDto> 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<TDto> : ControllerBase
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdateOrInsertRange([FromRoute] int idWell, IEnumerable<TDto> 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<TDto> : ControllerBase
/// Импорт РТК из excel (xlsx) файла
/// </summary>
/// <param name="idWell"></param>
/// <param name="files"></param>
/// <param name="file"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("parse")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<ActionResult<ParserResultDto<TDto>>> 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);
}
}

View File

@ -113,4 +113,18 @@ public static class Extensions
return file.OpenReadStream();
}
/// <summary>
/// Получение Excel
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
/// <exception cref="ArgumentInvalidException"></exception>
public static Stream GetExcelFile(this IFormFile file)
{
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
throw new ArgumentInvalidException(nameof(file), "Требуется .xlsx файл.");
return file.OpenReadStream();
}
}