DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WellSectionController.cs
2021-08-20 14:17:53 +05:00

76 lines
2.9 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Контроллер секций скважины
/// </summary>
[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<WellOperationsDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> 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<WellSectionDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> 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<IActionResult> 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<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
}