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 System.ComponentModel.DataAnnotations;
using AsbCloudApp.Repositories;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
///
/// Дело скважины
///
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class WellFinalDocumentsController : ControllerBase
{
private readonly IWellFinalDocumentsService wellFinalDocumentsService;
private readonly IWellService wellService;
private readonly IFileCategoryService fileCategoryService;
private readonly IWellFinalDocumentsRepository wellFinalDocumentsRepository;
public WellFinalDocumentsController(
IWellFinalDocumentsService wellFinalDocumentsService,
IWellService wellService,
IFileCategoryService fileCategoryService,
IWellFinalDocumentsRepository wellFinalDocumentsRepository)
{
this.wellFinalDocumentsService = wellFinalDocumentsService;
this.wellService = wellService;
this.fileCategoryService = fileCategoryService;
this.wellFinalDocumentsRepository = wellFinalDocumentsRepository;
}
///
/// Получение всех записей
///
///
///
///
[HttpGet("{idWell}")]
[Permission]
[ProducesResponseType(typeof(WellCaseDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetAsync(int idWell, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var idUser = User!.GetUserId()!;
var data = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, idUser.Value, token);
return Ok(data);
}
///
/// Получение списка ответственных
///
///
///
///
[HttpGet("{idWell}/availableUsers")]
[Permission]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetAvailableUsersAsync(int idWell, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await wellFinalDocumentsRepository.GetAvailableUsersAsync(idWell, token);
return Ok(data);
}
///
/// Обновление всех записей по скважине
///
///
///
///
///
[HttpPut("{idWell}")]
[Permission("WellFinalDocuments.editPublisher")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task UpdateRangeAsync(int idWell, [Required] IEnumerable dtos, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token);
return Ok(data);
}
///
/// Повторно оповестить ответственных за загрузку
///
///
///
///
/// количество оповещенных публикаторов
[HttpPut("{idWell}/reNotifyPublishers")]
[Permission("WellFinalDocuments.editPublisher")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task ReNotifyPublishersAsync(int idWell, [Required] int idCategory, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var idUser = User!.GetUserId()!;
var data = await wellFinalDocumentsService.ReNotifyPublishersAsync(idWell, idUser.Value, idCategory, token);
return Ok(data);
}
///
/// Получение истории файлов
///
///
///
///
///
[HttpGet("{idWell}/history")]
[Permission]
[ProducesResponseType(typeof(WellFinalDocumentsHistoryDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetFilesHistoryByIdCategoryAsync(int idWell,
[Required] int idCategory,
CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await this.wellFinalDocumentsService.GetFilesHistoryByIdCategoryAsync(idWell, idCategory, token);
return Ok(data);
}
///
/// Сохранение файла
///
///
///
///
///
///
[HttpPost("{idWell}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task SaveCategoryFileAsync(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var idUser = User!.GetUserId()!;
var fileStream = file.OpenReadStream();
var data = await this.wellFinalDocumentsService.SaveCategoryFileAsync(idWell, idCategory, idUser.Value, fileStream, file.FileName, token);
return Ok(data);
}
///
/// Получение справочника категорий файлов
///
///
[HttpGet]
[Route("wellCaseCategories")]
[Permission]
public async Task GetWellCaseCategoriesAsync(CancellationToken token = default)
{
var data = await fileCategoryService.GetWellCaseCategoriesAsync(token);
return Ok(data);
}
private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token )
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
#nullable disable
}