Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocumentService.cs 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Dapper;
  2. using Diligent.WebAPI.Contracts.DTOs.Document;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Configuration;
  5. using System.Data.SqlClient;
  6. using static System.Net.Mime.MediaTypeNames;
  7. namespace Diligent.WebAPI.Business.Services
  8. {
  9. public class DocumentService : IDocumentService
  10. {
  11. private readonly IConfiguration _configuration;
  12. public DocumentService(IConfiguration configuration)
  13. {
  14. _configuration = configuration;
  15. }
  16. public async Task<List<DocumentReadDTO>> GetAllDocuments()
  17. {
  18. using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
  19. var files = await connection.QueryAsync<DocumentReadDTO>("select * from dbo.DocumentOrganizerDocStore");
  20. return files.ToList();
  21. }
  22. public async Task<List<DocumentReadDTO>> GetDocumentsByText(string text)
  23. {
  24. using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
  25. var files = await connection.QueryAsync<DocumentReadDTO>("select * from dbo.DocumentOrganizerDocStore where contains(file_stream, @text)",
  26. new { text = text });
  27. return files.ToList();
  28. }
  29. public async Task<DocumentReadDTO> UploadDocument(IFormFile file)
  30. {
  31. using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
  32. var ms = new MemoryStream();
  33. file.CopyTo(ms);
  34. var fileBytes = ms.ToArray();
  35. string fileName = string.Format(@"{0}" + $"{Path.GetExtension(file.FileName)}", DateTime.Now.Ticks);
  36. await connection.ExecuteAsync("insert into dbo.DocumentOrganizerDocStore (file_stream,name) values(@fileBytes,@fileName)",
  37. new { fileBytes = fileBytes, fileName = fileName });
  38. var fileByName = await connection.QueryFirstAsync<DocumentReadDTO>("select * from dbo.DocumentOrganizerDocStore where name=@fileName",
  39. new { fileName = fileName });
  40. return fileByName;
  41. }
  42. }
  43. }