using AsbCloudApp.Data;
using AsbCloudApp.Data.AutogeneratedDailyReport;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers;
///
/// Контроллер для drill_test отчётов
///
[ApiController]
[Route("api/well/{idWell}/[controller]")]
[Authorize]
public class DrillTestReportController : ControllerBase
{
private readonly IDrillTestReportService drillTestReportService;
private readonly IWellService wellService;
public DrillTestReportController(IDrillTestReportService drillTestReportService,
IWellService wellService)
{
this.drillTestReportService = drillTestReportService;
this.wellService = wellService;
}
///
/// Формирование отчёта
///
/// Id скважины
/// Ключ entity test записи
///
///
[HttpGet]
[ProducesResponseType(typeof(PhysicalFileResult), (int)HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task GenerateReportAsync([FromRoute] int idWell,
[FromQuery] int id,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reportFile = await drillTestReportService.GenerateAsync(idWell, id, cancellationToken);
return File(reportFile.stream, "application/octet-stream", reportFile.fileName);
}
///
/// Список файлов drill test отчётов
///
/// Id скважины
/// Параметры запроса
///
///
[HttpGet("all")]
[ProducesResponseType(typeof(PaginationContainer), (int)HttpStatusCode.OK)]
public async Task GetListAsync([FromRoute][Required] int idWell,
[FromQuery] FileReportRequest request,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reports = await drillTestReportService.GetListAsync(idWell,
request,
cancellationToken);
return Ok(reports);
}
private async Task CanUserAccessToWellAsync(int idWell, CancellationToken cancellationToken)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, cancellationToken).ConfigureAwait(false);
}
}