using System;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.AutogeneratedDailyReport;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.AutoGeneratedDailyReports;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers;
///
/// Контроллер для авто-генерируемых суточных отчётов
///
[ApiController]
[Route("api/well/{idWell}/[controller]")]
[Authorize]
public class AutoGeneratedDailyReportController : ControllerBase
{
private readonly IAutoGeneratedDailyReportService autoGeneratedDailyReportService;
private readonly IWellService wellService;
public AutoGeneratedDailyReportController(IAutoGeneratedDailyReportService autoGeneratedDailyReportService,
IWellService wellService)
{
this.autoGeneratedDailyReportService = autoGeneratedDailyReportService;
this.wellService = wellService;
}
///
/// Формирование отчёта
///
/// Id скважины
/// Дата отчёта
///
///
[HttpGet]
[ProducesResponseType(typeof(PhysicalFileResult), (int)HttpStatusCode.OK, "application/octet-stream")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task GenerateReportAsync([FromRoute] int idWell,
[Required] DateOnly reportDate,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reportFile = await autoGeneratedDailyReportService.GenerateReportAsync(idWell,
reportDate,
cancellationToken);
return File(reportFile.stream, reportFile.fileName);
}
///
/// Список файлов суточных отчётов
///
/// Id скважины
/// Параметры запроса
///
///
[HttpGet("all")]
[ProducesResponseType(typeof(PaginationContainer), (int)HttpStatusCode.OK)]
public async Task GetListAsync([FromRoute][Required] int idWell,
[FromQuery] AutoGeneratedDailyReportRequest request,
CancellationToken cancellationToken)
{
if (!await CanUserAccessToWellAsync(idWell, cancellationToken))
return Forbid();
var reports = await autoGeneratedDailyReportService.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);
}
}