using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
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("aggregated")]
[ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)]
public async Task GetAggregatedWellByWellIdAsync(int idWell, int skip = 0, int take = 32,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await sectionsService.GetAggregatedWellByWellIdAsync(idWell, skip, take, token)
.ConfigureAwait(false);
return Ok(result);
}
[HttpGet]
[Route("divided")]
[ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)]
public async Task GetSectionsByWellIdAsync(int idWell, int skip = 0, int take = 32,
CancellationToken token = default)
{
if(!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await sectionsService.GetSectionsByWellIdAsync(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.GetSectionByWellIdAsync(idSection, 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);
}
}
}