using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers;
///
/// Ствол скважины
///
[Authorize]
[ApiController]
[Route("api/well/[controller]")]
public class WellboreController : ControllerBase
{
private readonly IWellOperationRepository wellOperationRepository;
public WellboreController(IWellOperationRepository wellOperationRepository)
{
this.wellOperationRepository = wellOperationRepository;
}
///
/// Получение ствола скважины
///
/// Id скважины
/// Id типа секции скважины
///
///
[HttpGet("{idWell:int}/{idSection:int}")]
[ProducesResponseType(typeof(WellboreDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task GetAsync(int idWell, int idSection, CancellationToken cancellationToken)
{
var wellbore = await wellOperationRepository.GetWellboreAsync(idWell, idSection, cancellationToken);
if (wellbore is null)
return NoContent();
return Ok(wellbore);
}
///
/// Получение списка стволов скважин
///
/// Пары идентификаторов скважины и секции
/// Опциональный параметр. Количество пропускаемых записей
/// Опциональный параметр. Количество получаемых записей
///
///
[HttpGet]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
public async Task GetAllAsync([FromQuery] IEnumerable ids,
int? skip,
int? take,
CancellationToken cancellationToken)
{
var request = new WellboreRequest
{
Ids = ParseIds(ids),
Skip = skip,
Take = take
};
return Ok(await wellOperationRepository.GetWellboresAsync(request, cancellationToken));
}
private static IEnumerable<(int, int?)> ParseIds(IEnumerable ids)
{
var result = new List<(int, int?)>();
foreach (var id in ids)
{
var idPair = id.Split(',');
if (!int.TryParse(idPair[0], out var idWell))
throw new ArgumentInvalidException("Не удалось получить Id скважины", nameof(ids));
if (idPair.Length < 2 || !int.TryParse(idPair[1], out var idWellSectionType))
{
result.Add((idWell, null));
continue;
}
result.Add((idWell, idWellSectionType));
}
return result;
}
}