using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMaps; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers; /// /// Композитная скважина /// [Route("api/well/{idWell}/composite")] [ApiController] [Authorize] public class WellCompositeController : ControllerBase { private readonly IWellCompositeRepository wellCompositeRepository; private readonly IWellService wellService; public WellCompositeController(IWellCompositeRepository wellCompositeRepository, IWellService wellService) { this.wellCompositeRepository = wellCompositeRepository; this.wellService = wellService; } /// /// Получает композитную скважину для скважины /// /// id скважины для которой собрана композитная скважина /// /// [HttpGet] [Permission] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync(int idWell, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellCompositeRepository.GetAsync(idWell, token).ConfigureAwait(false); return Ok(result); } /// /// Создает или заменяет композитную скважину для скважины с idWell /// /// id скважины для которой собрана композитная скважина /// Секции композитной скважины /// /// [HttpPost] [Permission] public async Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellCompositeRepository.SaveAsync(idWell, wellComposites, token).ConfigureAwait(false); return Ok(result); } /// /// Получение РТК по композитной скважине /// /// /// /// [HttpGet("compositeProcessMap")] [Permission] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetCompositeProcessMap(int idWell, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellCompositeRepository.GetCompositeProcessMap(idWell, token).ConfigureAwait(false); return Ok(result); } private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token) { int? idCompany = User.GetCompanyId(); return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); } }