forked from ddrilling/AsbCloudServer
139 lines
5.8 KiB
C#
139 lines
5.8 KiB
C#
using AsbCloudApp.Data;
|
||
using AsbCloudApp.Services;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System.IO;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AsbCloudWebApi.Controllers
|
||
{
|
||
[Route("api/well/{idWell}/drillingProgram")]
|
||
[ApiController]
|
||
[Authorize]
|
||
public class DrillingProgramController : ControllerBase
|
||
{
|
||
private readonly IDrillingProgramService drillingProgramService;
|
||
private readonly IFileService fileService;
|
||
private readonly IWellService wellService;
|
||
|
||
public DrillingProgramController(IDrillingProgramService drillingProgramService,
|
||
IFileService fileService, IWellService wellService)
|
||
{
|
||
this.drillingProgramService = drillingProgramService;
|
||
this.fileService = fileService;
|
||
this.wellService = wellService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создает программу бурения
|
||
/// </summary>
|
||
/// <param name="idWell"> id скважины </param>
|
||
/// <param name="token"> Токен отмены задачи </param>
|
||
/// <returns> Возвращает файл программы бурения </returns>
|
||
[HttpGet]
|
||
[ProducesResponseType(typeof(FileResult), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
|
||
{
|
||
var idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||
idWell, token).ConfigureAwait(false))
|
||
return Forbid();
|
||
|
||
var fileInfo = await drillingProgramService.GetAsync(idWell, token)
|
||
.ConfigureAwait(false);
|
||
if (fileInfo is null)
|
||
return NoContent();
|
||
|
||
var relativePath = fileService.GetUrl(fileInfo);
|
||
|
||
return PhysicalFile(Path.GetFullPath(relativePath), "application/octet-stream", fileInfo.Name);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создает программу бурения
|
||
/// </summary>
|
||
/// <param name="idWell"> id скважины </param>
|
||
/// <param name="token"> Токен отмены задачи </param>
|
||
/// <returns> Возвращает ссылку на файл программы бурения в облаке </returns>
|
||
[HttpGet("webUrl")]
|
||
[ProducesResponseType(typeof(string), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> GetFileWebLinkAsync(int idWell, CancellationToken token = default)
|
||
{
|
||
var idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||
idWell, token).ConfigureAwait(false))
|
||
return Forbid();
|
||
|
||
var fileInfo = await drillingProgramService.GetAsync(idWell, token)
|
||
.ConfigureAwait(false);
|
||
|
||
if (fileInfo is null)
|
||
return NoContent();
|
||
|
||
var userLogin = User.Identity?.Name ?? "";
|
||
|
||
var fileWebUrl = await fileService.GetFileWebUrlAsync(fileInfo,
|
||
userLogin, token);
|
||
|
||
return Ok(fileWebUrl);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Регистрирует совершенные действия с частями программы бурения
|
||
/// </summary>
|
||
/// <param name="idWell"> id скважины </param>
|
||
/// <param name="idMark"> id действия </param>
|
||
/// <param name="idFile"> id файла </param>
|
||
/// <param name="comment"> Комментарий </param>
|
||
/// <param name="token"> Токен отмены задачи </param>
|
||
/// <returns></returns>
|
||
[HttpPost("fileMark")]
|
||
[ProducesResponseType(typeof(FileMarkDto), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> CreateFileMarkAsync(int idWell, int idMark,
|
||
int idFile, string comment, CancellationToken token = default)
|
||
{
|
||
var idCompany = User.GetCompanyId();
|
||
|
||
var fileChangerId = User.GetUserId();
|
||
var fileChangerLogin = User.Identity?.Name ?? "";
|
||
|
||
if (idCompany is null || fileChangerId is null ||
|
||
!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||
idWell, token).ConfigureAwait(false))
|
||
return Forbid();
|
||
|
||
var result = await fileService.CreateFileMarkAsync(idMark, idFile, comment,
|
||
(int)fileChangerId, fileChangerLogin, token)
|
||
.ConfigureAwait(false);
|
||
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаляет отметку о действии с файлом-частью программы бурения
|
||
/// </summary>
|
||
/// <param name="idWell"> id скважины </param>
|
||
/// <param name="idMark"> id действия </param>
|
||
/// <param name="token"> Токен отмены задачи </param>
|
||
/// <returns></returns>
|
||
[HttpDelete("fileMark")]
|
||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> DeleteFileMarkAsync(int idWell, int idMark,
|
||
CancellationToken token = default)
|
||
{
|
||
var idCompany = User.GetCompanyId();
|
||
|
||
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||
idWell, token).ConfigureAwait(false))
|
||
return Forbid();
|
||
|
||
var result = await fileService.MarkFileMarkAsDeletedAsync(idMark, token);
|
||
|
||
return Ok(result);
|
||
}
|
||
}
|
||
}
|