namespace Tissue.Features.DataProcessing; public class DapperDataStorage(PostgresConfiguration configuration, ILogger logger) : IDataStorage { public async Task?> GetAllTorrents(CancellationToken cancellationToken = default) { const string GetAllTorrentsSql = "SELECT * FROM torrents"; try { await using var connection = await CreateAndOpenConnection(cancellationToken); var torrents = await connection.QueryAsync(GetAllTorrentsSql); return torrents.ToList(); } catch (Exception e) { logger.LogError(e, "Error while torrents from database"); return new List(); } } public async Task DeleteTorrentsByInfoHashes(IReadOnlyCollection infoHashes, CancellationToken cancellationToken = default) { const string deleteTorrentsSql = "DELETE FROM torrents WHERE \"infoHash\" = ANY(@infoHashes)"; try { await using var connection = await CreateAndOpenConnection(cancellationToken); await connection.ExecuteAsync(deleteTorrentsSql, new { infoHashes }); } catch (Exception e) { logger.LogError(e, "Error while deleting torrents from database"); } } private async Task CreateAndOpenConnection(CancellationToken cancellationToken = default) { var connection = new NpgsqlConnection(configuration.StorageConnectionString); await connection.OpenAsync(cancellationToken); return connection; } }