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

251 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;
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="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>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(DrillingProgramStateDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStateAsync(int idWell, 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.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);
}
/// <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);
}
}
}