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}/sections")] [ApiController] [Authorize] public class WellSectionController : ControllerBase { private readonly IWellSectionService sectionsService; private readonly IWellService wellService; public WellSectionController(IWellSectionService sectionsService, IWellService wellService) { this.sectionsService = sectionsService; this.wellService = wellService; } [HttpGet] [Route("types")] [ProducesResponseType(typeof(string[]), (int)System.Net.HttpStatusCode.OK)] public async Task GetTypesAsync(CancellationToken token = default) { var result = await sectionsService.GetTypesAsync(token).ConfigureAwait(false); return Ok(result); } [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 sectionsService.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false); return Ok(result); } [HttpGet] [Route("{idSection}")] [ProducesResponseType(typeof(WellSectionDto), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync(int idWell, int idSection, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await sectionsService.GetAsync(idSection, token).ConfigureAwait(false); return Ok(result); } [HttpPost] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task InsertAsync(int idWell, [FromBody] IEnumerable values, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await sectionsService.InsertRangeAsync(idWell, values, token).ConfigureAwait(false); return Ok(result); } [HttpPut("{idSection}")] [ProducesResponseType(typeof(WellSectionDto), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateAsync(int idWell, int idSection, [FromBody] WellSectionDto value, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await sectionsService.UpdateAsync(idWell, idSection, value, token).ConfigureAwait(false); return Ok(result); } [HttpDelete("{idSection}")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task DeleteAsync(int idWell, int idItem, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await sectionsService.DeleteAsync(new int[] { idItem }, 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); } } }