using AsbCloudApp.Data; 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 IWellCompositeService wellCompositeService; private readonly IWellService wellService; public WellCompositeController(IWellCompositeService wellCompositeService, IWellService wellService) { this.wellCompositeService = wellCompositeService; this.wellService = wellService; } /// /// Получает композитную скважину для скважины /// /// id скважины для которой собрана композитная скважина /// /// [HttpGet] [Permission] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync(int idWell, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellCompositeService.GetAsync(idWell, token).ConfigureAwait(false); return Ok(result); } /// /// Создает или заменяет композитную скважину для скважины с idWell /// /// id скважины для которой собрана композитная скважина /// Секции композитной скважины /// /// [HttpPost] [Permission] public async Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellCompositeService.SaveAsync(idWell, wellComposites, token).ConfigureAwait(false); return Ok(result); } private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); } } }