Skip to content

Commit 595fd9a

Browse files
authored
Merge pull request #3 from pk9r/dev
Add entity models and DbContext for Hoyolab integration
2 parents 1175c78 + ab7982b commit 595fd9a

8 files changed

Lines changed: 177 additions & 15 deletions

File tree

Directory.Packages.props

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
<Project>
2-
<PropertyGroup>
3-
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4-
</PropertyGroup>
5-
6-
<ItemGroup>
7-
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.4.0"/>
8-
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.7.0"/>
9-
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.4.0"/>
10-
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0"/>
11-
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0"/>
12-
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0"/>
13-
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0"/>
14-
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0"/>
15-
</ItemGroup>
2+
<PropertyGroup>
3+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.4.0" />
7+
<PackageVersion Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.4.0" />
8+
<PackageVersion Include="Aspire.StackExchange.Redis.DistributedCaching" Version="9.4.0" />
9+
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8" />
10+
<PackageVersion Include="Microsoft.Extensions.Caching.Hybrid" Version="9.7.0" />
11+
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.7.0" />
12+
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.4.0" />
13+
<PackageVersion Include="NetCord.Hosting.Services" Version="1.0.0-alpha.410" />
14+
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
15+
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
16+
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
17+
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
18+
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
19+
</ItemGroup>
1620
</Project>

Gitcg.NetCord.sln.DotSettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2-
<s:Boolean x:Key="/Default/UserDictionary/Words/=gitcg/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=gitcg/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=hoyolab/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Gitcg.NetCord.MainApp.Entities;
2+
3+
public class ActiveHoyolabAccount
4+
{
5+
public ulong DiscordUserId { get; set; }
6+
7+
public Guid HoyolabAccountId { get; set; } = Guid.Empty;
8+
9+
public DiscordUser DiscordUser { get; set; } = null!;
10+
11+
public HoyolabAccount? HoyolabAccount { get; set; }
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Gitcg.NetCord.MainApp.Entities;
2+
3+
public class DiscordUser
4+
{
5+
public ulong Id { get; set; }
6+
7+
public ICollection<HoyolabAccount> HoyolabAccounts { get; set; } = [];
8+
public ActiveHoyolabAccount? ActiveHoyolabAccount { get; set; }
9+
10+
public Task AddHoyolabAccountAsync(
11+
string hoyolabUserId, string token, string gameRoleId, string region
12+
)
13+
{
14+
var hoyolabAccount = new HoyolabAccount
15+
{
16+
HoyolabUserId = hoyolabUserId, //
17+
Token = token, //
18+
GameRoleId = gameRoleId, //
19+
Region = region //
20+
};
21+
22+
HoyolabAccounts.Add(hoyolabAccount);
23+
24+
if (
25+
ActiveHoyolabAccount == null ||
26+
ActiveHoyolabAccount.HoyolabAccountId == Guid.Empty
27+
)
28+
{
29+
ActiveHoyolabAccount = new ActiveHoyolabAccount
30+
{
31+
HoyolabAccount = hoyolabAccount //
32+
};
33+
}
34+
35+
return Task.CompletedTask;
36+
}
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Gitcg.NetCord.MainApp.Entities;
2+
3+
public class HoyolabAccount
4+
{
5+
public Guid Id { get; set; }
6+
public ulong DiscordUserId { get; set; }
7+
8+
public string HoyolabUserId { get; set; } = string.Empty;
9+
public string Token { get; set; } = string.Empty;
10+
public string GameRoleId { get; set; } = string.Empty;
11+
public string Region { get; set; } = string.Empty;
12+
13+
public DiscordUser DiscordUser { get; set; } = null!;
14+
}

src/Gitcg.NetCord.MainApp/Gitcg.NetCord.MainApp.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
8+
<UserSecretsId>84407954-6c48-4030-ba22-b3e8b16583d1</UserSecretsId>
89
</PropertyGroup>
910

1011
<ItemGroup>
@@ -17,4 +18,15 @@
1718
</Content>
1819
</ItemGroup>
1920

21+
<ItemGroup>
22+
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" />
23+
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" />
24+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
25+
<PrivateAssets>all</PrivateAssets>
26+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
27+
</PackageReference>
28+
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" />
29+
<PackageReference Include="NetCord.Hosting.Services" />
30+
</ItemGroup>
31+
2032
</Project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Gitcg.NetCord.MainApp.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace Gitcg.NetCord.MainApp.Infrastructure;
5+
6+
public class AppDbContext : DbContext
7+
{
8+
public DbSet<DiscordUser> DiscordUsers { get; set; } = null!;
9+
public DbSet<HoyolabAccount> HoyolabAccounts { get; set; } = null!;
10+
public DbSet<ActiveHoyolabAccount> ActiveHoyolabAccounts { get; set; } = null!;
11+
12+
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
13+
{
14+
}
15+
16+
protected override void OnModelCreating(ModelBuilder builder)
17+
{
18+
builder.Entity<DiscordUser>(config =>
19+
{
20+
config.Property(x => x.Id)
21+
.ValueGeneratedNever();
22+
23+
config.HasOne(x => x.ActiveHoyolabAccount)
24+
.WithOne(x => x.DiscordUser)
25+
.HasForeignKey<ActiveHoyolabAccount>(x => x.DiscordUserId);
26+
});
27+
28+
builder.Entity<HoyolabAccount>(config =>
29+
{
30+
config.Property(x => x.HoyolabUserId).HasMaxLength(10);
31+
config.Property(x => x.Token).HasMaxLength(255);
32+
config.Property(x => x.GameRoleId).HasMaxLength(10);
33+
config.Property(x => x.Region).HasMaxLength(10);
34+
});
35+
36+
builder.Entity<ActiveHoyolabAccount>(config =>
37+
{
38+
config.HasKey(x => x.DiscordUserId);
39+
config.Property(x => x.DiscordUserId)
40+
.ValueGeneratedNever();
41+
});
42+
43+
44+
base.OnModelCreating(builder);
45+
}
46+
}

src/Gitcg.NetCord.MainApp/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
using Gitcg.NetCord.MainApp.Components;
2+
using Gitcg.NetCord.MainApp.Infrastructure;
3+
using NetCord;
4+
using NetCord.Gateway;
5+
using NetCord.Hosting.Gateway;
6+
using NetCord.Hosting.Services.ApplicationCommands;
7+
using NetCord.Hosting.Services.ComponentInteractions;
8+
using NetCord.Services.ComponentInteractions;
29

310
var builder = WebApplication.CreateBuilder(args);
411

512
builder.AddServiceDefaults();
613

14+
builder.AddRedisDistributedCache("redis");
15+
builder.Services.AddHybridCache();
16+
17+
builder.AddNpgsqlDbContext<AppDbContext>("gitcgnetcorddb");
18+
19+
builder.Services
20+
.AddDiscordGateway(options =>
21+
{
22+
options.Intents = GatewayIntents.All;
23+
})
24+
.AddApplicationCommands()
25+
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
26+
.AddComponentInteractions<StringMenuInteraction, StringMenuInteractionContext>()
27+
.AddComponentInteractions<UserMenuInteraction, UserMenuInteractionContext>()
28+
.AddComponentInteractions<RoleMenuInteraction, RoleMenuInteractionContext>()
29+
.AddComponentInteractions<MentionableMenuInteraction, MentionableMenuInteractionContext>()
30+
.AddComponentInteractions<ChannelMenuInteraction, ChannelMenuInteractionContext>()
31+
.AddComponentInteractions<ModalInteraction, ModalInteractionContext>();
32+
733
// Add services to the container.
834
builder.Services.AddRazorComponents()
935
.AddInteractiveServerComponents();
@@ -27,4 +53,14 @@
2753
app.MapRazorComponents<App>()
2854
.AddInteractiveServerRenderMode();
2955

56+
app.AddSlashCommand(
57+
name: "ping",
58+
description: "Ping!",
59+
handler: () => "Pong!"
60+
);
61+
62+
app.MapGet("/api/", () => "Hello World!");
63+
64+
app.UseGatewayHandlers();
65+
3066
app.Run();

0 commit comments

Comments
 (0)