replace BadRequest by ValidationBadRequest

This commit is contained in:
ngfrolov 2023-09-28 17:42:30 +05:00
parent 1f9dc2b061
commit 4a6bc2c747
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
9 changed files with 28 additions and 20 deletions

View File

@ -88,10 +88,11 @@ namespace AsbCloudWebApi.Controllers
/// <returns>id</returns>
[HttpPost("range")]
[Permission]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public virtual async Task<ActionResult<int>> InsertRangeAsync([FromBody] IEnumerable<T> values, CancellationToken token)
{
if (!values.Any())
return BadRequest("there is no values to add");
return this.ValidationBadRequest(nameof(values), "there is no values to add");
if (InsertForbidAsync is not null)
foreach (var value in values)
@ -110,6 +111,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
[HttpPut]
[Permission]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public virtual async Task<ActionResult<int>> UpdateAsync([FromBody] T value, CancellationToken token)
{
if (UpdateForbidAsync is not null && await UpdateForbidAsync(value, token))
@ -117,7 +119,7 @@ namespace AsbCloudWebApi.Controllers
var result = await service.UpdateAsync(value, token).ConfigureAwait(false);
if (result == ICrudRepository<T>.ErrorIdNotFound)
return BadRequest($"id:{value.Id} does not exist in the db");
return this.ValidationBadRequest(nameof(value.Id), $"id:{value.Id} does not exist");
return Ok(result);
}

View File

@ -81,6 +81,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns>количество успешно записанных строк в БД</returns>
[HttpPost("import/{deleteBeforeImport}")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> ImportAsync(int idWell,
[FromForm] IFormFileCollection files,
bool deleteBeforeImport,
@ -93,10 +94,10 @@ namespace AsbCloudWebApi.Controllers
token).ConfigureAwait(false))
return Forbid();
if (files.Count < 1)
return BadRequest("нет файла");
return this.ValidationBadRequest(nameof(files), "нет файла");
var file = files[0];
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return BadRequest("Требуется xlsx файл.");
return this.ValidationBadRequest(nameof(files), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
try
@ -106,7 +107,7 @@ namespace AsbCloudWebApi.Controllers
}
catch (FileFormatException ex)
{
return BadRequest(ex.Message);
return this.ValidationBadRequest(nameof(files), ex.Message);
}
}

View File

@ -64,11 +64,12 @@ namespace AsbCloudWebApi.Controllers
[Obsolete("use GetByUidAsync(..) instead")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult GetByTelemetry(string uid, DateTime updateFrom, CancellationToken token)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is null)
return BadRequest($"Wrong uid {uid}");
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
return Ok(Enumerable.Empty<ProcessMapPlanDto>());
}
@ -82,11 +83,12 @@ namespace AsbCloudWebApi.Controllers
[HttpGet("/api/telemetry/{uid}/processMap")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken token)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is null)
return BadRequest($"Wrong uid {uid}");
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
var dto = await service.GetAllAsync((int)idWell,
updateFrom, token);
@ -191,6 +193,7 @@ namespace AsbCloudWebApi.Controllers
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("import/{idWell}/{options}")]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> ImportAsync(int idWell,
int options,
[Required] IFormFile file,
@ -205,7 +208,7 @@ namespace AsbCloudWebApi.Controllers
return Forbid();
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return BadRequest("Требуется xlsx файл.");
return this.ValidationBadRequest(nameof(file), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
@ -219,7 +222,7 @@ namespace AsbCloudWebApi.Controllers
}
catch (FileFormatException ex)
{
return BadRequest(ex.Message);
return this.ValidationBadRequest(nameof(file), ex.Message);
}
return Ok();

View File

@ -59,10 +59,11 @@ namespace AsbCloudWebApi.Controllers
/// <returns></returns>
[HttpGet("schema/{typeName}")]
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult GetSchema(string typeName)
{
if (string.IsNullOrEmpty(typeName))
return BadRequest("require typeName");
return this.ValidationBadRequest(nameof(typeName), "require typeName");
var type = RuntimeTypeModel.Default
.GetTypes()

View File

@ -125,6 +125,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
[Permission]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> ExportAsync(int? idWell, int? idCluster, CancellationToken token)
{
if (idCluster is null && idWell is null)

View File

@ -33,6 +33,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
[HttpGet]
[Permission]
[ProducesResponseType(typeof(PaginationContainer<MessageDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetMessagesAsync(
[FromRoute] int idWell,
[FromQuery] MessageRequestBase request,
@ -43,7 +44,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
return Forbid();
if (request.Take > 1024)
return BadRequest("limit mast be less then 1024");
return this.ValidationBadRequest(nameof(request.Take), "limit mast be less then 1024");
var requestToService = new MessageRequest(request, idWell);

View File

@ -51,6 +51,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
[HttpPost("api/well/{idWell}/setpoints")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task<IActionResult> InsertAsync(int idWell, SetpointsRequestDto setpoints, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
@ -70,10 +71,10 @@ namespace AsbCloudWebApi.Controllers.SAUB
if (setpoints is null
|| setpoints.ObsolescenceSec > ObsolescenceSecMax
|| setpoints.ObsolescenceSec < ObsolescenceSecMin)
return BadRequest("Wrong ObsolescenceSec");
return this.ValidationBadRequest(nameof(setpoints.ObsolescenceSec), "Wrong ObsolescenceSec");
if (!setpoints.Setpoints.Any())
return BadRequest("Wrong setpoints count");
return this.ValidationBadRequest(nameof(setpoints.Setpoints), "Wrong setpoints count");
var result = await setpointsService.InsertAsync(setpoints, token)
.ConfigureAwait(false);

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using AsbCloudApp.Repositories;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudWebApi.Controllers.SAUB
{
@ -47,11 +48,8 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// <returns></returns>
[HttpPost("{uid}")]
[AllowAnonymous]
public async Task<IActionResult> PostDataAsync(string uid, [FromBody] TelemetryWirelineRunOutBaseDto dto, CancellationToken token)
public async Task<IActionResult> PostDataAsync(string uid, [FromBody, Required] TelemetryWirelineRunOutBaseDto dto, CancellationToken token)
{
if (dto is null)
return BadRequest("Dto shouldn't be null");
var data = await repository.AddOrUpdateAsync(uid, dto, token);
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);

View File

@ -328,11 +328,11 @@ namespace AsbCloudWebApi.Controllers
return Forbid();
if (files.Count < 1)
return BadRequest("Нет файла");
return this.ValidationBadRequest(nameof(files), "Нет файла");
var file = files[0];
if (Path.GetExtension(file.FileName).ToLower() != ".xlsx")
return BadRequest("Требуется xlsx файл.");
return this.ValidationBadRequest(nameof(files), "Требуется xlsx файл.");
using Stream stream = file.OpenReadStream();
@ -348,7 +348,7 @@ namespace AsbCloudWebApi.Controllers
}
catch (FileFormatException ex)
{
return BadRequest(ex.Message);
return this.ValidationBadRequest(nameof(files), ex.Message);
}
return Ok();