forked from ddrilling/AsbCloudServer
Валидация данных wellOperation перед вставкой, удалением, импортом...
This commit is contained in:
parent
38cbbe4109
commit
6738a30592
@ -114,5 +114,20 @@ namespace AsbCloudApp.Repositories
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<DatesRangeDto?> GetDatesRangeAsync(int idWell, int idType, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Валидация данных
|
||||
/// </summary>
|
||||
/// <param name="wellOperations"></param>
|
||||
/// <returns></returns>
|
||||
bool Validate(IEnumerable<WellOperationDto> wellOperations);
|
||||
|
||||
/// <summary>
|
||||
/// Валидация данных (проверка с базой)
|
||||
/// </summary>
|
||||
/// <param name="wellOperations"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ValidateWithDbAsync(IEnumerable<WellOperationDto> wellOperations, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
@ -311,7 +311,8 @@ public class WellOperationRepository : IWellOperationRepository
|
||||
{
|
||||
dtos = dtos
|
||||
.GroupBy(o => o.IdParent!)
|
||||
.Select(g => {
|
||||
.Select(g =>
|
||||
{
|
||||
var idCategory = g.Key ?? int.MinValue;
|
||||
var category = parentRelationDictionary.GetValueOrDefault(idCategory);
|
||||
var newDto = new WellGroupOpertionDto
|
||||
@ -330,6 +331,58 @@ public class WellOperationRepository : IWellOperationRepository
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public async Task<bool> ValidateWithDbAsync(IEnumerable<WellOperationDto> wellOperationDtos, CancellationToken token)
|
||||
{
|
||||
var firstOperation = wellOperationDtos
|
||||
.FirstOrDefault();
|
||||
if (firstOperation is null)
|
||||
return false;
|
||||
|
||||
var request = new WellOperationRequest()
|
||||
{
|
||||
IdWell = firstOperation.IdWell,
|
||||
OperationType = firstOperation.IdType,
|
||||
};
|
||||
|
||||
var operationWithMaxDateStart = await BuildQuery(request)
|
||||
.OrderByDescending(o => o.DateStart)
|
||||
.FirstOrDefaultAsync(token);
|
||||
|
||||
if (operationWithMaxDateStart is null)
|
||||
return Validate(wellOperationDtos);
|
||||
|
||||
var maxOperationDateStart = operationWithMaxDateStart.DateStart;
|
||||
foreach (var dto in wellOperationDtos)
|
||||
{
|
||||
var currentOperationDateStart = dto.DateStart.ToUniversalTime();
|
||||
if (maxOperationDateStart!.AddMonths(3) < currentOperationDateStart)
|
||||
return false;
|
||||
|
||||
maxOperationDateStart = currentOperationDateStart;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Validate(IEnumerable<WellOperationDto> wellOperationDtos)
|
||||
{
|
||||
var firstOperation = wellOperationDtos
|
||||
.FirstOrDefault();
|
||||
if (firstOperation is null)
|
||||
return false;
|
||||
|
||||
var maxOperationDateStart = firstOperation.DateStart.ToUniversalTime();
|
||||
|
||||
foreach (var dto in wellOperationDtos)
|
||||
{
|
||||
var currentOperationDateStart = dto.DateStart.ToUniversalTime();
|
||||
if (maxOperationDateStart.AddMonths(3) < currentOperationDateStart)
|
||||
return false;
|
||||
|
||||
maxOperationDateStart = currentOperationDateStart;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<int> InsertRangeAsync(
|
||||
IEnumerable<WellOperationDto> wellOperationDtos,
|
||||
|
@ -1,7 +1,13 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Data.WellOperationImport;
|
||||
using AsbCloudApp.Data.WellOperationImport.Options;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Requests;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudApp.Services.WellOperationImport;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@ -12,12 +18,6 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AsbCloudApp.Data.WellOperationImport;
|
||||
using AsbCloudApp.Services.WellOperationImport;
|
||||
using AsbCloudApp.Data.WellOperationImport.Options;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
@ -237,6 +237,9 @@ namespace AsbCloudWebApi.Controllers
|
||||
wellOperation.IdUser = User.GetUserId();
|
||||
wellOperation.IdType = idType;
|
||||
|
||||
if (!await operationRepository.ValidateWithDbAsync(new[] { wellOperation }, cancellationToken))
|
||||
return this.ValidationBadRequest(nameof(wellOperation), "The date difference between the operations is more than 3 months");
|
||||
|
||||
var result = await operationRepository.InsertRangeAsync(new[] { wellOperation }, cancellationToken);
|
||||
|
||||
return Ok(result);
|
||||
@ -287,11 +290,31 @@ namespace AsbCloudWebApi.Controllers
|
||||
wellOperation.IdType = idType;
|
||||
}
|
||||
|
||||
|
||||
if (!await Validate(wellOperations, deleteBeforeInsert, cancellationToken))
|
||||
return this.ValidationBadRequest(nameof(wellOperations), "The date difference between the operations is more than 3 months");
|
||||
|
||||
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<bool> Validate(IEnumerable<WellOperationDto> wellOperations, bool deleteBeforeInsert, CancellationToken cancellationToken)
|
||||
{
|
||||
if (deleteBeforeInsert)
|
||||
return operationRepository.Validate(wellOperations);
|
||||
else
|
||||
return await operationRepository.ValidateWithDbAsync(wellOperations, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновляет выбранную операцию на скважине
|
||||
/// </summary>
|
||||
@ -317,6 +340,9 @@ namespace AsbCloudWebApi.Controllers
|
||||
value.LastUpdateDate = DateTimeOffset.UtcNow;
|
||||
value.IdUser = User.GetUserId();
|
||||
|
||||
if (!await operationRepository.ValidateWithDbAsync(new[] { value }, token))
|
||||
return this.ValidationBadRequest(nameof(value), "The date difference between the operations is more than 3 months");
|
||||
|
||||
var result = await operationRepository.UpdateAsync(value, token)
|
||||
.ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
|
Loading…
Reference in New Issue
Block a user