Skip to content

Commit 744d9ce

Browse files
List users and connections
1 parent 1308d93 commit 744d9ce

File tree

9 files changed

+121
-12
lines changed

9 files changed

+121
-12
lines changed

dotnet/S03E05/GlobalUsings.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33
global using Microsoft.Extensions.DependencyInjection;
44
global using Microsoft.Extensions.Hosting;
55
global using Microsoft.Extensions.Options;
6-
global using Common;
7-
global using Common.Models;
8-
global using Common.Services;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace S03E05.Models;
4+
5+
public record DatabaseRequest
6+
{
7+
[JsonPropertyName("task")]
8+
public string Task { get; } = "database";
9+
10+
[JsonPropertyName("apikey")]
11+
public required string ApiKey { get; init; }
12+
13+
[JsonPropertyName("query")]
14+
public required string Query { get; init; }
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace S03E05.Models;
2+
3+
using System.Text.Json.Serialization;
4+
5+
public record DbConnection
6+
{
7+
[JsonPropertyName("user1_id")]
8+
public required string User1Id { get; set; }
9+
10+
[JsonPropertyName("user2_id")]
11+
public required string User2Id { get; set; }
12+
}

dotnet/S03E05/Models/DbResponse.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace S03E05.Models;
2+
3+
public record DbResponse<T>
4+
{
5+
[JsonPropertyName("reply")]
6+
public required IReadOnlyList<T> Reply { get; set; }
7+
8+
[JsonPropertyName("error")]
9+
public required string Error { get; set; }
10+
}

dotnet/S03E05/Models/DbUser.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace S03E05.Models;
2+
3+
using System.Text.Json.Serialization;
4+
5+
public record DbUser
6+
{
7+
[JsonPropertyName("id")]
8+
public required string Id { get; set; }
9+
10+
[JsonPropertyName("username")]
11+
public required string Username { get; set; }
12+
13+
[JsonPropertyName("access_level")]
14+
public required string AccessLevel { get; set; }
15+
16+
[JsonPropertyName("is_active")]
17+
public required string IsActive { get; set; }
18+
19+
[JsonPropertyName("lastlog")]
20+
public required DateTime LastLog { get; set; }
21+
}
22+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace S03E05.Models;
4+
5+
public class S03E05Options
6+
{
7+
public const string SectionName = "S03E05";
8+
9+
[Required]
10+
public string DatabaseApiUrl { get; init; } = string.Empty;
11+
}

dotnet/S03E05/Program.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using S03E05;
1+
using System.Net.Http.Json;
2+
using Common.AiDevsApi.Models;
3+
using S03E05;
24
using S03E05.Models;
35

46
var builder = Host.CreateApplicationBuilder(args);
@@ -10,6 +12,13 @@
1012
await using var scope = host.Services.CreateAsyncScope();
1113
var sp = scope.ServiceProvider;
1214

15+
var dbUsers = await ListUsers();
16+
var dbConnections = await ListConnections();
17+
18+
Console.WriteLine(JsonSerializer.Serialize(dbUsers));
19+
Console.WriteLine(JsonSerializer.Serialize(dbConnections));
20+
21+
1322
// Add task-specific logic here
1423

1524
// var aiDevsApiService = sp.GetRequiredService<IAiDevsApiService>();
@@ -22,10 +31,30 @@
2231
// var taskResponseResult = await aiDevsApiService.VerifyTaskAnswerAsync(taskResponse);
2332
// Console.WriteLine(JsonSerializer.Serialize(taskResponseResult));
2433

34+
async Task<IReadOnlyList<DbUser>> ListUsers()
35+
{
36+
const string Command = "SELECT * FROM users";
37+
var dbResponse = await RunDbCommand(Command);
38+
39+
var parsedResponse = JsonSerializer.Deserialize<DbResponse<DbUser>>(dbResponse)
40+
?? throw new InvalidOperationException("Invalid API response");
41+
return parsedResponse.Reply;
42+
}
43+
44+
async Task<IReadOnlyList<DbConnection>> ListConnections()
45+
{
46+
const string Command = "SELECT * FROM connections";
47+
var dbResponse = await RunDbCommand(Command);
48+
49+
var parsedResponse = JsonSerializer.Deserialize<DbResponse<DbConnection>>(dbResponse)
50+
?? throw new InvalidOperationException("Invalid API response");
51+
return parsedResponse.Reply;
52+
}
53+
2554
async Task<string> RunDbCommand(string command)
2655
{
2756
var aiDevsOptions = sp.GetRequiredService<IOptions<AiDevsApiOptions>>().Value;
28-
var taskOptions = sp.GetRequiredService<IOptions<S03E03Options>>().Value;
57+
var taskOptions = sp.GetRequiredService<IOptions<S03E05Options>>().Value;
2958

3059
var dbRequest = new DatabaseRequest
3160
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"profiles": {
3+
"S03E05": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"ASPNETCORE_ENVIRONMENT": "Development",
7+
"DOTNET_ENVIRONMENT": "Development"
8+
}
9+
}
10+
}
11+
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
using Common.AiDevsApi.Extensions;
2+
using S03E05.Models;
3+
14
namespace S03E05;
25

36
public static class ServiceCollectionExtensions
47
{
58
public static IServiceCollection AddS03E05(this IServiceCollection services)
69
{
7-
services
8-
.AddCommonServices()
9-
.Configure<S03E05Options>(options =>
10-
{
11-
// Configure task-specific options here
12-
});
10+
services.AddOptions<S03E05Options>()
11+
.BindConfiguration(S03E05Options.SectionName)
12+
.ValidateDataAnnotations()
13+
.ValidateOnStart();
1314

14-
return services;
15+
return services
16+
.AddAiDevsApi();
1517
}
1618
}

0 commit comments

Comments
 (0)