Preliminary EntityFramework support

This commit is contained in:
Noi 2022-03-19 00:00:15 -07:00
parent d700cd8ce9
commit faccd9b3aa
5 changed files with 132 additions and 0 deletions

View file

@ -23,10 +23,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" /> <PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Discord.Net" Version="3.4.1" /> <PackageReference Include="Discord.Net" Version="3.4.1" />
<PackageReference Include="EFCore.NamingConventions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NodaTime" Version="3.0.9" /> <PackageReference Include="NodaTime" Version="3.0.9" />
<PackageReference Include="Npgsql" Version="6.0.3" /> <PackageReference Include="Npgsql" Version="6.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

18
Data/BlocklistEntry.cs Normal file
View file

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BirthdayBot.Data;
[Table("banned_users")]
public class BlocklistEntry {
[Key]
[Column("guild_id")]
public long GuildId { get; set; }
[Key]
[Column("user_id")]
public long UserId { get; set; }
[ForeignKey(nameof(GuildConfig.GuildId))]
[InverseProperty(nameof(GuildConfig.BlockedUsers))]
public GuildConfig Guild { get; set; } = null!;
}

View file

@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore;
namespace BirthdayBot.Data;
public class BotDatabaseContext : DbContext {
public virtual DbSet<BlocklistEntry> BlocklistEntries { get; set; } = null!;
public virtual DbSet<GuildConfig> GuildConfigurations { get; set; } = null!;
public virtual DbSet<UserEntry> UserEntries { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql("Host=localhost;Username=birthdaybot;Password=bb") // TODO use actual connection string
.UseSnakeCaseNamingConvention();
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<BlocklistEntry>(entity => {
entity.HasKey(e => new { e.GuildId, e.UserId })
.HasName("banned_users_pkey");
entity.HasOne(d => d.Guild)
.WithMany(p => p.BlockedUsers)
.HasForeignKey(d => d.GuildId)
.HasConstraintName("banned_users_guild_id_fkey");
});
modelBuilder.Entity<GuildConfig>(entity => {
entity.HasKey(e => e.GuildId)
.HasName("settings_pkey");
entity.Property(e => e.GuildId).ValueGeneratedNever();
entity.Property(e => e.LastSeen).HasDefaultValueSql("now()");
});
modelBuilder.Entity<UserEntry>(entity => {
entity.HasKey(e => new { e.GuildId, e.UserId })
.HasName("user_birthdays_pkey");
entity.Property(e => e.LastSeen).HasDefaultValueSql("now()");
entity.HasOne(d => d.Guild)
.WithMany(p => p.UserEntries)
.HasForeignKey(d => d.GuildId)
.HasConstraintName("user_birthdays_guild_id_fkey");
});
}
}

39
Data/GuildConfig.cs Normal file
View file

@ -0,0 +1,39 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BirthdayBot.Data;
[Table("settings")]
public class GuildConfig {
public GuildConfig() {
BlockedUsers = new HashSet<BlocklistEntry>();
UserEntries = new HashSet<UserEntry>();
}
[Key]
[Column("guild_id")]
public long GuildId { get; set; }
[Column("role_id")]
public long? RoleId { get; set; }
[Column("channel_announce_id")]
public long? ChannelAnnounceId { get; set; }
[Column("time_zone")]
public string? TimeZone { get; set; }
[Column("moderated")]
public bool Moderated { get; set; }
[Column("moderator_role")]
public long? ModeratorRole { get; set; }
[Column("announce_message")]
public string? AnnounceMessage { get; set; }
[Column("announce_message_pl")]
public string? AnnounceMessagePl { get; set; }
[Column("announce_ping")]
public bool AnnouncePing { get; set; }
[Column("last_seen")]
public DateTimeOffset LastSeen { get; set; }
[InverseProperty(nameof(BlocklistEntry.Guild))]
public ICollection<BlocklistEntry> BlockedUsers { get; set; }
[InverseProperty(nameof(UserEntry.Guild))]
public ICollection<UserEntry> UserEntries { get; set; }
}

26
Data/UserEntry.cs Normal file
View file

@ -0,0 +1,26 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BirthdayBot.Data;
[Table("user_birthdays")]
public class UserEntry {
[Key]
[Column("guild_id")]
public long GuildId { get; set; }
[Key]
[Column("user_id")]
public long UserId { get; set; }
[Column("birth_month")]
public int BirthMonth { get; set; }
[Column("birth_day")]
public int BirthDay { get; set; }
[Column("time_zone")]
public string? TimeZone { get; set; }
[Column("last_seen")]
public DateTimeOffset LastSeen { get; set; }
[ForeignKey(nameof(GuildConfig.GuildId))]
[InverseProperty(nameof(GuildConfig.UserEntries))]
public GuildConfig Guild { get; set; } = null!;
}