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 GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters)); [HttpPost] public async Task UploadFile([FromForm] CreateFileRequest request) { if (request == null) throw new BadHttpRequestException("Request cannot be null"); var filePath = await Upload.SaveFile(_hostingEnvironment.ContentRootPath, request.FileToUpload, "files"); var category = await _categoryService.GetCategoryEntityById(request.CategoryId); if (category == null) throw new BadHttpRequestException("Category cannot found"); List 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, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title,Note = request.Note}); return Ok(); } [HttpPut("update-note/{id}")] public async Task UpdateNote([FromBody]UpdateFileNoteRequest request, Guid id) { await _fileEntityService.UpdateNoteAsync(id, request); return Ok(); } [HttpDelete("delete-file/{id}")] public async Task DeleteFile([FromRoute]Guid id) { await _fileEntityService.DeleteFileAsync(id); return Ok(); } } }