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

158 lines
6.0 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using AsbCloudInfrastructure.Services;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
/// <summary>
/// Дело скважины
/// </summary>
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class WellFinalDocumentsController : ControllerBase
{
private readonly IWellFinalDocumentsService wellFinalDocumentsService;
private readonly IWellService wellService;
private readonly IFileCategoryService fileCategoryService;
public WellFinalDocumentsController(
IWellFinalDocumentsService wellFinalDocumentsService,
IWellService wellService,
IFileCategoryService fileCategoryService)
{
this.wellFinalDocumentsService = wellFinalDocumentsService;
this.wellService = wellService;
this.fileCategoryService = fileCategoryService;
}
/// <summary>
/// Получение всех записей
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(WellCaseDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var idUser = User?.GetUserId();
var data = await this.wellFinalDocumentsService.GetByWellId(idWell, idUser ?? default, token);
return Ok(data);
}
/// <summary>
/// Получение списка ответственных
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Permission]
[Route("availableUsers")]
[ProducesResponseType(typeof(IEnumerable<UserDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.GetAvailableUsersAsync(idWell, token);
return Ok(data);
}
/// <summary>
/// Добавление записи
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut]
[Permission("WellFinalDocuments.editPublisher")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token);
return Ok(data);
}
/// <summary>
/// Получение истории файлов
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Permission]
[Route("filesHistoryByIdCategory")]
[ProducesResponseType(typeof(WellFinalDocumentsHistoryDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFilesHistoryByIdCategory(int idWell,
int idCategory,
CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.GetFilesHistoryByIdCategory(idWell, idCategory, token);
return Ok(data);
}
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="file"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> SaveCategoryFile(int idWell, int idCategory, IFormFile file, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var idUser = User.GetUserId() ?? -1;
var fileStream = file.OpenReadStream();
var data = await this.wellFinalDocumentsService.SaveCategoryFile(idWell, idCategory, idUser, fileStream, file.FileName, token);
return Ok(data);
}
/// <summary>
/// Получение справочника категорий файлов
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("/api/wellFileCategories")]
[Permission]
public async Task<IActionResult> GetWellFileCategory(CancellationToken token = default)
{
var data = await fileCategoryService.GetWellCategoryAsync(token);
return Ok(data);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
#nullable disable
}