Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FileEntityService.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Azure.Core;
  2. using Dapper;
  3. using Diligent.WebAPI.Contracts.DTOs.Categories;
  4. using Diligent.WebAPI.Contracts.DTOs.Document;
  5. using Diligent.WebAPI.Contracts.DTOs.File;
  6. using Diligent.WebAPI.Contracts.DTOs.Files;
  7. using Diligent.WebAPI.Contracts.DTOs.Tags;
  8. using Microsoft.Extensions.Configuration;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data.SqlClient;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using static System.Net.Mime.MediaTypeNames;
  16. namespace Diligent.WebAPI.Business.Services
  17. {
  18. public class FileEntityService : IFileEntityService
  19. {
  20. private readonly ILogger<FileEntityService> _logger;
  21. private readonly DatabaseContext _context;
  22. private readonly IMapper _mapper;
  23. private readonly IConfiguration _configuration;
  24. public FileEntityService(DatabaseContext context, ILogger<FileEntityService> logger, IMapper mapper, IConfiguration configuration)
  25. {
  26. _context = context;
  27. _logger = logger;
  28. _mapper = mapper;
  29. _configuration = configuration;
  30. }
  31. public async Task<List<FileEntityResponse>> GetAllAsync() =>
  32. _mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).Where(x => x.Deleted == false).ToListAsync());
  33. public async Task UploadPdfAsync(FileEntity file)
  34. {
  35. await _context.Files.AddAsync(file);
  36. await _context.SaveChangesAsync();
  37. }
  38. public async Task<IEnumerable<FileEntity>> GetAll()
  39. {
  40. return await _context.Files.Where(x => x.Deleted == false).ToListAsync();
  41. }
  42. public async Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(String content)
  43. {
  44. using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
  45. var sql = @"select * from Files inner join DocumentOrganizerDocStore on Files.DocumentId = DocumentOrganizerDocStore.stream_id
  46. where contains(DocumentOrganizerDocStore.file_stream,@content) AND Files.Deleted = 0";
  47. var files = await connection.QueryAsync<FileEntity>(sql,new {content=content});
  48. return files.ToList();
  49. }
  50. public async Task<object> GetAllFiltered(FileFilter filters)
  51. {
  52. using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
  53. 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
  54. FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId
  55. inner join Tags on FileEntityTag.TagsId = Tags.Id
  56. inner join Categories on Files.CategoryId = Categories.Id
  57. inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" +
  58. (filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);");
  59. var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) =>
  60. {
  61. file.Tags.Add(tag);
  62. file.Category = category;
  63. return file;
  64. }, splitOn: "TagId, CategoryId",
  65. param:new {content = filters.Content});
  66. var filesList = files.ToList();
  67. var filtered = filesList
  68. .FilterFiles(filters)
  69. .DistinctBy(x => x.stream_id).ToList();
  70. return new
  71. {
  72. Data = filtered.ApplyPagging(filters)
  73. .Select(n => new { n.stream_id, n.FileName, n.cached_file_size, n.file_type, n.Title }),
  74. Total = filtered.Count
  75. };
  76. }
  77. public async Task<FileEntity> GetFileEntityByIdAsync(Guid id)
  78. {
  79. var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync();
  80. if (file is null)
  81. {
  82. throw new EntityNotFoundException("File not found");
  83. }
  84. return file;
  85. }
  86. public async Task DeleteFileAsync(Guid id)
  87. {
  88. var file = await GetFileEntityByIdAsync(id);
  89. file.Deleted= true;
  90. await _context.SaveChangesAsync();
  91. }
  92. }
  93. }