Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FilesController.cs 1.9KB

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