Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FilesController.cs 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Diligent.WebAPI.Contracts.DTOs.File;
  2. using Diligent.WebAPI.Contracts.DTOs.Files;
  3. using Diligent.WebAPI.Host.Methods;
  4. namespace Diligent.WebAPI.Host.Controllers.V1
  5. {
  6. [ApiVersion("1.0")]
  7. [Route("v{version:apiVersion}/files")]
  8. [ApiController]
  9. public class FilesController : ControllerBase
  10. {
  11. private readonly ICategoryService _categoryService;
  12. private readonly ITagService _tagService;
  13. private readonly IFileEntityService _fileEntityService;
  14. private readonly IDocumentService _documentService;
  15. private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;
  16. public FilesController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IFileEntityService fileEntityService, ICategoryService categoryService, ITagService tagService, IDocumentService documentService)
  17. {
  18. _hostingEnvironment = hostingEnvironment;
  19. _fileEntityService = fileEntityService;
  20. _categoryService = categoryService;
  21. _tagService = tagService;
  22. _documentService = documentService;
  23. }
  24. [HttpGet]
  25. public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll());
  26. [HttpGet("filterByContent")]
  27. public async Task<IActionResult> GetAllDocumentsByContent(string content) => Ok(await _fileEntityService.GetAllFilesBasedOnContent(content));
  28. [HttpGet("filtered")]
  29. public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters));
  30. [HttpPost]
  31. public async Task<IActionResult> UploadPdf([FromForm] CreateFileRequest request)
  32. {
  33. if (request == null) throw new BadHttpRequestException("Request cannot be null");
  34. var filePath = await Upload.SaveFile(_hostingEnvironment.ContentRootPath, request.FileToUpload, "files");
  35. var category = await _categoryService.GetCategoryEntityById(request.CategoryId);
  36. if (category == null) throw new BadHttpRequestException("Category cannot found");
  37. List<Tag> tags = new();
  38. foreach (var id in request.TagsIds)
  39. tags.Add(await _tagService.GetTagEntityById(id));
  40. var file = await _documentService.UploadDocument(request.FileToUpload);
  41. await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title});
  42. return Ok();
  43. }
  44. }
  45. }