-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathProgram.cs
69 lines (57 loc) · 1.98 KB
/
Program.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
DeltaExtensions.UseResponseDiagnostics = true;
var connectionString = PostgresConnection.ConnectionString;
#region UseDeltaPostgres
var builder = WebApplication.CreateBuilder();
builder.Services.AddScoped(_ => new NpgsqlConnection(connectionString));
var app = builder.Build();
app.UseDelta();
#endregion
await using (var connection = new NpgsqlConnection(connectionString))
{
await connection.OpenAsync();
await PostgresDbBuilder.Create(connection);
await using var command = connection.CreateCommand();
command.CommandText =
$"""
insert into "Companies"("Id", "Content")
values ('{Guid.NewGuid()}', 'The company')
""";
await command.ExecuteNonQueryAsync();
}
app.MapGet(
"/",
async _ =>
{
var connection = _.RequestServices.GetRequiredService<NpgsqlConnection>();
if (connection.State == ConnectionState.Closed)
{
await connection.OpenAsync();
}
var lastTimeStamp = await connection.GetLastTimeStamp();
await using var command = connection.CreateCommand();
command.CommandText = """
select * from "Companies"
""";
await using var reader = await command.ExecuteReaderAsync();
var builder = new StringBuilder("Results: ");
builder.AppendLine();
builder.AppendLine($"LastTimeStamp: {lastTimeStamp}");
builder.AppendLine();
while (await reader.ReadAsync())
{
for (var i = 0; i < reader.FieldCount; i++)
{
var value = reader.GetValue(i);
if (value is byte[] bytes)
{
value = BitConverter.ToString(bytes);
}
builder.AppendLine($"{reader.GetName(i)}: {value}");
}
}
await _.Response.WriteAsync(builder.ToString());
});
app.MapGroup("/group")
.UseDelta()
.MapGet("/", () => "Hello Group!");
app.Run();