DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/DrillingProgramController.cs

251 lines
10 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
2021-08-29 17:25:16 +05:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
2021-08-29 17:25:16 +05:00
using Microsoft.AspNetCore.Mvc;
using System;
2021-08-29 17:25:16 +05:00
using System.IO;
using System.Linq;
2021-08-29 17:25:16 +05:00
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
2021-08-29 17:29:24 +05:00
[Route("api/well/{idWell}/drillingProgram")]
2021-08-29 17:25:16 +05:00
[ApiController]
[Authorize]
public class DrillingProgramController : ControllerBase
{
private readonly IDrillingProgramService drillingProgramService;
private readonly IFileService fileService;
private readonly IWellService wellService;
2021-08-29 17:25:16 +05:00
public DrillingProgramController(IDrillingProgramService drillingProgramService,
IFileService fileService, IWellService wellService)
2021-08-29 17:25:16 +05:00
{
this.drillingProgramService = drillingProgramService;
this.fileService = fileService;
this.wellService = wellService;
2021-08-29 17:25:16 +05:00
}
/// <summary>
/// Список категорий файлов из которых состоит программа бурения
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("categories")]
[ProducesResponseType(typeof(FileCategoryDto[]), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetCategories(CancellationToken token)
{
var result = await drillingProgramService.GetCategoriesAsync(token);
return Ok(result);
}
/// <summary>
/// Состояние процесса согласования программы бурения
/// </summary>
/// <param name="idWell"> id скважины </param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns> Возвращает файл программы бурения </returns>
2021-08-29 17:25:16 +05:00
[HttpGet]
[Permission]
[ProducesResponseType(typeof(DrillingProgramStateDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStateAsync(int idWell, CancellationToken token)
2021-08-29 17:25:16 +05:00
{
var idCompany = User.GetCompanyId();
var idUser = User.GetUserId();
if (idCompany is null || idUser is null ||
!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = await drillingProgramService.GetStateAsync(idWell, (int)idUser, token);
return Ok(result);
}
/// <summary>
/// Загрузка файла программы бурения
/// </summary>
/// <param name="idPart">ID части программы. Не путать с категорией файла</param>
/// <param name="idWell"></param>
/// <param name="files"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("{idPart}")]
[Permission]
[ProducesResponseType(typeof(Task<int>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddFile(
int idPart,
int idWell,
[FromForm] IFormFileCollection files,
CancellationToken token)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
if (files.Count > 1)
return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(files), "only 1 file can be uploaded"));
if (files.Count == 0)
return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(files), "at list 1 file can be uploaded"));
var fileName = files[0].FileName;
var fileStream = files[0].OpenReadStream();
var result = await drillingProgramService.AddFile(idPart, (int)idUser, fileName, fileStream, token);
return Ok(result);
}
2021-11-17 13:06:48 +05:00
/// <summary>
/// Добавить раздел программы бурения
/// </summary>
/// <param name="idWell"></param>
/// <param name="idFileCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("part")]
[Permission]
[ProducesResponseType(typeof(Task<int>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddPartAsync(int idWell, int idFileCategory, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = drillingProgramService.AddPartAsync(idWell, idFileCategory, token);
return Ok(result);
}
/// <summary>
/// Удалить раздел программы бурения
/// </summary>
/// <param name="idWell"></param>
/// <param name="idFileCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("part")]
[Permission]
[ProducesResponseType(typeof(Task<int>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> RemovePartAsync(int idWell, int idFileCategory, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = drillingProgramService.RemovePartAsync(idWell, idFileCategory, token);
return Ok(result);
}
[HttpPost("part/{idPart}/user/{idUser}")]
[Permission]
[ProducesResponseType(typeof(Task<int>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> AddUserAsync(int idWell, int idUser, int idPart, int idUserRole, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUserEditor = User.GetUserId();
if (idCompany is null || idUserEditor is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = drillingProgramService.AddUserAsync(idUser, idPart, idUserRole, token);
return Ok(result);
}
[HttpDelete("part/{idPart}/user/{idUser}")]
[Permission]
[ProducesResponseType(typeof(Task<int>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> RemoveUserAsync(int idWell, int idUser, int idPart, int idUserRole, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUserEditor = User.GetUserId();
if (idCompany is null || idUserEditor is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = drillingProgramService.RemoveUserAsync(idUser, idPart, idUserRole, token);
return Ok(result);
}
/// <summary>
/// Создает метку для файла входящего в программу бурения, заменить существующую
/// </summary>
/// <param name="idWell"> id скважины </param>
/// <param name="markDto">метка файла</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns></returns>
[HttpPost("fileMark")]
[Permission]
public async Task<IActionResult> AddOrReplaceFileMarkAsync(int idWell, FileMarkDto markDto, CancellationToken token)
{
var idCompany = User.GetCompanyId();
var idUser = User.GetUserId();
if (idCompany is null || idUser is null ||
!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = await drillingProgramService.AddOrReplaceFileMarkAsync(markDto, (int)idUser, 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")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> MarkAsDeletedFileMarkAsync(int idWell, int idMark,
CancellationToken token)
{
var idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
var result = await drillingProgramService.MarkAsDeletedFileMarkAsync(idMark, token)
.ConfigureAwait(false);
return Ok(result);
}
2021-08-29 17:25:16 +05:00
}
}