You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.6 KiB

2 years ago
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using BirdsiteLive.DAL.Contracts;
  5. using BirdsiteLive.DAL.Models;
  6. using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
  7. using BirdsiteLive.DAL.Postgres.Settings;
  8. using Dapper;
  9. namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
  10. {
  11. public class TwitterUserPostgresDal : PostgresBase, ITwitterUserDal
  12. {
  13. #region Ctor
  14. public TwitterUserPostgresDal(PostgresSettings settings) : base(settings)
  15. {
  16. }
  17. #endregion
  18. public async Task CreateTwitterUserAsync(string acct, long lastTweetPostedId)
  19. {
  20. acct = acct.ToLowerInvariant();
  21. using (var dbConnection = Connection)
  22. {
  23. dbConnection.Open();
  24. await dbConnection.ExecuteAsync(
  25. $"INSERT INTO {_settings.TwitterUserTableName} (acct,lastTweetPostedId,lastTweetSynchronizedForAllFollowersId) VALUES(@acct,@lastTweetPostedId,@lastTweetSynchronizedForAllFollowersId)",
  26. new { acct, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = lastTweetPostedId });
  27. }
  28. }
  29. public async Task<SyncTwitterUser> GetTwitterUserAsync(string acct)
  30. {
  31. var query = $"SELECT * FROM {_settings.TwitterUserTableName} WHERE acct = @acct";
  32. acct = acct.ToLowerInvariant();
  33. using (var dbConnection = Connection)
  34. {
  35. dbConnection.Open();
  36. var result = (await dbConnection.QueryAsync<SyncTwitterUser>(query, new { acct })).FirstOrDefault();
  37. return result;
  38. }
  39. }
  40. public async Task<SyncTwitterUser> GetTwitterUserAsync(int id)
  41. {
  42. var query = $"SELECT * FROM {_settings.TwitterUserTableName} WHERE id = @id";
  43. using (var dbConnection = Connection)
  44. {
  45. dbConnection.Open();
  46. var result = (await dbConnection.QueryAsync<SyncTwitterUser>(query, new { id })).FirstOrDefault();
  47. return result;
  48. }
  49. }
  50. public async Task<int> GetTwitterUsersCountAsync()
  51. {
  52. var query = $"SELECT COUNT(*) FROM {_settings.TwitterUserTableName}";
  53. using (var dbConnection = Connection)
  54. {
  55. dbConnection.Open();
  56. var result = (await dbConnection.QueryAsync<int>(query)).FirstOrDefault();
  57. return result;
  58. }
  59. }
  60. public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync(int maxNumber)
  61. {
  62. var query = $"SELECT * FROM {_settings.TwitterUserTableName} ORDER BY lastSync ASC LIMIT @maxNumber";
  63. using (var dbConnection = Connection)
  64. {
  65. dbConnection.Open();
  66. var result = await dbConnection.QueryAsync<SyncTwitterUser>(query, new { maxNumber });
  67. return result.ToArray();
  68. }
  69. }
  70. public async Task<SyncTwitterUser[]> GetAllTwitterUsersAsync()
  71. {
  72. var query = $"SELECT * FROM {_settings.TwitterUserTableName}";
  73. using (var dbConnection = Connection)
  74. {
  75. dbConnection.Open();
  76. var result = await dbConnection.QueryAsync<SyncTwitterUser>(query);
  77. return result.ToArray();
  78. }
  79. }
  80. public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, int fetchingErrorCount, DateTime lastSync)
  81. {
  82. if(id == default) throw new ArgumentException("id");
  83. if(lastTweetPostedId == default) throw new ArgumentException("lastTweetPostedId");
  84. if(lastTweetSynchronizedForAllFollowersId == default) throw new ArgumentException("lastTweetSynchronizedForAllFollowersId");
  85. if(lastSync == default) throw new ArgumentException("lastSync");
  86. var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId, fetchingErrorCount = @fetchingErrorCount, lastSync = @lastSync WHERE id = @id";
  87. using (var dbConnection = Connection)
  88. {
  89. dbConnection.Open();
  90. await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId, fetchingErrorCount, lastSync = lastSync.ToUniversalTime() });
  91. }
  92. }
  93. public async Task UpdateTwitterUserAsync(SyncTwitterUser user)
  94. {
  95. await UpdateTwitterUserAsync(user.Id, user.LastTweetPostedId, user.LastTweetSynchronizedForAllFollowersId, user.FetchingErrorCount, user.LastSync);
  96. }
  97. public async Task DeleteTwitterUserAsync(string acct)
  98. {
  99. if (string.IsNullOrWhiteSpace(acct)) throw new ArgumentException("acct");
  100. acct = acct.ToLowerInvariant();
  101. var query = $"DELETE FROM {_settings.TwitterUserTableName} WHERE acct = @acct";
  102. using (var dbConnection = Connection)
  103. {
  104. dbConnection.Open();
  105. await dbConnection.QueryAsync(query, new { acct });
  106. }
  107. }
  108. public async Task DeleteTwitterUserAsync(int id)
  109. {
  110. if (id == default) throw new ArgumentException("id");
  111. var query = $"DELETE FROM {_settings.TwitterUserTableName} WHERE id = @id";
  112. using (var dbConnection = Connection)
  113. {
  114. dbConnection.Open();
  115. await dbConnection.QueryAsync(query, new { id });
  116. }
  117. }
  118. }
  119. }