using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Exceptions; using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace AsbCloudWebApi.Controllers; /// /// Ствол скважины /// [Authorize] [ApiController] [Route("api/[controller]")] public class WellboreController : ControllerBase { private readonly IWellboreService wellboreService; public WellboreController(IWellboreService wellboreService) { this.wellboreService = wellboreService; } /// /// Получение ствола скважины /// /// 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 wellboreService.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 = ids.Select(id => ParseId(id)), Skip = skip, Take = take }; return Ok(await wellboreService.GetWellboresAsync(request, cancellationToken)); } private static (int, int?) ParseId(string id) { var idPair = id.Split(','); if (!int.TryParse(idPair[0], out var idWell)) throw new ArgumentInvalidException(nameof(id), $"Не удалось получить Id скважины \"{idPair[0]}\""); if (idPair.Length > 1) { if (int.TryParse(idPair[1], out int idWellSectionType)) return (idWell, idWellSectionType); else throw new ArgumentInvalidException(nameof(id), $"Не удалось получить Id ствола \"{idPair[1]}\""); } return (idWell, null); } }