From 5c9b8e87716a77318a97bbc4a29ee9bd082ae3a0 Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Fri, 30 Dec 2022 16:16:40 -0500 Subject: [PATCH] switched to vanilla npgsql for more queries 2 --- .../DataAccessLayers/FollowersPostgresDal.cs | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/FollowersPostgresDal.cs index 96a6694..a4d4b92 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 System.Text.Json; using Npgsql; namespace BirdsiteLive.DAL.Postgres.DataAccessLayers @@ -63,16 +64,36 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers public async Task GetFollowerAsync(string acct, string host) { - var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE acct = @acct AND host = @host"; + var query = $"SELECT * FROM {_settings.FollowersTableName} WHERE acct = $1 AND host = $2"; acct = acct.ToLowerInvariant(); host = host.ToLowerInvariant(); - using (var dbConnection = Connection) + await using var connection = DataSource.CreateConnection(); + await connection.OpenAsync(); + await using var command = new NpgsqlCommand(query, connection) { + Parameters = { new() { Value = acct}, new() { Value = host }}, + }; + var reader = await command.ExecuteReaderAsync(); + + if (!await reader.ReadAsync()) + return null; + + string syncStatusString = reader["followingsSyncStatus"] as string; + var syncStatus = System.Text.Json.JsonSerializer.Deserialize>(syncStatusString); + return new Follower { - var result = (await dbConnection.QueryAsync(query, new { acct, host })).FirstOrDefault(); - return Convert(result); - } + Id = reader["id"] as int? ?? default, + Followings = (reader["followings"] as int[] ?? new int[0]).ToList(), + FollowingsSyncStatus = syncStatus, + ActorId = reader["actorId"] as string, + Acct = reader["acct"] as string, + Host = reader["host"] as string, + InboxRoute = reader["inboxRoute"] as string, + SharedInboxRoute = reader["sharedInboxRoute"] as string, + PostingErrorCount = reader["postingErrorCount"] as int? ?? default, + }; + } public async Task GetFollowersAsync(int followedUserId) @@ -91,15 +112,17 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers var followers = new List(); while (await reader.ReadAsync()) { + string syncStatusString = reader["followingsSyncStatus"] as string; + var syncStatus = System.Text.Json.JsonSerializer.Deserialize>(syncStatusString); followers.Add(new Follower { Id = reader["id"] as int? ?? default, - Followings = reader["followings"] as List ?? new List(), - FollowingsSyncStatus = reader["followingsSyncStatus"] as Dictionary ?? new Dictionary(), + Followings = (reader["followings"] as int[] ?? new int[0]).ToList(), + FollowingsSyncStatus = syncStatus, ActorId = reader["actorId"] as string, Acct = reader["acct"] as string, Host = reader["host"] as string, - InboxRoute = reader["host"] as string, + InboxRoute = reader["inboxRoute"] as string, SharedInboxRoute = reader["sharedInboxRoute"] as string, PostingErrorCount = reader["postingErrorCount"] as int? ?? default, });