forked from ddrilling/AsbCloudServer
94 lines
3.1 KiB
C#
94 lines
3.1 KiB
C#
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using AsbCloudApp.Repositories;
|
|||
|
using AsbCloudApp.Services;
|
|||
|
using Microsoft.AspNetCore.Http;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
namespace AsbCloudWebApi.Controllers;
|
|||
|
|
|||
|
[ApiController]
|
|||
|
[Route("api/[controller]")]
|
|||
|
public class ManualFolderController : ControllerBase
|
|||
|
{
|
|||
|
private readonly IManualCatalogService manualCatalogService;
|
|||
|
private readonly IUserRepository userRepository;
|
|||
|
|
|||
|
public ManualFolderController(IManualCatalogService manualCatalogService,
|
|||
|
IUserRepository userRepository)
|
|||
|
{
|
|||
|
this.manualCatalogService = manualCatalogService;
|
|||
|
this.userRepository = userRepository;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создание папки
|
|||
|
/// </summary>
|
|||
|
/// <param name="name">Название</param>
|
|||
|
/// <param name="idParent">Необязательный параметр. Id родительской папки</param>
|
|||
|
/// <param name="idCategory">Id категории. 30000 - АСУ ТП, 30001 - Технология бурения</param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost]
|
|||
|
[Permission]
|
|||
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|||
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|||
|
public async Task<IActionResult> AddFolderAsync(string name, int? idParent,
|
|||
|
[Required(ErrorMessage = "Обязательный параметр")]
|
|||
|
[Range(minimum: 30000, maximum: 30001, ErrorMessage = "Категория файла недопустима. Допустимые: 30000, 30001")]
|
|||
|
int idCategory,
|
|||
|
CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
if (!CanUserAccess())
|
|||
|
Forbid();
|
|||
|
|
|||
|
return Ok(await manualCatalogService.AddFolderAsync(name, idParent, idCategory, cancellationToken));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Обновление папки
|
|||
|
/// </summary>
|
|||
|
/// <param name="id"></param>
|
|||
|
/// <param name="name">Новое название папки</param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPut]
|
|||
|
[Permission]
|
|||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|||
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|||
|
public async Task<IActionResult> UpdateFolderAsync(int id, string name, CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
if (!CanUserAccess())
|
|||
|
Forbid();
|
|||
|
|
|||
|
await manualCatalogService.UpdateFolderAsync(id, name, cancellationToken);
|
|||
|
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление папки
|
|||
|
/// </summary>
|
|||
|
/// <param name="id"></param>
|
|||
|
/// <param name="cancellationToken"></param>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpDelete]
|
|||
|
[Permission]
|
|||
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|||
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|||
|
public async Task<IActionResult> DeleteFolderAsync(int id, CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
if (!CanUserAccess())
|
|||
|
Forbid();
|
|||
|
|
|||
|
return Ok(await manualCatalogService.DeleteFolderAsync(id, cancellationToken));
|
|||
|
}
|
|||
|
|
|||
|
private bool CanUserAccess()
|
|||
|
{
|
|||
|
var idUser = User.GetUserId();
|
|||
|
|
|||
|
return idUser.HasValue && userRepository.HasPermission(idUser.Value, "Manual.edit");
|
|||
|
}
|
|||
|
}
|