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

181 lines
7.4 KiB
C#
Raw Normal View History

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;
2022-09-13 11:47:12 +05:00
using System.ComponentModel.DataAnnotations;
using AsbCloudApp.Repositories;
using AsbCloudApp.Data.User;
namespace AsbCloudWebApi.Controllers
{
2023-05-19 16:51:41 +05:00
/// <summary>
/// Дело скважины
/// </summary>
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class WellFinalDocumentsController : ControllerBase
{
private readonly IWellFinalDocumentsService wellFinalDocumentsService;
private readonly IWellService wellService;
2022-09-13 10:06:56 +05:00
private readonly IFileCategoryService fileCategoryService;
private readonly IWellFinalDocumentsRepository wellFinalDocumentsRepository;
2022-09-13 10:06:56 +05:00
public WellFinalDocumentsController(
IWellFinalDocumentsService wellFinalDocumentsService,
IWellService wellService,
IFileCategoryService fileCategoryService,
IWellFinalDocumentsRepository wellFinalDocumentsRepository)
{
this.wellFinalDocumentsService = wellFinalDocumentsService;
this.wellService = wellService;
2022-09-13 10:06:56 +05:00
this.fileCategoryService = fileCategoryService;
this.wellFinalDocumentsRepository = wellFinalDocumentsRepository;
}
/// <summary>
/// Получение всех записей
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-09-13 11:47:12 +05:00
[HttpGet("{idWell}")]
[Permission]
2022-09-12 12:05:19 +05:00
[ProducesResponseType(typeof(WellCaseDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2023-01-31 10:13:32 +05:00
var idUser = User!.GetUserId()!;
var data = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, idUser.Value, token);
return Ok(data);
}
/// <summary>
/// Получение списка ответственных
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-09-13 11:47:12 +05:00
[HttpGet("{idWell}/availableUsers")]
[Permission]
[ProducesResponseType(typeof(IEnumerable<UserDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var data = await wellFinalDocumentsRepository.GetAvailableUsersAsync(idWell, token);
return Ok(data);
}
/// <summary>
/// Обновление всех записей по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="dtos"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-09-13 11:47:12 +05:00
[HttpPut("{idWell}")]
2022-09-13 10:06:56 +05:00
[Permission("WellFinalDocuments.editPublisher")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateRangeAsync(int idWell, [Required] IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2023-01-31 10:13:32 +05:00
var data = await wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token);
2022-11-23 14:03:08 +05:00
return Ok(data);
}
/// <summary>
/// Повторно оповестить ответственных за загрузку
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="token"></param>
/// <returns>количество оповещенных публикаторов</returns>
2022-11-25 15:27:35 +05:00
[HttpPut("{idWell}/reNotifyPublishers")]
2022-11-23 14:03:08 +05:00
[Permission("WellFinalDocuments.editPublisher")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> ReNotifyPublishersAsync(int idWell, [Required] int idCategory, CancellationToken token )
2022-11-23 14:03:08 +05:00
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2023-01-31 10:13:32 +05:00
var idUser = User!.GetUserId()!;
var data = await wellFinalDocumentsService.ReNotifyPublishersAsync(idWell, idUser.Value, idCategory, token);
return Ok(data);
}
/// <summary>
/// Получение истории файлов
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-09-13 11:47:12 +05:00
[HttpGet("{idWell}/history")]
[Permission]
[ProducesResponseType(typeof(WellFinalDocumentsHistoryDto), (int)System.Net.HttpStatusCode.OK)]
2022-11-23 14:03:08 +05:00
public async Task<IActionResult> GetFilesHistoryByIdCategoryAsync(int idWell,
2022-09-13 11:47:12 +05:00
[Required] int idCategory,
CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2022-11-23 14:03:08 +05:00
var data = await this.wellFinalDocumentsService.GetFilesHistoryByIdCategoryAsync(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>
2022-09-13 11:47:12 +05:00
[HttpPost("{idWell}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> SaveCategoryFileAsync(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token )
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2023-01-31 10:13:32 +05:00
var idUser = User!.GetUserId()!;
var fileStream = file.OpenReadStream();
2023-01-31 10:13:32 +05:00
var data = await this.wellFinalDocumentsService.SaveCategoryFileAsync(idWell, idCategory, idUser.Value, fileStream, file.FileName, token);
return Ok(data);
}
2022-09-13 10:06:56 +05:00
/// <summary>
/// Получение справочника категорий файлов
/// </summary>
/// <returns></returns>
[HttpGet]
2022-09-13 12:43:22 +05:00
[Route("wellCaseCategories")]
2022-09-13 10:06:56 +05:00
[Permission]
public async Task<IActionResult> GetWellCaseCategoriesAsync(CancellationToken token)
2022-09-13 10:06:56 +05:00
{
2022-09-13 12:43:22 +05:00
var data = await fileCategoryService.GetWellCaseCategoriesAsync(token);
2022-09-13 10:06:56 +05:00
return Ok(data);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token )
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
2023-05-19 16:51:41 +05:00
}