forked from ddrilling/AsbCloudServer
126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
|
using AsbCloudApp.Services;
|
|||
|
using Microsoft.AspNetCore.Http;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.IO;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using AsbCloudApp.Data;
|
|||
|
using AsbCloudApp.Repositories;
|
|||
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
|||
|
namespace AsbCloudWebApi.Controllers;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Справки по страницам
|
|||
|
/// </summary>
|
|||
|
[Route("api/[controller]")]
|
|||
|
[ApiController]
|
|||
|
[Authorize]
|
|||
|
public class HelpPageController : ControllerBase
|
|||
|
{
|
|||
|
private readonly IHelpPageService helpPageService;
|
|||
|
private readonly IUserRepository userRepository;
|
|||
|
|
|||
|
public HelpPageController(IHelpPageService helpPageService,
|
|||
|
IUserRepository userRepository)
|
|||
|
{
|
|||
|
this.helpPageService = helpPageService;
|
|||
|
this.userRepository = userRepository;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создание файла справки
|
|||
|
/// </summary>
|
|||
|
/// <param name="urlPage">Url страницы для которой предназначена эта справка</param>
|
|||
|
/// <param name="idCategory">Id катагории файла</param>
|
|||
|
/// <param name="file">Загружаемый файл</param>
|
|||
|
/// <returns>Id созданной справки</returns>
|
|||
|
[HttpPost]
|
|||
|
[Permission]
|
|||
|
[Route("saveFile")]
|
|||
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|||
|
public async Task<IActionResult> CreateAsync(string urlPage,
|
|||
|
int idCategory,
|
|||
|
[Required] IFormFile file)
|
|||
|
{
|
|||
|
int? idUser = User.GetUserId();
|
|||
|
|
|||
|
if(!idUser.HasValue)
|
|||
|
return Forbid();
|
|||
|
|
|||
|
if (!userRepository.HasPermission(idUser.Value, $"HelpPage.create"))
|
|||
|
return Forbid();
|
|||
|
|
|||
|
var helpPage = await helpPageService.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage,
|
|||
|
idCategory,
|
|||
|
CancellationToken.None);
|
|||
|
|
|||
|
using var fileStream = file.OpenReadStream();
|
|||
|
|
|||
|
if (helpPage is not null)
|
|||
|
{
|
|||
|
await helpPageService.UpdateAsync(helpPage,
|
|||
|
idCategory,
|
|||
|
file.FileName,
|
|||
|
fileStream,
|
|||
|
CancellationToken.None);
|
|||
|
|
|||
|
return Ok(helpPage.Id);
|
|||
|
}
|
|||
|
|
|||
|
int helpPageId = await helpPageService.CreateAsync(urlPage,
|
|||
|
idCategory,
|
|||
|
file.FileName,
|
|||
|
fileStream,
|
|||
|
CancellationToken.None);
|
|||
|
|
|||
|
return Ok(helpPageId);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение файла справки
|
|||
|
/// </summary>
|
|||
|
/// <param name="id">Id справки</param>
|
|||
|
/// <returns>Файл</returns>
|
|||
|
[HttpGet]
|
|||
|
[Route("getById/{id}")]
|
|||
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
|||
|
public async Task<IActionResult> GetFileAsync(int id)
|
|||
|
{
|
|||
|
var helpPage = await helpPageService.GetOrDefaultByIdAsync(id,
|
|||
|
CancellationToken.None);
|
|||
|
|
|||
|
if (helpPage is null)
|
|||
|
return NotFound();
|
|||
|
|
|||
|
using var fileStream = helpPageService.GetFileStream(helpPage);
|
|||
|
|
|||
|
var memoryStream = new MemoryStream();
|
|||
|
await fileStream.CopyToAsync(memoryStream,
|
|||
|
CancellationToken.None);
|
|||
|
memoryStream.Position = 0;
|
|||
|
|
|||
|
return File(memoryStream, "application/octet-stream", helpPage.Name);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение информации о справке
|
|||
|
/// </summary>
|
|||
|
/// <param name="urlPage">Url страницы</param>
|
|||
|
/// <param name="idCategory">Id категории</param>
|
|||
|
/// <returns>Dto справки</returns>
|
|||
|
[HttpGet]
|
|||
|
[Route("getByUrlPage/{urlPage}/{idCategory}")]
|
|||
|
[ProducesResponseType(typeof(HelpPageDto), (int)System.Net.HttpStatusCode.OK)]
|
|||
|
public async Task<IActionResult> GetByUrlPageAsync(string urlPage,
|
|||
|
int idCategory)
|
|||
|
{
|
|||
|
var helpPage = await helpPageService.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage,
|
|||
|
idCategory,
|
|||
|
CancellationToken.None);
|
|||
|
|
|||
|
return Ok(helpPage);
|
|||
|
}
|
|||
|
}
|