using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; namespace AsbCloudWebApi.Controllers { /// /// Контроллер операций на скважине /// [Route("api/wellOperation")] [ApiController] [Authorize] public class WellOperationController : ControllerBase { private readonly IWellOperationService operationService; private readonly IWellService wellService; public WellOperationController(IWellOperationService operationService, IWellService wellService) { this.operationService = operationService; this.wellService = wellService; } [HttpGet] [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] public async Task GetAllAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await operationService.GetAllByWellIdAsync(idWell, skip, take, 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); } } }