LinkedApiKeyRepository.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using AipDatabase.API.Infrastructures;
  2. using AipDatabase.API.Interfaces;
  3. using AipDatabase.API.Models;
  4. using Dapper;
  5. using Microsoft.Data.SqlClient;
  6. using System.Data;
  7. namespace AipDatabase.API.Repositories
  8. {
  9. public class LinkedApiKeyRepository : ILinkedApiKeyRepository
  10. {
  11. private readonly ILogger<LinkedApiKeyRepository> _log;
  12. private readonly IDatabaseFactory _databaseFactory;
  13. private readonly SqlHelper<LinkedApiKeyRepository> _sqlHelper;
  14. public LinkedApiKeyRepository(ILogger<LinkedApiKeyRepository> log, IDatabaseFactory databaseFactory)
  15. {
  16. _log = log;
  17. _databaseFactory = databaseFactory;
  18. _sqlHelper = new SqlHelper<LinkedApiKeyRepository>();
  19. }
  20. public async Task<Pagination<LinkedApiKey>> GetLists(int pagePerCount, int pageSize, int page, int systemId)
  21. {
  22. try
  23. {
  24. using var connection = _databaseFactory.GetDbConnection();
  25. var parameters1 = new
  26. {
  27. systemId
  28. };
  29. string sql = _sqlHelper.GetSqlFromEmbeddedResource("LinkedApiKey.GetListsTotal");
  30. Int64 totalCount = await connection.QueryFirstAsync<Int64>(
  31. sql,
  32. parameters1,
  33. commandType: CommandType.Text
  34. );
  35. _log.LogInformation($"LinkedApiKey.GetListsTotal: {totalCount} EA.");
  36. Pagination<LinkedApiKey> result = new Pagination<LinkedApiKey>(page, pageSize, pagePerCount, (int)totalCount);
  37. var parameters2 = new
  38. {
  39. systemId,
  40. startRow = result.startRow,
  41. endRow = result.endRow
  42. };
  43. sql = _sqlHelper.GetSqlFromEmbeddedResource("LinkedApiKey.GetLists");
  44. IEnumerable<LinkedApiKey> Results = await connection.QueryAsync<LinkedApiKey>(
  45. sql,
  46. parameters2,
  47. commandType: CommandType.Text
  48. );
  49. result.lists = Results.ToList();
  50. return result;
  51. }
  52. catch (Exception)
  53. {
  54. throw;
  55. }
  56. }
  57. }
  58. }