using AsbCloudApp.Data.Trajectory;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.Trajectory.Export;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers.Trajectory
{
///
/// Плановые и фактические траектории (загрузка и хранение)
///
[ApiController]
[Authorize]
public abstract class TrajectoryController : ControllerBase
where Tdto : TrajectoryGeoDto
{
protected abstract string fileName { get; set; }
private readonly IWellService wellService;
private readonly TrajectoryExportService trajectoryExportService;
private readonly ITrajectoryRepository trajectoryRepository;
public TrajectoryController(IWellService wellService,
TrajectoryExportService trajectoryExportService,
ITrajectoryRepository trajectoryRepository)
{
this.trajectoryExportService = trajectoryExportService;
this.wellService = wellService;
this.trajectoryRepository = trajectoryRepository;
}
///
/// Формируем 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 trajectoryExportService.ExportAsync(idWell, token);
var fileName = await trajectoryExportService.GetFileNameAsync(idWell, token);
return File(stream, "application/octet-stream", fileName);
}
///
/// Получаем список всех строк координат траектории (для клиента)
///
/// id скважины
/// Токен отмены задачи
/// Список добавленных координат траектории
[HttpGet]
//[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetAsync([FromRoute] int idWell, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))
return Forbid();
var result = await trajectoryRepository.GetAsync(idWell, token);
return Ok(result);
}
protected 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);
}
}
}