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; 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; public WellFinalDocumentsController( IWellFinalDocumentsService wellFinalDocumentsService, IWellService wellService, IFileCategoryService fileCategoryService) { this.wellFinalDocumentsService = wellFinalDocumentsService; this.wellService = wellService; this.fileCategoryService = fileCategoryService; } /// /// Получение всех записей /// /// /// /// [HttpGet("{idWell}")] [Permission] [ProducesResponseType(typeof(WellCaseDto), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync(int idWell, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var idUser = User?.GetUserId(); var data = await this.wellFinalDocumentsService.GetByWellIdAsync(idWell, idUser ?? default, token); return Ok(data); } /// /// Получение списка ответственных /// /// /// /// [HttpGet("{idWell}/availableUsers")] [Permission] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task 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); } /// /// Добавление записи /// /// /// /// /// [HttpPut("{idWell}")] [Permission("WellFinalDocuments.editPublisher")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateRangeAsync(int idWell, [Required] IEnumerable dtos, CancellationToken token = default) { 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 = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var idUser = User.GetUserId() ?? -1; var data = await wellFinalDocumentsService.ReNotifyPublishersAsync(idWell, idUser, 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 = default) { 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 = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var idUser = User.GetUserId() ?? -1; var fileStream = file.OpenReadStream(); var data = await this.wellFinalDocumentsService.SaveCategoryFileAsync(idWell, idCategory, idUser, 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 = default) { int? idCompany = User.GetCompanyId(); return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); } } #nullable disable }