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.

FilesController.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. var category = await _categoryService.GetCategoryEntityById(request.CategoryId);
  32. if (category == null) throw new BadHttpRequestException("Category cannot found");
  33. List<Tag> tags = new();
  34. foreach (var id in request.TagsIds)
  35. tags.Add(await _tagService.GetTagEntityById(id));
  36. var file = await _documentService.UploadDocument(request.FileToUpload);
  37. await _fileEntityService.UploadFileAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title,Note = request.Note});
  38. return Ok();
  39. }
  40. [HttpDelete("delete-file/{id}")]
  41. public async Task<IActionResult> DeleteFile([FromRoute]Guid id)
  42. {
  43. await _fileEntityService.DeleteFileAsync(id);
  44. return Ok();
  45. }
  46. }
  47. }