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;
}
///
/// Список категорий файлов из которых состоит программа бурения
///
///
///
[HttpGet("categories")]
[ProducesResponseType(typeof(FileCategoryDto[]), (int)System.Net.HttpStatusCode.OK)]
public async Task GetCategories(CancellationToken token)
{
var result = await drillingProgramService.GetCategoriesAsync(token);
return Ok(result);
}
///
/// Состояние процесса согласования программы бурения
///
/// id скважины
/// Токен отмены задачи
/// Возвращает файл программы бурения
[HttpGet]
[Permission]
[ProducesResponseType(typeof(DrillingProgramStateDto), (int)System.Net.HttpStatusCode.OK)]
public async Task 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);
}
///
/// Загрузка файла программы бурения
///
/// ID части программы. Не путать с категорией файла
///
///
///
///
[HttpPost("{idPart}")]
[Permission]
[ProducesResponseType(typeof(Task), (int)System.Net.HttpStatusCode.OK)]
public async Task 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);
}
///
/// Добавить раздел программы бурения
///
///
///
///
///
[HttpPost("part")]
[Permission]
[ProducesResponseType(typeof(Task), (int)System.Net.HttpStatusCode.OK)]
public async Task 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);
}
///
/// Удалить раздел программы бурения
///
///
///
///
///
[HttpPost("part")]
[Permission]
[ProducesResponseType(typeof(Task), (int)System.Net.HttpStatusCode.OK)]
public async Task 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)System.Net.HttpStatusCode.OK)]
public async Task 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)System.Net.HttpStatusCode.OK)]
public async Task 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);
}
///
/// Создает метку для файла входящего в программу бурения, заменить существующую
///
/// id скважины
/// метка файла
/// Токен отмены задачи
///
[HttpPost("fileMark")]
[Permission]
public async Task 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);
}
///
/// Помечает метку у файла входящего, в программу бурения, как удаленную
///
/// id скважины
/// id метки
/// Токен отмены задачи
///
[HttpDelete("fileMark")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task 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);
}
}
}