diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs index df2fc79..beee5d5 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/Base/PostgresBase.cs @@ -1,4 +1,5 @@ using BirdsiteLive.DAL.Postgres.Settings; +using BirdsiteLive.DAL.Models; using Npgsql; namespace BirdsiteLive.DAL.Postgres.DataAccessLayers.Base @@ -12,7 +13,8 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers.Base protected PostgresBase(PostgresSettings settings) { _settings = settings; - _dataSource = NpgsqlDataSource.Create(settings.ConnString); + var dataSourceBuilder = new NpgsqlDataSourceBuilder(settings.ConnString); + _dataSource = dataSourceBuilder.Build(); } #endregion diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs index a5bb75d..96a6694 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs @@ -8,6 +8,7 @@ using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base; using BirdsiteLive.DAL.Postgres.Settings; using Dapper; using Newtonsoft.Json; +using Npgsql; namespace BirdsiteLive.DAL.Postgres.DataAccessLayers { @@ -78,13 +79,33 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers { if (followedUserId == default) throw new ArgumentException("followedUserId"); - var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE @id=ANY(followings)"; - - using (var dbConnection = Connection) + var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE $1=ANY(followings)"; + + await using var connection = DataSource.CreateConnection(); + await connection.OpenAsync(); + await using var command = new NpgsqlCommand(query, connection) { + Parameters = { new() { Value = followedUserId}} + }; + var reader = await command.ExecuteReaderAsync(); + + var followers = new List(); + while (await reader.ReadAsync()) { - var result = await dbConnection.QueryAsync(query, new { id = followedUserId}); - return result.Select(Convert).ToArray(); + followers.Add(new Follower + { + Id = reader["id"] as int? ?? default, + Followings = reader["followings"] as List ?? new List(), + FollowingsSyncStatus = reader["followingsSyncStatus"] as Dictionary ?? new Dictionary(), + ActorId = reader["actorId"] as string, + Acct = reader["acct"] as string, + Host = reader["host"] as string, + InboxRoute = reader["host"] as string, + SharedInboxRoute = reader["sharedInboxRoute"] as string, + PostingErrorCount = reader["postingErrorCount"] as int? ?? default, + }); } + + return followers.ToArray(); } public async Task GetAllFollowersAsync() diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs index b5a3aa9..b859b6d 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs @@ -144,12 +144,14 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers if(username == default) throw new ArgumentException("id"); if(twitterUserId == default) throw new ArgumentException("twtterUserId"); - var query = $"UPDATE {_settings.TwitterUserTableName} SET twitterUserId = @twitterUserId WHERE acct = @username"; + var query = $"UPDATE {_settings.TwitterUserTableName} SET twitterUserId = $1 WHERE acct = $2"; + await using var connection = DataSource.CreateConnection(); + await connection.OpenAsync(); + await using var command = new NpgsqlCommand(query, connection) { + Parameters = { new() { Value = twitterUserId}, new() { Value = username}} + }; - using (var dbConnection = Connection) - { - await dbConnection.QueryAsync(query, new { username, twitterUserId }); - } + await command.ExecuteNonQueryAsync(); } public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, int fetchingErrorCount, DateTime lastSync) { @@ -158,12 +160,21 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers if(lastTweetSynchronizedForAllFollowersId == default) throw new ArgumentException("lastTweetSynchronizedForAllFollowersId"); if(lastSync == default) throw new ArgumentException("lastSync"); - var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId, fetchingErrorCount = @fetchingErrorCount, lastSync = @lastSync WHERE id = @id"; + var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = $1, lastTweetSynchronizedForAllFollowersId = $2, fetchingErrorCount = $3, lastSync = $4 WHERE id = $5"; - using (var dbConnection = Connection) - { - await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId, fetchingErrorCount, lastSync = lastSync.ToUniversalTime() }); - } + await using var connection = DataSource.CreateConnection(); + await connection.OpenAsync(); + await using var command = new NpgsqlCommand(query, connection) { + Parameters = { + new() { Value = lastTweetPostedId}, + new() { Value = lastTweetSynchronizedForAllFollowersId}, + new() { Value = fetchingErrorCount}, + new() { Value = lastSync.ToUniversalTime()}, + new() { Value = id}, + } + }; + + await command.ExecuteNonQueryAsync(); } public async Task UpdateTwitterUserAsync(SyncTwitterUser user)