using AsbCloudApp.Data.Trajectory; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Requests.ExportOptions; using AsbCloudInfrastructure.Services.Trajectory.Export; namespace AsbCloudWebApi.Controllers.Trajectory { /// /// Плановые и фактические траектории (загрузка и хранение) /// [ApiController] [Authorize] public abstract class TrajectoryController : ControllerBase where TDto : TrajectoryGeoDto { protected abstract string TemplateFileName { get; } private readonly IWellService wellService; private readonly TrajectoryExportService trajectoryExportService; private readonly ITrajectoryRepository trajectoryRepository; protected 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 exportOptions = new WellRelatedExportRequest(idWell); var (fileName, file) = await trajectoryExportService.ExportAsync(exportOptions, token); return File(file, "application/octet-stream", fileName); } /// /// Получаем список всех строк координат траектории (для клиента) /// /// ключ скважины /// Токен отмены задачи /// Список добавленных координат траектории [HttpGet] public async Task>> GetAsync(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); } } }