2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Services;
|
2023-06-28 16:33:27 +05:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-06-29 16:31:37 +05:00
|
|
|
using System.Net;
|
2023-06-28 16:33:27 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Справки по страницам
|
|
|
|
/// </summary>
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
[ApiController]
|
|
|
|
[Authorize]
|
|
|
|
public class HelpPageController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly IHelpPageService helpPageService;
|
|
|
|
private readonly IUserRepository userRepository;
|
2023-07-14 12:39:02 +05:00
|
|
|
private readonly IHelpPageRepository helpPageRepository;
|
2023-06-28 16:33:27 +05:00
|
|
|
|
|
|
|
public HelpPageController(IHelpPageService helpPageService,
|
2023-07-14 12:39:02 +05:00
|
|
|
IUserRepository userRepository,
|
|
|
|
IHelpPageRepository helpPageRepository)
|
2023-06-28 16:33:27 +05:00
|
|
|
{
|
|
|
|
this.helpPageService = helpPageService;
|
|
|
|
this.userRepository = userRepository;
|
2023-07-14 12:39:02 +05:00
|
|
|
this.helpPageRepository = helpPageRepository;
|
2023-06-28 16:33:27 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-06-29 16:31:37 +05:00
|
|
|
/// Загрузка файла справки
|
2023-06-28 16:33:27 +05:00
|
|
|
/// </summary>
|
2023-07-18 16:51:55 +05:00
|
|
|
/// <param name="key">Ключ страницы</param>
|
2023-07-11 11:15:20 +05:00
|
|
|
/// <param name="idCategory">Id категории файла. Допустимое значение параметра: 20000</param>
|
2023-06-29 16:31:37 +05:00
|
|
|
/// <param name="file">Файл справки</param>
|
|
|
|
/// <param name="cancellationToken">Токен для отмены задачи</param>
|
|
|
|
/// <returns></returns>
|
2023-06-28 16:33:27 +05:00
|
|
|
[HttpPost]
|
|
|
|
[Permission]
|
2023-06-29 16:31:37 +05:00
|
|
|
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
|
2023-06-30 17:00:27 +05:00
|
|
|
public async Task<IActionResult> UploadAsync(
|
2023-07-18 16:38:49 +05:00
|
|
|
[Required] string key,
|
2023-06-30 17:00:27 +05:00
|
|
|
[Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")]
|
2023-06-28 16:33:27 +05:00
|
|
|
int idCategory,
|
2023-06-29 16:31:37 +05:00
|
|
|
[Required] IFormFile file,
|
|
|
|
CancellationToken cancellationToken)
|
2023-06-28 16:33:27 +05:00
|
|
|
{
|
|
|
|
int? idUser = User.GetUserId();
|
|
|
|
|
|
|
|
if(!idUser.HasValue)
|
|
|
|
return Forbid();
|
|
|
|
|
2023-06-29 16:31:37 +05:00
|
|
|
if (!userRepository.HasPermission(idUser.Value, $"HelpPage.edit"))
|
2023-06-28 16:33:27 +05:00
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
using var fileStream = file.OpenReadStream();
|
|
|
|
|
2023-07-18 16:38:49 +05:00
|
|
|
int helpPageId = await helpPageService.AddOrUpdateAsync(key,
|
2023-06-28 16:33:27 +05:00
|
|
|
idCategory,
|
|
|
|
file.FileName,
|
|
|
|
fileStream,
|
2023-06-29 16:31:37 +05:00
|
|
|
cancellationToken);
|
2023-06-28 16:33:27 +05:00
|
|
|
|
|
|
|
return Ok(helpPageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Получение файла справки
|
|
|
|
/// </summary>
|
2023-07-18 16:51:55 +05:00
|
|
|
/// <param name="key">Ключ страницы</param>
|
2023-07-11 13:39:57 +05:00
|
|
|
/// <param name="idCategory">Id категории файла. Допустимое значение параметра: 20000</param>
|
2023-06-29 16:31:37 +05:00
|
|
|
/// <param name="cancellationToken">Токен для отмены задачи</param>
|
|
|
|
/// <returns></returns>
|
2023-06-28 16:33:27 +05:00
|
|
|
[HttpGet]
|
2023-06-29 16:31:37 +05:00
|
|
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)HttpStatusCode.OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2023-07-14 12:39:02 +05:00
|
|
|
public async Task<IActionResult> GetFileAsync(
|
2023-07-18 16:38:49 +05:00
|
|
|
[Required] string key,
|
2023-07-14 12:39:02 +05:00
|
|
|
[Range(minimum: 20000, maximum: 20000, ErrorMessage = "Категория файла недопустима. Допустимые: 20000")]
|
|
|
|
int idCategory,
|
2023-06-29 16:31:37 +05:00
|
|
|
CancellationToken cancellationToken)
|
2023-06-28 16:33:27 +05:00
|
|
|
{
|
2023-07-18 16:38:49 +05:00
|
|
|
var file = await helpPageService.GetFileStreamAsync(key,
|
2023-06-29 16:31:37 +05:00
|
|
|
idCategory,
|
|
|
|
cancellationToken);
|
2023-06-28 16:33:27 +05:00
|
|
|
|
2023-07-26 17:53:25 +05:00
|
|
|
if (!file.HasValue)
|
2023-07-27 10:20:12 +05:00
|
|
|
return NoContent();
|
2023-06-28 16:33:27 +05:00
|
|
|
|
2023-07-26 17:53:25 +05:00
|
|
|
return File(file.Value.stream, "application/pdf", file.Value.fileName);
|
2023-06-28 16:33:27 +05:00
|
|
|
}
|
|
|
|
}
|