| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using Diligent.WebAPI.Contracts.DTOs.File;
- using Diligent.WebAPI.Contracts.DTOs.Files;
- using Diligent.WebAPI.Host.Methods;
-
- namespace Diligent.WebAPI.Host.Controllers.V1
- {
- [ApiVersion("1.0")]
- [Route("v{version:apiVersion}/files")]
- [ApiController]
- public class FilesController : ControllerBase
- {
- private readonly ICategoryService _categoryService;
- private readonly ITagService _tagService;
- private readonly IFileEntityService _fileEntityService;
- private readonly IDocumentService _documentService;
- private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;
-
- public FilesController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IFileEntityService fileEntityService, ICategoryService categoryService, ITagService tagService, IDocumentService documentService)
- {
- _hostingEnvironment = hostingEnvironment;
- _fileEntityService = fileEntityService;
- _categoryService = categoryService;
- _tagService = tagService;
- _documentService = documentService;
- }
-
- [HttpGet("filtered")]
- public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters));
-
- [HttpPost]
- public async Task<IActionResult> UploadFile([FromForm] CreateFileRequest request)
- {
- if (request == null) throw new BadHttpRequestException("Request cannot be null");
- var filePath = await Upload.SaveFile(_hostingEnvironment.ContentRootPath, request.FileToUpload, "files");
-
- Category category = null;
-
- if(request.CategoryId > 0)
- {
- category = await _categoryService.GetCategoryEntityById(request.CategoryId);
-
- if (category == null) throw new BadHttpRequestException("Category cannot found");
- }
-
-
- List<Tag> tags = new();
- foreach (var id in request.TagsIds)
- tags.Add(await _tagService.GetTagEntityById(id));
-
- var file = await _documentService.UploadDocument(request.FileToUpload);
-
- await _fileEntityService.UploadFileAsync(new FileEntity { CategoryId = request.CategoryId > 0 ? request.CategoryId : null, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title});
- return Ok();
- }
-
- [HttpPut("update-note/{id}")]
- public async Task<IActionResult> UpdateNote([FromBody]UpdateFileNoteRequest request, Guid id)
- {
- await _fileEntityService.UpdateNoteAsync(id, request);
- return Ok();
- }
-
- [HttpDelete("delete-file/{id}")]
- public async Task<IActionResult> DeleteFile([FromRoute]Guid id)
- {
- await _fileEntityService.DeleteFileAsync(id);
- return Ok();
- }
- }
- }
|