You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. [HttpDelete("delete-file/{id}")]
  45. public async Task<IActionResult> DeleteFile([FromRoute]Guid id)
  46. {
  47. await _fileEntityService.DeleteFileAsync(id);
  48. return Ok();
  49. }
  50. }
  51. }