From ca10f50645bc5ade83aab5de39d777f908067092 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Tue, 4 Aug 2020 23:40:15 +0200 Subject: [PATCH] Replace models with proper EF-models --- ChaosBot/Models/ChaosbotContext.cs | 49 ++++++ ChaosBot/Models/CommandPermission.cs | 20 +++ ChaosBot/Models/Configuration.cs | 18 ++ ChaosBot/Models/LevelUp.cs | 15 -- ChaosBot/Models/LodestoneCharacter.cs | 21 ++- ChaosBot/Models/LodestoneFreeCompany.cs | 17 +- ChaosBot/Models/Point.cs | 15 ++ ChaosBot/Models/PointsTable.cs | 15 -- ChaosBot/Models/Raffle.cs | 16 ++ ChaosBot/Models/RaffleTable.cs | 14 -- ChaosBot/Models/ServerConfigurationFlag.cs | 14 -- ChaosBot/Models/chaosbotContext.cs | 195 --------------------- 12 files changed, 141 insertions(+), 268 deletions(-) create mode 100644 ChaosBot/Models/ChaosbotContext.cs create mode 100644 ChaosBot/Models/CommandPermission.cs create mode 100644 ChaosBot/Models/Configuration.cs delete mode 100644 ChaosBot/Models/LevelUp.cs create mode 100644 ChaosBot/Models/Point.cs delete mode 100644 ChaosBot/Models/PointsTable.cs create mode 100644 ChaosBot/Models/Raffle.cs delete mode 100644 ChaosBot/Models/RaffleTable.cs delete mode 100644 ChaosBot/Models/ServerConfigurationFlag.cs delete mode 100644 ChaosBot/Models/chaosbotContext.cs diff --git a/ChaosBot/Models/ChaosbotContext.cs b/ChaosBot/Models/ChaosbotContext.cs new file mode 100644 index 0000000..ad60227 --- /dev/null +++ b/ChaosBot/Models/ChaosbotContext.cs @@ -0,0 +1,49 @@ +using ChaosBot.Database.Repository; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +namespace ChaosBot.Models +{ + public class ChaosbotContext : DbContext + { + public DbSet LodestoneCharacter { get; set; } + public DbSet LodestoneFreeCompany { get; set; } + public DbSet Points { get; set; } + public DbSet Raffles { get; set; } + public DbSet CommandPermissions { get; set; } + public DbSet Configuration { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + if (ConfigurationRepository.AppSettingsHandler == null) + { + ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder() + .SetBasePath(System.IO.Directory.GetCurrentDirectory()) + .AddJsonFile("./appsettings.json", optional: false, reloadOnChange: true).Build(); + } + string server = ConfigurationRepository.GetValue("Database:Host"); + int port = ConfigurationRepository.GetValue("Database:Port"); + string user = ConfigurationRepository.GetValue("Database:User"); + string pass = ConfigurationRepository.GetValue("Database:Pass"); + string name = ConfigurationRepository.GetValue("Database:Name"); + optionsBuilder.UseMySql( + $"server={server};port={port};user={user};password={pass};database={name}", + x => x.ServerVersion("5.5.64-mariadb")); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasKey(x => new {x.DiscordGuildId, x.LodestoneId}); + modelBuilder.Entity() + .HasKey(x => new {x.DiscordGuildId, x.LodestoneId}); + modelBuilder.Entity() + .HasKey(x => new {x.DiscordGuildId, x.DiscordUserId}); + modelBuilder.Entity() + .HasKey(x => new {x.DiscordGuildId, x.Key}); + } + } +} \ No newline at end of file diff --git a/ChaosBot/Models/CommandPermission.cs b/ChaosBot/Models/CommandPermission.cs new file mode 100644 index 0000000..3a72724 --- /dev/null +++ b/ChaosBot/Models/CommandPermission.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; + +namespace ChaosBot.Models +{ + #region Required + public class CommandPermission + { + [Key] + public ulong Id { get; set; } + [Required] + public ulong DiscordGuildId { get; set; } + [Required] + public ulong TargetId { get; set; } + [Required] + public int TargetType { get; set; } + [Required] + public string Command { get; set; } + } + #endregion +} \ No newline at end of file diff --git a/ChaosBot/Models/Configuration.cs b/ChaosBot/Models/Configuration.cs new file mode 100644 index 0000000..2c06fd3 --- /dev/null +++ b/ChaosBot/Models/Configuration.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.EntityFrameworkCore; + +namespace ChaosBot.Models +{ + #region Required + public class Configuration + { + [Required] + public ulong DiscordGuildId { get; set; } + [Required] + [MaxLength(128)] + public string Key { get; set; } + [Required] + public string SerializedValue { get; set; } + } + #endregion +} \ No newline at end of file diff --git a/ChaosBot/Models/LevelUp.cs b/ChaosBot/Models/LevelUp.cs deleted file mode 100644 index 32d46ab..0000000 --- a/ChaosBot/Models/LevelUp.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; - -#nullable disable - -namespace ChaosBot.Models -{ - public partial class LevelUp - { - public int? Id { get; set; } - public long? UserId { get; set; } - public long? GuildId { get; set; } - public long? Experience { get; set; } - } -} diff --git a/ChaosBot/Models/LodestoneCharacter.cs b/ChaosBot/Models/LodestoneCharacter.cs index 2b21535..64f652d 100644 --- a/ChaosBot/Models/LodestoneCharacter.cs +++ b/ChaosBot/Models/LodestoneCharacter.cs @@ -1,15 +1,20 @@ -using System; -using System.Collections.Generic; - -#nullable disable +using System.ComponentModel.DataAnnotations; namespace ChaosBot.Models { - public partial class LodestoneCharacter + #region Required + public class LodestoneCharacter { - public int? Id { get; set; } + [Required] + public ulong LodestoneId { get; set; } + [Required] + public ulong DiscordGuildId { get; set; } + [Required] + public ulong DiscordUserId { get; set; } + [Required] public string Name { get; set; } + [Required] public string Avatar { get; set; } - public long? UserId { get; set; } } -} + #endregion +} \ No newline at end of file diff --git a/ChaosBot/Models/LodestoneFreeCompany.cs b/ChaosBot/Models/LodestoneFreeCompany.cs index 369ca90..8e14c7e 100644 --- a/ChaosBot/Models/LodestoneFreeCompany.cs +++ b/ChaosBot/Models/LodestoneFreeCompany.cs @@ -1,13 +1,16 @@ -using System; -using System.Collections.Generic; - -#nullable disable +using System.ComponentModel.DataAnnotations; namespace ChaosBot.Models { - public partial class LodestoneFreeCompany + #region Required + public class LodestoneFreeCompany { - public string Id { get; set; } + [Required] + public ulong LodestoneId { get; set; } + [Required] + public ulong DiscordGuildId { get; set; } + [Required] public string Name { get; set; } } -} + #endregion +} \ No newline at end of file diff --git a/ChaosBot/Models/Point.cs b/ChaosBot/Models/Point.cs new file mode 100644 index 0000000..59465f5 --- /dev/null +++ b/ChaosBot/Models/Point.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace ChaosBot.Models +{ + #region Required + public class Point + { + [Required] + public ulong DiscordUserId { get; set; } + [Required] + public ulong DiscordGuildId { get; set; } + public ulong Amount { get; set; } + } + #endregion +} \ No newline at end of file diff --git a/ChaosBot/Models/PointsTable.cs b/ChaosBot/Models/PointsTable.cs deleted file mode 100644 index c7695f2..0000000 --- a/ChaosBot/Models/PointsTable.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; - -#nullable disable - -namespace ChaosBot.Models -{ - public partial class PointsTable - { - public int? Id { get; set; } - public int? Points { get; set; } - public long? UserId { get; set; } - public long? GuildId { get; set; } - } -} diff --git a/ChaosBot/Models/Raffle.cs b/ChaosBot/Models/Raffle.cs new file mode 100644 index 0000000..5681c06 --- /dev/null +++ b/ChaosBot/Models/Raffle.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace ChaosBot.Models +{ + #region Required + public class Raffle + { + [Key] + public ulong Id { get; set; } + [Required] + public ulong DiscordUserId { get; set; } + [Required] + public ulong DiscordGuildId { get; set; } + } + #endregion +} \ No newline at end of file diff --git a/ChaosBot/Models/RaffleTable.cs b/ChaosBot/Models/RaffleTable.cs deleted file mode 100644 index 2c4b180..0000000 --- a/ChaosBot/Models/RaffleTable.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; - -#nullable disable - -namespace ChaosBot.Models -{ - public partial class RaffleTable - { - public int? Id { get; set; } - public long? UserId { get; set; } - public long? GuildId { get; set; } - } -} diff --git a/ChaosBot/Models/ServerConfigurationFlag.cs b/ChaosBot/Models/ServerConfigurationFlag.cs deleted file mode 100644 index 5acfd9a..0000000 --- a/ChaosBot/Models/ServerConfigurationFlag.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; - -#nullable disable - -namespace ChaosBot.Models -{ - public partial class ServerConfigurationFlag - { - public string Key { get; set; } - public string SerializedValue { get; set; } - public long? GuildId { get; set; } - } -} diff --git a/ChaosBot/Models/chaosbotContext.cs b/ChaosBot/Models/chaosbotContext.cs deleted file mode 100644 index d6da8cf..0000000 --- a/ChaosBot/Models/chaosbotContext.cs +++ /dev/null @@ -1,195 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata; - -#nullable disable - -namespace ChaosBot.Models -{ - public partial class chaosbotContext : DbContext - { - public chaosbotContext() - { - } - - public chaosbotContext(DbContextOptions options) - : base(options) - { - } - - public virtual DbSet LodestoneCharacters { get; set; } - public virtual DbSet LodestoneFreeCompanies { get; set; } - public virtual DbSet PointsTables { get; set; } - public virtual DbSet RaffleTables { get; set; } - public virtual DbSet ServerConfigurationFlags { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (!optionsBuilder.IsConfigured) - { -#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263. - optionsBuilder.UseMySql($"server={Program.LoadConfiguration("Database:Host")};port={Program.LoadConfiguration("Database:Port")};user={Program.LoadConfiguration("Database:User")};password={Program.LoadConfiguration("Database:Pass")};database={Program.LoadConfiguration("Database:Name")}", x => x.ServerVersion("5.5.64-mariadb")); - } - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity(entity => - { - entity.HasNoKey(); - - entity.ToTable("LodestoneCharacter"); - - entity.Property(e => e.Avatar) - .HasColumnType("text") - .HasColumnName("avatar") - .HasCharSet("latin1") - .HasCollation("latin1_swedish_ci") - .HasAnnotation("Relational:ColumnType", "text"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("id") - .HasAnnotation("Relational:ColumnType", "int(11)"); - - entity.Property(e => e.Name) - .HasColumnType("text") - .HasColumnName("name") - .HasCharSet("latin1") - .HasCollation("latin1_swedish_ci") - .HasAnnotation("Relational:ColumnType", "text"); - - entity.Property(e => e.UserId) - .HasColumnType("bigint(20)") - .HasColumnName("userId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - }); - - modelBuilder.Entity(entity => - { - entity.HasNoKey(); - - entity.ToTable("LodestoneFreeCompany"); - - entity.Property(e => e.Id) - .HasColumnType("text") - .HasColumnName("id") - .HasCharSet("latin1") - .HasCollation("latin1_swedish_ci") - .HasAnnotation("Relational:ColumnType", "text"); - - entity.Property(e => e.Name) - .HasColumnType("text") - .HasColumnName("name") - .HasCharSet("latin1") - .HasCollation("latin1_swedish_ci") - .HasAnnotation("Relational:ColumnType", "text"); - }); - - modelBuilder.Entity(entity => - { - entity.HasNoKey(); - - entity.ToTable("PointsTable"); - - entity.Property(e => e.GuildId) - .HasColumnType("bigint(20)") - .HasColumnName("guildId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("id") - .HasAnnotation("Relational:ColumnType", "int(11)"); - - entity.Property(e => e.Points) - .HasColumnType("int(11)") - .HasColumnName("points") - .HasAnnotation("Relational:ColumnType", "int(11)"); - - entity.Property(e => e.UserId) - .HasColumnType("bigint(20)") - .HasColumnName("userId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - }); - - modelBuilder.Entity(entity => - { - entity.HasNoKey(); - - entity.ToTable("RaffleTable"); - - entity.Property(e => e.GuildId) - .HasColumnType("bigint(20)") - .HasColumnName("guildId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("id") - .HasAnnotation("Relational:ColumnType", "int(11)"); - - entity.Property(e => e.UserId) - .HasColumnType("bigint(20)") - .HasColumnName("userId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - }); - - modelBuilder.Entity(entity => - { - entity.HasNoKey(); - - entity.ToTable("LevelTable"); - - entity.Property(e => e.GuildId) - .HasColumnType("bigint(20)") - .HasColumnName("guildId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("id") - .HasAnnotation("Relational:ColumnType", "int(11)"); - - entity.Property(e => e.UserId) - .HasColumnType("bigint(20)") - .HasColumnName("userId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - - entity.Property(e => e.Experience) - .HasColumnType("bigint(20)") - .HasColumnName("xp") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - - }); - - modelBuilder.Entity(entity => - { - entity.HasNoKey(); - - entity.Property(e => e.GuildId) - .HasColumnType("bigint(20)") - .HasColumnName("guildId") - .HasAnnotation("Relational:ColumnType", "bigint(20)"); - - entity.Property(e => e.Key) - .HasColumnType("text") - .HasColumnName("key") - .HasCharSet("latin1") - .HasCollation("latin1_swedish_ci") - .HasAnnotation("Relational:ColumnType", "text"); - - entity.Property(e => e.SerializedValue) - .HasColumnType("text") - .HasColumnName("serializedValue") - .HasCharSet("latin1") - .HasCollation("latin1_swedish_ci") - .HasAnnotation("Relational:ColumnType", "text"); - }); - - OnModelCreatingPartial(modelBuilder); - } - - partial void OnModelCreatingPartial(ModelBuilder modelBuilder); - } -}