Рефакторинг + доработки

1. Переделал логику получения стволов скважин.
2. Поправил контроллер.
3. Рефакторинг DTO ствола.
This commit is contained in:
parent 04c2266591
commit 3702cf2e8c
5 changed files with 77 additions and 59 deletions

View File

@ -10,12 +10,12 @@ public class WellboreDto
/// <summary>
/// Идентификатор
/// </summary>
public int? Id { get; set; }
public int Id { get; set; }
/// <summary>
/// Название
/// </summary>
public string? Name { get; set; }
public string Name { get; set; } = null!;
/// <summary>
/// Идентификатор скважины
@ -45,20 +45,20 @@ public class WellboreDto
/// <summary>
/// Начальная глубина ствола
/// </summary>
public double? DepthFrom { get; set; }
public double DepthFrom { get; set; }
/// <summary>
/// Конечная глубина скважины
/// </summary>
public double? DepthTo { get; set; }
public double DepthTo { get; set; }
/// <summary>
/// Дата начала первой операции
/// </summary>
public DateTimeOffset? DateFrom { get; set; }
public DateTimeOffset DateFrom { get; set; }
/// <summary>
/// Дата завершения последней операции
/// </summary>
public DateTimeOffset? DateTo { get; set; }
public DateTimeOffset DateTo { get; set; }
}

View File

@ -78,10 +78,10 @@ namespace AsbCloudApp.Repositories
/// Получение ствола скважины
/// </summary>
/// <param name="idWell"></param>
/// <param name="idWellSectionType"></param>
/// <param name="idSection"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<WellboreDto?> GetWellboreAsync(int idWell, int? idWellSectionType, CancellationToken cancellationToken);
Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken);
/// <summary>
/// Получение стволов скважин
@ -89,8 +89,7 @@ namespace AsbCloudApp.Repositories
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<PaginationContainer<WellboreDto>> GetWellboresAsync(WellboreRequest request,
CancellationToken cancellationToken);
Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken);
/// <summary>
/// Добавить несколько операций за один раз

View File

@ -10,5 +10,5 @@ public class WellboreRequest : RequestBase
/// <summary>
/// Пары идентификаторов скважины и секции
/// </summary>
public IEnumerable<(int idWell, int? idWellSectionType)> Ids { get; set; } = null!;
public IEnumerable<(int idWell, int? idSection)> Ids { get; set; } = null!;
}

View File

@ -255,77 +255,89 @@ namespace AsbCloudInfrastructure.Repository
return dtos;
}
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int? idWellSectionType,
CancellationToken cancellationToken)
public async Task<WellboreDto?> GetWellboreAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
var well = await db.Wells.Include(w => w.WellOperations)
.FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken);
if (well is null)
var section = await db.WellSectionTypes
.FirstOrDefaultAsync(w => w.Id == idSection, cancellationToken);
if (well is null || section is null)
return null;
var factOperations = well.WellOperations.AsQueryable();
var factOperations = well.WellOperations
.Where(o => o.IdType == WellOperation.IdOperationTypeFact &&
o.IdWellSectionType == section.Id)
.OrderBy(o => o.DateStart);
var section = await db.WellSectionTypes
.FirstOrDefaultAsync(w => w.Id == idWellSectionType, cancellationToken);
if (section is not null)
factOperations = factOperations.Where(w => w.IdWellSectionType == section.Id);
factOperations = factOperations.OrderBy(f => f.DateStart);
var firstOperation = factOperations.FirstOrDefault();
var lastOperation = factOperations.LastOrDefault();
if (!factOperations.Any())
return null;
var firstOperation = factOperations.First();
var lastOperation = factOperations.Last();
return new WellboreDto
{
Id = section?.Id,
Name = section?.Caption,
Id = section.Id,
Name = section.Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation?.DepthStart,
DepthTo = lastOperation?.DepthEnd,
DateFrom = firstOperation?.DateStart,
DateTo = lastOperation?.DateStart.AddHours(lastOperation.DurationHours),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
};
}
public async Task<PaginationContainer<WellboreDto>> GetWellboresAsync(WellboreRequest request,
CancellationToken cancellationToken)
public async Task<IEnumerable<WellboreDto>> GetWellboresAsync(WellboreRequest request, CancellationToken cancellationToken)
{
var wellbores = new List<WellboreDto>();
var skip = request.Skip ?? 0;
var take = request.Take ?? 10;
var sections = GetSectionTypes()
.ToDictionary(w => w.Id, w => w);
foreach (var id in request.Ids)
foreach (var (idWell, idSection) in request.Ids)
{
var wellbore = await GetWellboreAsync(id.idWell,
id.idWellSectionType,
cancellationToken);
var well = await db.Wells.Include(w => w.WellOperations)
.FirstOrDefaultAsync(w => w.Id == idWell, cancellationToken);
if (wellbore is null)
if (well is null)
continue;
var factOperations = well.WellOperations
.Where(o => o.IdType == WellOperation.IdOperationTypeFact)
.OrderBy(o => o.DateStart)
.GroupBy(o => o.IdWellSectionType);
wellbores.Add(wellbore);
if(idSection.HasValue)
factOperations = factOperations.Where(w => w.Key == idSection);
wellbores.AddRange(from factOperation in factOperations
let firstOperation = factOperation.First()
let lastOperation = factOperation.Last()
select new WellboreDto
{
Id = sections[factOperation.Key].Id,
Name = sections[factOperation.Key].Caption,
IdWell = well.Id,
WellName = well.Caption,
IdWellState = well.IdState,
IdWellTelemetry = well.IdTelemetry,
WellTimezone = well.Timezone.Adapt<SimpleTimezoneDto>(),
DepthFrom = firstOperation.DepthStart,
DepthTo = lastOperation.DepthEnd,
DateFrom = firstOperation.DateStart,
DateTo = lastOperation.DateStart.AddHours(lastOperation.DurationHours),
});
}
var result = new PaginationContainer<WellboreDto>
{
Skip = skip,
Take = take,
Count = wellbores.Count
};
if (result.Count < skip)
return result;
result.Items = wellbores.Skip(skip).Take(take).ToList();
return result;
return wellbores.Skip(skip).Take(take);
}
/// <inheritdoc/>

View File

@ -6,6 +6,7 @@ using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers;
@ -29,15 +30,21 @@ public class WellboreController : ControllerBase
/// Получение ствола скважины
/// </summary>
/// <param name="idWell">Id скважины</param>
/// <param name="idSectionType">Опциональный параметр. Id типа секции</param>
/// <param name="idSection">Id типа секции скважины</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("{idWell:int}")]
[ProducesResponseType(typeof(WellboreDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, int? idSectionType, CancellationToken cancellationToken)
[HttpGet("{idWell:int}/{idSection:int}")]
[ProducesResponseType(typeof(WellboreDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<IActionResult> GetAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
return Ok(await wellOperationRepository.GetWellboreAsync(idWell, idSectionType, cancellationToken));
var wellbore = await wellOperationRepository.GetWellboreAsync(idWell, idSection, cancellationToken);
if (wellbore is null)
return NoContent();
return Ok(wellbore);
}
/// <summary>
@ -49,7 +56,7 @@ public class WellboreController : ControllerBase
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(PaginationContainer<WellboreDto>), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(IEnumerable<WellboreDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllAsync([FromQuery] IEnumerable<string> ids,
int? skip,
int? take,