using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Trajectory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers.Trajectory;
///
/// Фактическая траектория из ННБ
///
[Authorize]
[ApiController]
[Route("api/well/{idWell}/[controller]")]
public class NnbTrajectoryController : ControllerBase
{
private readonly ITrajectoryNnbRepository trajectoryNnbRepository;
private readonly NnbTrajectoryImportService factNnbTrajectoryImportService;
private readonly IWellService wellService;
public NnbTrajectoryController(
ITrajectoryNnbRepository trajectoryNnbRepository,
NnbTrajectoryImportService factNnbTrajectoryImportService,
IWellService wellService)
{
this.trajectoryNnbRepository = trajectoryNnbRepository;
this.factNnbTrajectoryImportService = factNnbTrajectoryImportService;
this.wellService = wellService;
this.wellService = wellService;
}
///
/// Метод получения всех строк фактических траекторий по id скважины из ННБ
///
/// Id скважины
/// Токен отмены операции
///
[HttpGet]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetRowsAsync([FromRoute] int idWell,
CancellationToken cancellationToken)
{
var nnbFactTrajectories = await trajectoryNnbRepository.GetAsync(idWell,
cancellationToken);
return Ok(nnbFactTrajectories);
}
///
/// Формируем excel файл с текущими строками фактической ннб-траектории
///
/// id скважины
/// Токен отмены задачи
/// Запрашиваемый файл
[HttpGet("export")]
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task ExportAsync([FromRoute] int idWell, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))
return Forbid();
var stream = await factNnbTrajectoryImportService.ExportAsync(idWell, token);
var fileName = await factNnbTrajectoryImportService.GetFileNameAsync(idWell, token);
return File(stream, "application/octet-stream", fileName);
}
private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}