Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FilesController.cs 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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("filtered")]
  25. public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters));
  26. [HttpPost]
  27. public async Task<IActionResult> UploadFile([FromForm] CreateFileRequest request)
  28. {
  29. if (request == null) throw new BadHttpRequestException("Request cannot be null");
  30. var filePath = await Upload.SaveFile(_hostingEnvironment.ContentRootPath, request.FileToUpload, "files");
  31. Category category = null;
  32. if(request.CategoryId > 0)
  33. {
  34. category = await _categoryService.GetCategoryEntityById(request.CategoryId);
  35. if (category == null) throw new BadHttpRequestException("Category cannot found");
  36. }
  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.UploadFileAsync(new FileEntity { CategoryId = request.CategoryId > 0 ? request.CategoryId : null, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title});
  42. return Ok();
  43. }
  44. [HttpPut("update-note/{id}")]
  45. public async Task<IActionResult> UpdateNote([FromBody]UpdateFileNoteRequest request, Guid id)
  46. {
  47. await _fileEntityService.UpdateNoteAsync(id, request);
  48. return Ok();
  49. }
  50. [HttpDelete("delete-file/{id}")]
  51. public async Task<IActionResult> DeleteFile([FromRoute]Guid id)
  52. {
  53. await _fileEntityService.DeleteFileAsync(id);
  54. return Ok();
  55. }
  56. }
  57. }