forked from ddrilling/AsbCloudServer
WellOperationController remove validation by dates gap between operations
This commit is contained in:
parent
f7caa7eb38
commit
97e01be7db
@ -118,21 +118,6 @@ namespace AsbCloudApp.Repositories
|
||||
/// <returns></returns>
|
||||
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Валидация данных
|
||||
/// </summary>
|
||||
/// <param name="wellOperations"></param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<ValidationResult> Validate(IEnumerable<WellOperationDto> wellOperations);
|
||||
|
||||
/// <summary>
|
||||
/// Валидация данных (проверка с базой)
|
||||
/// </summary>
|
||||
/// <param name="wellOperations"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<ValidationResult>> ValidateWithDbAsync(IEnumerable<WellOperationDto> wellOperations, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Удаление полных дубликатов операций по всем скважинам
|
||||
/// </summary>
|
||||
|
@ -4,10 +4,6 @@ using AsbCloudApp.Requests;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb;
|
||||
using AsbCloudDb.Model;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using DocumentFormat.OpenXml.Vml;
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using Irony.Parsing;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
@ -21,7 +17,6 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Repository;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// репозиторий операций по скважине
|
||||
/// </summary>
|
||||
@ -326,59 +321,6 @@ public class WellOperationRepository : IWellOperationRepository
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ValidationResult>> ValidateWithDbAsync(IEnumerable<WellOperationDto> wellOperationDtos, CancellationToken token)
|
||||
{
|
||||
var firstOperation = wellOperationDtos
|
||||
.FirstOrDefault();
|
||||
|
||||
if (firstOperation is null)
|
||||
return Enumerable.Empty<ValidationResult>();
|
||||
|
||||
var request = new WellOperationRequest()
|
||||
{
|
||||
IdWell = firstOperation.IdWell,
|
||||
OperationType = firstOperation.IdType,
|
||||
};
|
||||
|
||||
var entities = await BuildQuery(request)
|
||||
.AsNoTracking()
|
||||
.ToArrayAsync(token);
|
||||
|
||||
var wellOperationsUnion = entities.Union(wellOperationDtos).OrderBy(o => o.DateStart);
|
||||
|
||||
var results = Validate(wellOperationsUnion);
|
||||
return results;
|
||||
}
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(IEnumerable<WellOperationDto> wellOperationDtos)
|
||||
{
|
||||
var enumerator = wellOperationDtos.OrderBy(o => o.DateStart)
|
||||
.GetEnumerator();
|
||||
|
||||
if (!enumerator.MoveNext())
|
||||
yield break;
|
||||
|
||||
var previous = enumerator.Current;
|
||||
|
||||
while(enumerator.MoveNext())
|
||||
{
|
||||
var current = enumerator.Current;
|
||||
var previousDateStart = previous.DateStart.ToUniversalTime();
|
||||
var currentDateStart = current.DateStart.ToUniversalTime();
|
||||
|
||||
var previousDateEnd = previous.DateStart.AddHours(previous.DurationHours).ToUniversalTime();
|
||||
|
||||
if (previousDateStart.AddDays(Gap) < currentDateStart)
|
||||
{
|
||||
yield return new ValidationResult(
|
||||
"Разница дат между операциями не должна превышать 90 дней",
|
||||
new[] { nameof(wellOperationDtos) });
|
||||
}
|
||||
|
||||
previous = current;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<int> InsertRangeAsync(
|
||||
IEnumerable<WellOperationDto> wellOperationDtos,
|
||||
|
@ -36,56 +36,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
||||
}
|
||||
};
|
||||
|
||||
private readonly WellOperationDto[] dtosWithError = new WellOperationDto[]
|
||||
{
|
||||
new()
|
||||
{
|
||||
Id = 3,
|
||||
IdWell = idWell,
|
||||
IdType = 1,
|
||||
DateStart = DateTimeOffset.Now,
|
||||
CategoryInfo = "1",
|
||||
CategoryName = "1",
|
||||
Comment = "1",
|
||||
Day = 1,
|
||||
DepthEnd = 20,
|
||||
DepthStart = 10,
|
||||
DurationHours = 1,
|
||||
IdCategory = 5000,
|
||||
IdParentCategory = null,
|
||||
IdPlan = null,
|
||||
IdUser = 1,
|
||||
IdWellSectionType = 1,
|
||||
LastUpdateDate = DateTimeOffset.Now,
|
||||
NptHours = 1,
|
||||
WellSectionTypeName = null,
|
||||
UserName = null
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = 4,
|
||||
IdWell = idWell,
|
||||
IdType = 1,
|
||||
DateStart = DateTimeOffset.Now.AddDays(1000),
|
||||
CategoryInfo = "1",
|
||||
CategoryName = "1",
|
||||
Comment = "1",
|
||||
Day = 1,
|
||||
DepthEnd = 20,
|
||||
DepthStart = 10,
|
||||
DurationHours = 1,
|
||||
IdCategory = 5000,
|
||||
IdParentCategory = null,
|
||||
IdPlan = null,
|
||||
IdUser = 1,
|
||||
IdWellSectionType = 1,
|
||||
LastUpdateDate = DateTimeOffset.Now,
|
||||
NptHours = 1,
|
||||
WellSectionTypeName = null,
|
||||
UserName = null
|
||||
}
|
||||
};
|
||||
|
||||
private IWellOperationClient client;
|
||||
|
||||
public WellOperationControllerTest(WebAppFactoryFixture factory)
|
||||
@ -109,21 +59,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Неуспешное добавление операций (без предварительной очистки данных)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task InsertRange_returns_error()
|
||||
{
|
||||
//act
|
||||
var response = await client.InsertRangeAsync(idWell, 1, false, dtosWithError);
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Успешное добавление операций (с предварительной очисткой данных)
|
||||
/// </summary>
|
||||
@ -138,20 +73,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Неуспешное добавление операций (с предварительной очисткой данных)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task InsertRangeWithDeleteBefore_returns_error()
|
||||
{
|
||||
//act
|
||||
var response = await client.InsertRangeAsync(idWell, 1, true, dtosWithError);
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Успешное обновление операции
|
||||
/// </summary>
|
||||
@ -166,21 +87,6 @@ public class WellOperationControllerTest : BaseIntegrationTest
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Неуспешное обновление операции
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task UpdateAsync_returns_error()
|
||||
{
|
||||
//act
|
||||
var dto = dtosWithError.LastOrDefault()!;
|
||||
var response = await client.UpdateAsync(idWell, 1, dto, CancellationToken.None);
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение плановых операций
|
||||
|
@ -236,10 +236,6 @@ namespace AsbCloudWebApi.Controllers
|
||||
wellOperation.IdUser = User.GetUserId();
|
||||
wellOperation.IdType = idType;
|
||||
|
||||
var validationResult = await operationRepository.ValidateWithDbAsync(new[] { wellOperation }, cancellationToken);
|
||||
if (validationResult.Any())
|
||||
return this.ValidationBadRequest(validationResult);
|
||||
|
||||
var result = await operationRepository.InsertRangeAsync(new[] { wellOperation }, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
@ -290,32 +286,11 @@ namespace AsbCloudWebApi.Controllers
|
||||
wellOperation.IdType = idType;
|
||||
}
|
||||
|
||||
|
||||
var validationResult = await Validate(wellOperations, deleteBeforeInsert, cancellationToken);
|
||||
if (validationResult.Any())
|
||||
return this.ValidationBadRequest(validationResult);
|
||||
|
||||
var result = await operationRepository.InsertRangeAsync(wellOperations, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Валидация данных перед вставкой / обновлением / импортом
|
||||
/// </summary>
|
||||
/// <param name="wellOperations"></param>
|
||||
/// <param name="deleteBeforeInsert"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<IEnumerable<ValidationResult>> Validate(IEnumerable<WellOperationDto> wellOperations, bool deleteBeforeInsert, CancellationToken cancellationToken)
|
||||
{
|
||||
if (deleteBeforeInsert)
|
||||
return operationRepository.Validate(wellOperations);
|
||||
else
|
||||
return await operationRepository.ValidateWithDbAsync(wellOperations, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновляет выбранную операцию на скважине
|
||||
/// </summary>
|
||||
@ -341,10 +316,6 @@ namespace AsbCloudWebApi.Controllers
|
||||
value.LastUpdateDate = DateTimeOffset.UtcNow;
|
||||
value.IdUser = User.GetUserId();
|
||||
|
||||
var validationResult = await operationRepository.ValidateWithDbAsync(new[] { value }, token);
|
||||
if (validationResult.Any())
|
||||
return this.ValidationBadRequest(validationResult);
|
||||
|
||||
var result = await operationRepository.UpdateAsync(value, token)
|
||||
.ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
|
Loading…
Reference in New Issue
Block a user