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] public async Task GetAll() => Ok(await _fileEntityService.GetAll()); [HttpGet("filterByContent")] public async Task GetAllDocumentsByContent(string content) => Ok(await _fileEntityService.GetAllFilesBasedOnContent(content)); [HttpGet("filtered")] public async Task GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters)); [HttpPost] public async Task UploadPdf([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.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title}); return Ok(); } } }