📄
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
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 NotifiedScreenings (Id TEXT PRIMARY KEY, NotifiedAt TEXT NOT NULL)"; command.ExecuteNonQuery(); } public async Task<bool> HasBeenNotifiedAsync(string screeningId) { await using var connection = new SqliteConnection(_connectionString); await connection.OpenAsync(); using var command = connection.CreateCommand(); command.CommandText = "SELECT 1 FROM NotifiedScreenings WHERE Id = $id"; command.Parameters.AddWithValue("$id", screeningId); return await command.ExecuteScalarAsync() is not null; } public async Task MarkNotifiedAsync(string screeningId) { await using var connection = new SqliteConnection(_connectionString); await connection.OpenAsync(); using var command = connection.CreateCommand(); command.CommandText = "INSERT OR IGNORE INTO NotifiedScreenings (Id, NotifiedAt) VALUES ($id, $at)"; command.Parameters.AddWithValue("$id", screeningId); command.Parameters.AddWithValue("$at", DateTimeOffset.UtcNow.ToString("O")); await command.ExecuteNonQueryAsync(); } }