📄
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
using Microsoft.Data.Sqlite; using Microsoft.Extensions.Configuration; internal sealed class ScreeningRepository { private readonly string connectionString; public ScreeningRepository(IConfiguration configuration) { connectionString = configuration.GetConnectionString("Screenings") ?? "DataSource=screenings.db"; Initialize(); } private void Initialize() { using var connection = new SqliteConnection(connectionString); connection.Open(); using var command = connection.CreateCommand(); command.CommandText = "CREATE TABLE IF NOT EXISTS DetectedHtml (Id INTEGER PRIMARY KEY AUTOINCREMENT, Html TEXT NOT NULL, DetectedAt TEXT NOT NULL)"; command.ExecuteNonQuery(); } public async Task<string?> LatestHtml(CancellationToken cancellationToken) { await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(cancellationToken); using var command = connection.CreateCommand(); command.CommandText = "SELECT Html FROM DetectedHtml ORDER BY DetectedAt DESC LIMIT 1"; return await command.ExecuteScalarAsync(cancellationToken) as string; } public async Task InsertNewDetection(string html, DateTimeOffset detectedAt) { await using var connection = new SqliteConnection(connectionString); await connection.OpenAsync(); using var command = connection.CreateCommand(); command.CommandText = "INSERT INTO DetectedHtml (Html, DetectedAt) VALUES ($html, $at)"; command.Parameters.AddWithValue("$html", html); command.Parameters.AddWithValue("$at", detectedAt.ToString("O")); await command.ExecuteNonQueryAsync(); } }