using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data.Manuals; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace AsbCloudWebApi.Controllers; [ApiController] [Route("api/[controller]")] public class ManualController : ControllerBase { private readonly IManualCatalogService manualCatalogService; private readonly IUserRepository userRepository; public ManualController(IManualCatalogService manualCatalogService, IUserRepository userRepository) { this.manualCatalogService = manualCatalogService; this.userRepository = userRepository; } /// /// Сохранение файла /// /// Необязательный параметр. 30000 - АСУ ТП, 30001 - Технология бурения /// Необязательный параметр. Id папки /// Загружаемый файл /// /// [HttpPost] [Permission] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task SaveFileAsync( [Range(minimum: 30000, maximum: 30001, ErrorMessage = "Категория файла недопустима. Допустимые: 30000, 30001")] int? idCategory, int? idFolder, [Required] IFormFile file, CancellationToken cancellationToken) { if(!CanUserAccess("Manual.edit")) return Forbid(); using var fileStream = file.OpenReadStream(); var id = await manualCatalogService.SaveFileAsync(idCategory, idFolder, file.FileName, fileStream, cancellationToken); return Ok(id); } /// /// Получение файла /// /// Id инструкции /// /// [HttpGet("{id:int}")] [Permission] [ProducesResponseType(typeof(PhysicalFileResult), StatusCodes.Status200OK, "application/octet-stream")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task GetFileAsync(int id, CancellationToken cancellationToken) { if(!CanUserAccess("Manual.view")) return Forbid(); var file = await manualCatalogService.GetFileAsync(id, cancellationToken); if (!file.HasValue) return NoContent(); return File(file.Value.stream, "application/octet-stream", file.Value.fileName); } /// /// Удаление файла /// /// Id инструкции /// /// [HttpDelete] [Permission] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task DeleteFileAsync(int id, CancellationToken cancellationToken) { if(!CanUserAccess("Manual.edit")) return Forbid(); return Ok(await manualCatalogService.DeleteFileAsync(id, cancellationToken)); } /// /// Получение каталога с инструкциями /// /// /// [HttpGet] [Permission] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task GetCatalogAsync(CancellationToken cancellationToken) { if(!CanUserAccess("Manual.view")) return Forbid(); return Ok(await manualCatalogService.GetCatalogAsync(cancellationToken)); } private bool CanUserAccess(string permission) { var idUser = User.GetUserId(); return idUser.HasValue && userRepository.HasPermission(idUser.Value, permission); } }