| @@ -9,7 +9,7 @@ namespace Diligent.WebAPI.Business.Extensions | |||
| { | |||
| if (values == null || values.Length == 0) | |||
| return query; | |||
| return query.Where(n => values.Contains(n.file_type)).ToList(); | |||
| return query.Where(n => values.Contains(n.File_type)).ToList(); | |||
| } | |||
| public static List<FileFilterReturnDto> FilterByCategory(this List<FileFilterReturnDto> query, string[]? values) | |||
| { | |||
| @@ -36,7 +36,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var sql = @"SELECT C.Id, C.Name FROM AspNetUsers AS U INNER JOIN UserCategories AS UC ON U.Id = UC.UserId | |||
| INNER JOIN Categories AS C ON UC.CategoryId = C.Id WHERE U.Id = @UserId"; | |||
| var categories = await connection.QueryAsync<CategoriesNamesResponse>(sql, new { UserId = 9 }); | |||
| var categories = await connection.QueryAsync<CategoriesNamesResponse>(sql, new { UserId = userId }); | |||
| var categoriesList = categories.ToList(); | |||
| return categoriesList; | |||
| } | |||
| @@ -15,21 +15,6 @@ namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| _configuration = configuration; | |||
| } | |||
| public async Task<List<DocumentReadDTO>> GetAllDocuments() | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var files = await connection.QueryAsync<DocumentReadDTO>("select * from dbo.DocumentOrganizerDocStore"); | |||
| return files.ToList(); | |||
| } | |||
| public async Task<List<DocumentReadDTO>> GetDocumentsByText(string text) | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var files = await connection.QueryAsync<DocumentReadDTO>("select * from dbo.DocumentOrganizerDocStore where contains(file_stream, @text)", | |||
| new { text = text }); | |||
| return files.ToList(); | |||
| } | |||
| public async Task<DocumentReadDTO> UploadDocument(IFormFile file) | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| @@ -31,9 +31,6 @@ namespace Diligent.WebAPI.Business.Services | |||
| _configuration = configuration; | |||
| } | |||
| public async Task<List<FileEntityResponse>> GetAllAsync() => | |||
| _mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).Where(x => x.Deleted == false).ToListAsync()); | |||
| public async Task UploadPdfAsync(FileEntity file) | |||
| { | |||
| await _context.Files.AddAsync(file); | |||
| @@ -41,21 +38,6 @@ namespace Diligent.WebAPI.Business.Services | |||
| await _context.SaveChangesAsync(); | |||
| } | |||
| public async Task<IEnumerable<FileEntity>> GetAll() | |||
| { | |||
| return await _context.Files.Where(x => x.Deleted == false).ToListAsync(); | |||
| } | |||
| public async Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(String content) | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var sql = @"select * from Files inner join DocumentOrganizerDocStore on Files.DocumentId = DocumentOrganizerDocStore.stream_id | |||
| where contains(DocumentOrganizerDocStore.file_stream,@content) AND Files.Deleted = 0"; | |||
| var files = await connection.QueryAsync<FileEntity>(sql,new {content=content}); | |||
| return files.ToList(); | |||
| } | |||
| public async Task<object> GetAllFiltered(FileFilter filters) | |||
| { | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| @@ -67,7 +49,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" + | |||
| (filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);"); | |||
| var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) => | |||
| var files = await connection.QueryAsync<FileFilterReturn, TagResponse, CategoryResponse, FileFilterReturn>(sql,(file, tag, category) => | |||
| { | |||
| file.Tags.Add(tag); | |||
| file.Category = category; | |||
| @@ -75,20 +57,50 @@ namespace Diligent.WebAPI.Business.Services | |||
| }, splitOn: "TagId, CategoryId", | |||
| param:new {content = filters.Content}); | |||
| var filesList = files.ToList(); | |||
| var filesList = new List<FileFilterReturnDto>(); | |||
| foreach (var fileItem in files.ToList()) | |||
| { | |||
| var fileFilterReturnDto = new FileFilterReturnDto | |||
| { | |||
| Stream_id = fileItem.Stream_id, | |||
| Cached_file_size = fileItem.Cached_file_size, | |||
| Category = fileItem.Category, | |||
| FileName = fileItem.FileName, | |||
| File_type = fileItem.File_type, | |||
| Tags = fileItem.Tags, | |||
| Title = fileItem.Title | |||
| }; | |||
| // return File_stream in base64 format | |||
| MemoryStream stream = new MemoryStream(fileItem.File_stream); | |||
| fileFilterReturnDto.File_stream = ConvertToBase64(stream); | |||
| filesList.Add(fileFilterReturnDto); | |||
| } | |||
| var filtered = filesList | |||
| .FilterFiles(filters) | |||
| .DistinctBy(x => x.stream_id).ToList(); | |||
| .DistinctBy(x => x.Stream_id).ToList(); | |||
| return new | |||
| { | |||
| Data = filtered.ApplyPagging(filters) | |||
| .Select(n => new { n.stream_id, n.FileName, n.cached_file_size, n.file_type, n.Title }), | |||
| .Select(n => new { n.Stream_id, n.FileName,n.File_stream, n.Cached_file_size, n.File_type, n.Title }), | |||
| Total = filtered.Count | |||
| }; | |||
| } | |||
| private static string ConvertToBase64(Stream stream) | |||
| { | |||
| byte[] bytes; | |||
| using (var memoryStream = new MemoryStream()) | |||
| { | |||
| stream.CopyTo(memoryStream); | |||
| bytes = memoryStream.ToArray(); | |||
| } | |||
| string base64 = Convert.ToBase64String(bytes); | |||
| return base64; | |||
| } | |||
| public async Task<FileEntity> GetFileEntityByIdAsync(Guid id) | |||
| { | |||
| var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync(); | |||
| @@ -5,8 +5,6 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| { | |||
| public interface IDocumentService | |||
| { | |||
| Task<List<DocumentReadDTO>> GetAllDocuments(); | |||
| Task<List<DocumentReadDTO>> GetDocumentsByText(string text); | |||
| Task<DocumentReadDTO> UploadDocument(IFormFile file); | |||
| } | |||
| } | |||
| @@ -10,10 +10,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| { | |||
| public interface IFileEntityService | |||
| { | |||
| Task<List<FileEntityResponse>> GetAllAsync(); | |||
| Task UploadPdfAsync(FileEntity file); | |||
| Task<IEnumerable<FileEntity>> GetAll(); | |||
| Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content); | |||
| Task<object> GetAllFiltered(FileFilter filters); | |||
| Task<FileEntity> GetFileEntityByIdAsync(Guid id); | |||
| Task DeleteFileAsync(Guid id); | |||
| @@ -28,9 +28,9 @@ namespace Diligent.WebAPI.Business.Services | |||
| var tags = await _databaseContext.Tags.ToArrayAsync(); | |||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | |||
| var files = await connection.QueryAsync<FileFilterReturnDto>("SELECT * FROM Files inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId;"); | |||
| var files = await connection.QueryAsync<FileFilterReturn>("SELECT * FROM Files inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId;"); | |||
| var extensionsArray = files.Select(x => x.file_type).Distinct().ToArray(); | |||
| var extensionsArray = files.Select(x => x.File_type).Distinct().ToArray(); | |||
| return new FileFiltersReturnDTO | |||
| { | |||
| @@ -0,0 +1,22 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||
| using Diligent.WebAPI.Contracts.DTOs.Tags; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Files | |||
| { | |||
| public class FileFilterReturn | |||
| { | |||
| public Guid Stream_id { get; set; } | |||
| public string FileName { get; set; } | |||
| public string Title { get; set; } | |||
| public string File_type { get; set; } | |||
| public int Cached_file_size { get; set; } | |||
| public byte[] File_stream { get; set; } | |||
| public List<TagResponse> Tags { get; set; } = new(); | |||
| public CategoryResponse Category { get; set; } | |||
| } | |||
| } | |||
| @@ -10,11 +10,12 @@ namespace Diligent.WebAPI.Contracts.DTOs.Files | |||
| { | |||
| public class FileFilterReturnDto | |||
| { | |||
| public Guid stream_id { get; set; } | |||
| public Guid Stream_id { get; set; } | |||
| public string FileName { get; set; } | |||
| public string Title { get; set; } | |||
| public string file_type { get; set; } | |||
| public int cached_file_size { get; set; } | |||
| public string File_type { get; set; } | |||
| public int Cached_file_size { get; set; } | |||
| public string File_stream { get; set; } | |||
| public List<TagResponse> Tags { get; set; } = new(); | |||
| public CategoryResponse Category { get; set; } | |||
| public bool Deleted { get; set; } | |||
| @@ -1,28 +0,0 @@ | |||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| { | |||
| [ApiVersion("1.0")] | |||
| [Route("v{version:apiVersion}/documents")] | |||
| [ApiController] | |||
| public class DocumentController:ControllerBase | |||
| { | |||
| private readonly IDocumentService _documentService; | |||
| public DocumentController(IDocumentService documentService) | |||
| { | |||
| _documentService = documentService; | |||
| } | |||
| [HttpGet] | |||
| public async Task<IActionResult> GetAllDocuments() => Ok(await _documentService.GetAllDocuments()); | |||
| [HttpGet("filter")] | |||
| public async Task<IActionResult> GetDocumentsByText(string text) => Ok(await _documentService.GetDocumentsByText(text)); | |||
| [HttpPost] | |||
| public async Task<IActionResult> UploadDocument(IFormFile file) | |||
| { | |||
| await _documentService.UploadDocument(file); | |||
| return Ok(); | |||
| } | |||
| } | |||
| } | |||
| @@ -24,12 +24,6 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| _documentService = documentService; | |||
| } | |||
| [HttpGet] | |||
| public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll()); | |||
| [HttpGet("filterByContent")] | |||
| public async Task<IActionResult> GetAllDocumentsByContent(string content) => Ok(await _fileEntityService.GetAllFilesBasedOnContent(content)); | |||
| [HttpGet("filtered")] | |||
| public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters)); | |||