NadekoBot/src/NadekoBot/Services/Database/NadekoContext.cs

292 lines
11 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using NadekoBot.Services.Database.Models;
2016-10-10 04:38:20 +00:00
using NadekoBot.Extensions;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace NadekoBot.Services.Database
{
public class NadekoContextFactory : IDbContextFactory<NadekoContext>
{
/// <summary>
/// :\ Used for migrations
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public NadekoContext Create(DbContextFactoryOptions options)
{
var optionsBuilder = new DbContextOptionsBuilder();
2017-06-21 22:22:50 +00:00
optionsBuilder.UseSqlite("Filename=./data/NadekoBot.db");
2017-06-21 22:12:29 +00:00
var ctx = new NadekoContext(optionsBuilder.Options);
2017-06-21 22:22:50 +00:00
ctx.Database.SetCommandTimeout(60);
2017-06-21 22:12:29 +00:00
return ctx;
}
}
public class NadekoContext : DbContext
{
2016-08-24 19:38:17 +00:00
public DbSet<Quote> Quotes { get; set; }
public DbSet<Donator> Donators { get; set; }
2016-08-24 23:41:11 +00:00
public DbSet<GuildConfig> GuildConfigs { get; set; }
2016-08-25 17:23:47 +00:00
public DbSet<ClashWar> ClashOfClans { get; set; }
public DbSet<ClashCaller> ClashCallers { get; set; }
2016-08-25 22:05:29 +00:00
public DbSet<Reminder> Reminders { get; set; }
2016-08-26 17:25:54 +00:00
public DbSet<SelfAssignedRole> SelfAssignableRoles { get; set; }
public DbSet<BotConfig> BotConfig { get; set; }
public DbSet<Currency> Currency { get; set; }
2016-09-04 13:11:02 +00:00
public DbSet<ConvertUnit> ConversionUnits { get; set; }
public DbSet<MusicPlaylist> MusicPlaylists { get; set; }
public DbSet<CustomReaction> CustomReactions { get; set; }
public DbSet<CurrencyTransaction> CurrencyTransactions { get; set; }
public DbSet<UserPokeTypes> PokeGame { get; set; }
public DbSet<WaifuUpdate> WaifuUpdates { get; set; }
2017-03-30 01:51:54 +00:00
public DbSet<Warning> Warnings { get; set; }
//logging
public DbSet<LogSetting> LogSettings { get; set; }
public DbSet<IgnoredLogChannel> IgnoredLogChannels { get; set; }
public DbSet<IgnoredVoicePresenceChannel> IgnoredVoicePresenceCHannels { get; set; }
//orphans xD
public DbSet<EightBallResponse> EightBallResponses { get; set; }
public DbSet<RaceAnimal> RaceAnimals { get; set; }
public DbSet<ModulePrefix> ModulePrefixes { get; set; }
2017-04-01 22:01:02 +00:00
public DbSet<RewardedUser> RewardedUsers { get; set; }
public NadekoContext() : base()
{
}
public NadekoContext(DbContextOptions options) : base(options)
{
}
public void EnsureSeedData()
{
if (!BotConfig.Any())
{
var bc = new BotConfig();
2016-10-10 04:38:20 +00:00
bc.ModulePrefixes.AddRange(new HashSet<ModulePrefix>()
{
new ModulePrefix() { ModuleName = "Administration", Prefix = "." },
new ModulePrefix() { ModuleName = "Searches", Prefix = "~" },
new ModulePrefix() { ModuleName = "Translator", Prefix = "~" },
new ModulePrefix() { ModuleName = "NSFW", Prefix = "~" },
new ModulePrefix() { ModuleName = "ClashOfClans", Prefix = "," },
new ModulePrefix() { ModuleName = "Help", Prefix = "-" },
new ModulePrefix() { ModuleName = "Music", Prefix = "!!" },
new ModulePrefix() { ModuleName = "Trello", Prefix = "trello" },
new ModulePrefix() { ModuleName = "Games", Prefix = ">" },
new ModulePrefix() { ModuleName = "Gambling", Prefix = "$" },
new ModulePrefix() { ModuleName = "Permissions", Prefix = ";" },
new ModulePrefix() { ModuleName = "Pokemon", Prefix = ">" },
new ModulePrefix() { ModuleName = "Utility", Prefix = "." },
new ModulePrefix() { ModuleName = "CustomReactions", Prefix = "." },
new ModulePrefix() { ModuleName = "PokeGame", Prefix = ">" }
});
2016-10-10 04:38:20 +00:00
bc.RaceAnimals.AddRange(new HashSet<RaceAnimal>
{
new RaceAnimal { Icon = "🐼", Name = "Panda" },
new RaceAnimal { Icon = "🐻", Name = "Bear" },
new RaceAnimal { Icon = "🐧", Name = "Pengu" },
new RaceAnimal { Icon = "🐨", Name = "Koala" },
new RaceAnimal { Icon = "🐬", Name = "Dolphin" },
new RaceAnimal { Icon = "🐞", Name = "Ladybird" },
new RaceAnimal { Icon = "🦀", Name = "Crab" },
new RaceAnimal { Icon = "🦄", Name = "Unicorn" }
});
2016-10-10 04:38:20 +00:00
bc.EightBallResponses.AddRange(new HashSet<EightBallResponse>
{
new EightBallResponse() { Text = "Most definitely yes" },
new EightBallResponse() { Text = "For sure" },
new EightBallResponse() { Text = "Totally!" },
new EightBallResponse() { Text = "Of course!" },
new EightBallResponse() { Text = "As I see it, yes" },
new EightBallResponse() { Text = "My sources say yes" },
new EightBallResponse() { Text = "Yes" },
new EightBallResponse() { Text = "Most likely" },
new EightBallResponse() { Text = "Perhaps" },
new EightBallResponse() { Text = "Maybe" },
new EightBallResponse() { Text = "Not sure" },
new EightBallResponse() { Text = "It is uncertain" },
new EightBallResponse() { Text = "Ask me again later" },
new EightBallResponse() { Text = "Don't count on it" },
new EightBallResponse() { Text = "Probably not" },
new EightBallResponse() { Text = "Very doubtful" },
new EightBallResponse() { Text = "Most likely no" },
new EightBallResponse() { Text = "Nope" },
new EightBallResponse() { Text = "No" },
new EightBallResponse() { Text = "My sources say no" },
new EightBallResponse() { Text = "Dont even think about it" },
new EightBallResponse() { Text = "Definitely no" },
new EightBallResponse() { Text = "NO - It may cause disease contraction" }
});
BotConfig.Add(bc);
this.SaveChanges();
}
}
2016-08-24 13:29:01 +00:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region QUOTES
2016-08-24 17:04:24 +00:00
2017-02-14 13:30:21 +00:00
//var quoteEntity = modelBuilder.Entity<Quote>();
2016-08-24 13:29:01 +00:00
2016-08-24 17:04:24 +00:00
#endregion
2016-08-24 17:04:24 +00:00
#region Donators
2016-08-24 13:29:01 +00:00
2016-08-24 17:04:24 +00:00
var donatorEntity = modelBuilder.Entity<Donator>();
donatorEntity
.HasIndex(d => d.UserId)
2016-08-24 13:29:01 +00:00
.IsUnique();
#endregion
#region GuildConfig
2016-08-24 17:04:24 +00:00
2016-08-24 23:41:11 +00:00
var configEntity = modelBuilder.Entity<GuildConfig>();
2016-08-24 17:04:24 +00:00
configEntity
.HasIndex(c => c.GuildId)
.IsUnique();
modelBuilder.Entity<AntiSpamSetting>()
.HasOne(x => x.GuildConfig)
.WithOne(x => x.AntiSpamSetting);
modelBuilder.Entity<AntiRaidSetting>()
.HasOne(x => x.GuildConfig)
.WithOne(x => x.AntiRaidSetting);
//modelBuilder.Entity<ProtectionIgnoredChannel>()
// .HasAlternateKey(c => new { c.ChannelId, c.ProtectionType });
2016-08-24 13:29:01 +00:00
#endregion
2016-08-25 17:23:47 +00:00
#region BotConfig
2017-02-14 13:30:21 +00:00
//var botConfigEntity = modelBuilder.Entity<BotConfig>();
//botConfigEntity
// .HasMany(c => c.ModulePrefixes)
// .WithOne(mp => mp.BotConfig)
// .HasForeignKey(mp => mp.BotConfigId);
#endregion
2016-08-25 17:23:47 +00:00
#region ClashOfClans
var callersEntity = modelBuilder.Entity<ClashCaller>();
callersEntity
.HasOne(c => c.ClashWar)
.WithMany(c => c.Bases);
#endregion
2016-08-26 17:25:54 +00:00
#region Self Assignable Roles
var selfassignableRolesEntity = modelBuilder.Entity<SelfAssignedRole>();
selfassignableRolesEntity
.HasIndex(s => new { s.GuildId, s.RoleId })
.IsUnique();
#endregion
2016-08-28 20:59:12 +00:00
#region Currency
var currencyEntity = modelBuilder.Entity<Currency>();
currencyEntity
.HasIndex(c => c.UserId)
.IsUnique();
#endregion
#region Permission
var permissionEntity = modelBuilder.Entity<Permission>();
permissionEntity
.HasOne(p => p.Next)
.WithOne(p => p.Previous)
.IsRequired(false);
#endregion
#region LogSettings
//var logSettingEntity = modelBuilder.Entity<LogSetting>();
//logSettingEntity
// .HasMany(ls => ls.IgnoredChannels)
// .WithOne(ls => ls.LogSetting)
// .HasPrincipalKey(ls => ls.id;
//logSettingEntity
// .HasMany(ls => ls.IgnoredVoicePresenceChannelIds)
// .WithOne(ls => ls.LogSetting);
#endregion
2016-10-02 01:00:03 +00:00
#region MusicPlaylists
var musicPlaylistEntity = modelBuilder.Entity<MusicPlaylist>();
musicPlaylistEntity
2016-10-02 02:15:57 +00:00
.HasMany(p => p.Songs)
2016-10-02 01:00:03 +00:00
.WithOne()
.OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Cascade);
#endregion
#region PokeGame
var pokeGameEntity = modelBuilder.Entity<UserPokeTypes>();
pokeGameEntity
.HasIndex(pt => pt.UserId)
.IsUnique();
2016-10-02 01:00:03 +00:00
#endregion
#region CommandPrice
//well, i failed
modelBuilder.Entity<CommandPrice>()
.HasIndex(cp => cp.Price)
.IsUnique();
//modelBuilder.Entity<CommandCost>()
// .HasIndex(cp => cp.CommandName)
// .IsUnique();
2016-10-02 01:00:03 +00:00
#endregion
#region Waifus
var wi = modelBuilder.Entity<WaifuInfo>();
wi.HasOne(x => x.Waifu)
.WithOne();
// //.HasForeignKey<WaifuInfo>(w => w.WaifuId)
// //.IsRequired(true);
//wi.HasOne(x => x.Claimer)
// .WithOne();
// //.HasForeignKey<WaifuInfo>(w => w.ClaimerId)
// //.IsRequired(false);
var du = modelBuilder.Entity<DiscordUser>();
du.HasAlternateKey(w => w.UserId);
#endregion
2017-03-30 01:51:54 +00:00
#region Warnings
var warn = modelBuilder.Entity<Warning>();
#endregion
2017-04-01 22:01:02 +00:00
#region PatreonRewards
var pr = modelBuilder.Entity<RewardedUser>();
pr.HasIndex(x => x.UserId)
.IsUnique();
#endregion
2016-08-24 13:29:01 +00:00
}
}
}