| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using Diligent.WebAPI.Contracts.DTOs.File;
- 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 Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;
-
- public FilesController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IFileEntityService fileEntityService, ICategoryService categoryService, ITagService tagService)
- {
- _hostingEnvironment = hostingEnvironment;
- _fileEntityService = fileEntityService;
- _categoryService = categoryService;
- _tagService = tagService;
- }
-
- [HttpGet]
- public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll());
-
- [HttpPost]
- public async Task<IActionResult> 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<Tag> tags = new();
- foreach (var id in request.TagsIds)
- tags.Add(await _tagService.GetTagEntityById(id));
-
-
- await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, Category = category, Tags = tags, Name = filePath, Extension = "pdf", Size = 100});
- return Ok();
- }
- }
- }
|