| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using Azure.Core;
- using Dapper;
- using Diligent.WebAPI.Contracts.DTOs.Categories;
- using Diligent.WebAPI.Contracts.DTOs.Document;
- using Diligent.WebAPI.Contracts.DTOs.File;
- using Diligent.WebAPI.Contracts.DTOs.Files;
- using Diligent.WebAPI.Contracts.DTOs.Tags;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static System.Net.Mime.MediaTypeNames;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class FileEntityService : IFileEntityService
- {
- private readonly ILogger<FileEntityService> _logger;
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly IConfiguration _configuration;
-
- public FileEntityService(DatabaseContext context, ILogger<FileEntityService> logger, IMapper mapper, IConfiguration configuration)
- {
- _context = context;
- _logger = logger;
- _mapper = mapper;
- _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);
-
- 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"));
-
- var sql = @"SELECT Files.Id as FileId, Files.Deleted, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName, Categories.Id as CategoryId, Categories.Name as CategoryName
- FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId
- inner join Tags on FileEntityTag.TagsId = Tags.Id
- inner join Categories on Files.CategoryId = Categories.Id
- 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) =>
- {
- file.Tags.Add(tag);
- file.Category = category;
- return file;
- }, splitOn: "TagId, CategoryId",
- param:new {content = filters.Content});
-
- var filesList = files.ToList();
-
- var filtered = filesList
- .FilterFiles(filters)
- .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 }),
- Total = filtered.Count
- };
- }
-
- public async Task<FileEntity> GetFileEntityByIdAsync(Guid id)
- {
- var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync();
-
- if (file is null)
- {
- throw new EntityNotFoundException("File not found");
- }
-
- return file;
- }
-
- public async Task DeleteFileAsync(Guid id)
- {
- var file = await GetFileEntityByIdAsync(id);
-
- file.Deleted= true;
-
- await _context.SaveChangesAsync();
- }
- }
- }
|