📄
ScreeningRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using BfiMonitor; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Configuration; internal sealed record Detection(int Id, string Html, DateTimeOffset DetectedAt); internal sealed class ScreeningRepository { private readonly string connectionString; public ScreeningRepository(IConfiguration configuration) { connectionString = configuration.GetConnectionString("Screenings") ?? "DataSource=screenings.db"; Initialize(); } private void Initialize() { const string sql = "CREATE TABLE IF NOT EXISTS DetectedHtml (Id INTEGER PRIMARY KEY AUTOINCREMENT, Html TEXT NOT NULL, DetectedAt TEXT NOT NULL)"; using var activity = Tracing.StartInitializeScreeningsDatabase(sql); using var connection = new SqliteConnection(connectionString); connection.Open(); using var command = connection.CreateCommand(); command.CommandText = sql; command.ExecuteNonQuery(); } public async Task<string?> LatestHtml(CancellationToken cancellationToken) { const string sql = "SELECT Html FROM DetectedHtml ORDER BY DetectedAt DESC LIMIT 1"; using var activity = Tracing.StartLatestHtml(sql); await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(cancellationToken); using var command = connection.CreateCommand(); command.CommandText = sql; return await command.ExecuteScalarAsync(cancellationToken) as string; } public async Task InsertNewDetection(string html, DateTimeOffset detectedAt) { const string sql = "INSERT INTO DetectedHtml (Html, DetectedAt) VALUES ($html, $at)"; using var activity = Tracing.StartInsertNewDetection(sql); await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(); using var command = connection.CreateCommand(); command.CommandText = sql; command.Parameters.AddWithValue("$html", html); command.Parameters.AddWithValue("$at", detectedAt.ToString("O")); await command.ExecuteNonQueryAsync(); } public async Task<IReadOnlyList<Detection>> GetDetectionsAsync( int limit, CancellationToken cancellationToken = default ) { const string sql = "SELECT Id, Html, DetectedAt FROM DetectedHtml ORDER BY DetectedAt DESC LIMIT $limit"; using var activity = Tracing.StartGetDetections(sql); await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(cancellationToken); using var command = connection.CreateCommand(); command.CommandText = sql; command.Parameters.AddWithValue("$limit", limit); var results = new List<Detection>(); await using var reader = await command.ExecuteReaderAsync(cancellationToken); while (await reader.ReadAsync(cancellationToken)) { results.Add( new Detection(reader.GetInt32(0), reader.GetString(1), DateTimeOffset.Parse(reader.GetString(2))) ); } return results; } public async Task<Detection?> GetDetectionByIdAsync(int id, CancellationToken cancellationToken = default) { const string sql = "SELECT Id, Html, DetectedAt FROM DetectedHtml WHERE Id = $id"; using var activity = Tracing.StartGetDetectionById(sql); await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(cancellationToken); using var command = connection.CreateCommand(); command.CommandText = sql; command.Parameters.AddWithValue("$id", id); await using var reader = await command.ExecuteReaderAsync(cancellationToken); if (!await reader.ReadAsync(cancellationToken)) { return null; } return new Detection(reader.GetInt32(0), reader.GetString(1), DateTimeOffset.Parse(reader.GetString(2))); } public async Task<int> CountDetectionsAsync(CancellationToken cancellationToken = default) { const string sql = "SELECT COUNT(*) FROM DetectedHtml"; using var activity = Tracing.StartCountDetections(sql); await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(cancellationToken); using var command = connection.CreateCommand(); command.CommandText = sql; return Convert.ToInt32(await command.ExecuteScalarAsync(cancellationToken)); } }