forked from ddrilling/AsbCloudServer
83 lines
3.6 KiB
C#
83 lines
3.6 KiB
C#
using AsbCloudApp.Data.Trajectory;
|
||
using AsbCloudApp.Repositories;
|
||
using AsbCloudApp.Requests;
|
||
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
|
||
{
|
||
|
||
/// <summary>
|
||
/// Плановые и фактические траектории (загрузка и хранение)
|
||
/// </summary>
|
||
[ApiController]
|
||
[Authorize]
|
||
public abstract class TrajectoryController<Tdto> : ControllerBase
|
||
where Tdto : TrajectoryGeoDto
|
||
{
|
||
protected abstract string fileName { get; set; }
|
||
|
||
private readonly IWellService wellService;
|
||
private readonly TrajectoryExportService<Tdto> trajectoryExportService;
|
||
private readonly ITrajectoryRepository<Tdto> trajectoryRepository;
|
||
|
||
public TrajectoryController(IWellService wellService,
|
||
TrajectoryExportService<Tdto> trajectoryExportService,
|
||
ITrajectoryRepository<Tdto> trajectoryRepository)
|
||
{
|
||
this.trajectoryExportService = trajectoryExportService;
|
||
this.wellService = wellService;
|
||
this.trajectoryRepository = trajectoryRepository;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Формируем excel файл с текущими строками траектории
|
||
/// </summary>
|
||
/// <param name="idWell">id скважины</param>
|
||
/// <param name="token"> Токен отмены задачи </param>
|
||
/// <returns>Запрашиваемый файл</returns>
|
||
[HttpGet("export")]
|
||
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK, "application/octet-stream")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
public async Task<IActionResult> 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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получаем список всех строк координат траектории (для клиента)
|
||
/// </summary>
|
||
/// <param name="request">параметры запроса</param>
|
||
/// <param name="token"> Токен отмены задачи </param>
|
||
/// <returns>Список добавленных координат траектории</returns>
|
||
[HttpGet("/api/[controller]")]
|
||
//[ProducesResponseType(typeof(IEnumerable<Tdto>), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> GetAsync([FromQuery] TrajectoryRequest request, CancellationToken token)
|
||
{
|
||
if (!await CanUserAccessToWellAsync(request.IdWell,
|
||
token).ConfigureAwait(false))
|
||
return Forbid();
|
||
var result = await trajectoryRepository.GetAsync(request, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
protected async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
|
||
{
|
||
int? idCompany = User.GetCompanyId();
|
||
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||
idWell, token).ConfigureAwait(false);
|
||
}
|
||
}
|
||
|
||
}
|