| { | { | ||||
| if (values == null || values.Length == 0) | if (values == null || values.Length == 0) | ||||
| return query; | 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) | public static List<FileFilterReturnDto> FilterByCategory(this List<FileFilterReturnDto> query, string[]? values) | ||||
| { | { |
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | 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 | 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"; | 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(); | var categoriesList = categories.ToList(); | ||||
| return categoriesList; | return categoriesList; | ||||
| } | } |
| { | { | ||||
| _configuration = configuration; | _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) | public async Task<DocumentReadDTO> UploadDocument(IFormFile file) | ||||
| { | { | ||||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); |
| _configuration = configuration; | _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) | public async Task UploadPdfAsync(FileEntity file) | ||||
| { | { | ||||
| await _context.Files.AddAsync(file); | await _context.Files.AddAsync(file); | ||||
| await _context.SaveChangesAsync(); | 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) | public async Task<object> GetAllFiltered(FileFilter filters) | ||||
| { | { | ||||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | ||||
| inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" + | inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" + | ||||
| (filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);"); | (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.Tags.Add(tag); | ||||
| file.Category = category; | file.Category = category; | ||||
| }, splitOn: "TagId, CategoryId", | }, splitOn: "TagId, CategoryId", | ||||
| param:new {content = filters.Content}); | 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 | var filtered = filesList | ||||
| .FilterFiles(filters) | .FilterFiles(filters) | ||||
| .DistinctBy(x => x.stream_id).ToList(); | |||||
| .DistinctBy(x => x.Stream_id).ToList(); | |||||
| return new | return new | ||||
| { | { | ||||
| Data = filtered.ApplyPagging(filters) | 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 | 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) | public async Task<FileEntity> GetFileEntityByIdAsync(Guid id) | ||||
| { | { | ||||
| var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync(); | var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync(); |
| { | { | ||||
| public interface IDocumentService | public interface IDocumentService | ||||
| { | { | ||||
| Task<List<DocumentReadDTO>> GetAllDocuments(); | |||||
| Task<List<DocumentReadDTO>> GetDocumentsByText(string text); | |||||
| Task<DocumentReadDTO> UploadDocument(IFormFile file); | Task<DocumentReadDTO> UploadDocument(IFormFile file); | ||||
| } | } | ||||
| } | } |
| { | { | ||||
| public interface IFileEntityService | public interface IFileEntityService | ||||
| { | { | ||||
| Task<List<FileEntityResponse>> GetAllAsync(); | |||||
| Task UploadPdfAsync(FileEntity file); | Task UploadPdfAsync(FileEntity file); | ||||
| Task<IEnumerable<FileEntity>> GetAll(); | |||||
| Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content); | |||||
| Task<object> GetAllFiltered(FileFilter filters); | Task<object> GetAllFiltered(FileFilter filters); | ||||
| Task<FileEntity> GetFileEntityByIdAsync(Guid id); | Task<FileEntity> GetFileEntityByIdAsync(Guid id); | ||||
| Task DeleteFileAsync(Guid id); | Task DeleteFileAsync(Guid id); |
| var tags = await _databaseContext.Tags.ToArrayAsync(); | var tags = await _databaseContext.Tags.ToArrayAsync(); | ||||
| using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); | 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 | return new FileFiltersReturnDTO | ||||
| { | { |
| 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; } | |||||
| } | |||||
| } |
| { | { | ||||
| public class FileFilterReturnDto | public class FileFilterReturnDto | ||||
| { | { | ||||
| public Guid stream_id { get; set; } | |||||
| public Guid Stream_id { get; set; } | |||||
| public string FileName { get; set; } | public string FileName { get; set; } | ||||
| public string Title { 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 List<TagResponse> Tags { get; set; } = new(); | ||||
| public CategoryResponse Category { get; set; } | public CategoryResponse Category { get; set; } | ||||
| public bool Deleted { get; set; } | public bool Deleted { get; set; } |
| 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(); | |||||
| } | |||||
| } | |||||
| } |
| _documentService = documentService; | _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")] | [HttpGet("filtered")] | ||||
| public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters)); | public async Task<IActionResult> GetAllFiltered([FromQuery] FileFilter filters) => Ok(await _fileEntityService.GetAllFiltered(filters)); | ||||