diff --git a/src/NadekoBot/Migrations/20160824223801_FirstMigration.Designer.cs b/src/NadekoBot/Migrations/20160824232035_FirstMigration.Designer.cs similarity index 97% rename from src/NadekoBot/Migrations/20160824223801_FirstMigration.Designer.cs rename to src/NadekoBot/Migrations/20160824232035_FirstMigration.Designer.cs index b482fe7e..1a614906 100644 --- a/src/NadekoBot/Migrations/20160824223801_FirstMigration.Designer.cs +++ b/src/NadekoBot/Migrations/20160824232035_FirstMigration.Designer.cs @@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl; namespace NadekoBot.Migrations { [DbContext(typeof(NadekoSqliteContext))] - [Migration("20160824223801_FirstMigration")] + [Migration("20160824232035_FirstMigration")] partial class FirstMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/src/NadekoBot/Migrations/20160824223801_FirstMigration.cs b/src/NadekoBot/Migrations/20160824232035_FirstMigration.cs similarity index 100% rename from src/NadekoBot/Migrations/20160824223801_FirstMigration.cs rename to src/NadekoBot/Migrations/20160824232035_FirstMigration.cs diff --git a/src/NadekoBot/Modules/Administration/Administration.cs b/src/NadekoBot/Modules/Administration/Administration.cs index 2811da66..345b8949 100644 --- a/src/NadekoBot/Modules/Administration/Administration.cs +++ b/src/NadekoBot/Modules/Administration/Administration.cs @@ -43,7 +43,7 @@ namespace NadekoBot.Modules.Administration public async Task Delmsgoncmd(IMessage imsg) { var channel = (ITextChannel)imsg.Channel; - Config conf; + GuildConfig conf; using (var uow = DbHandler.UnitOfWork()) { conf = uow.GuildConfigs.For(channel.Guild.Id); diff --git a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs index 2be6466a..bd847420 100644 --- a/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs +++ b/src/NadekoBot/Modules/Administration/Commands/AutoAssignRoleCommands.cs @@ -21,7 +21,7 @@ namespace NadekoBot.Modules.Administration var _client = NadekoBot.Client; _client.UserJoined += async (user) => { - Config conf; + GuildConfig conf; using (var uow = DbHandler.UnitOfWork()) { conf = uow.GuildConfigs.For(user.Guild.Id); @@ -45,7 +45,7 @@ namespace NadekoBot.Modules.Administration { var channel = (ITextChannel)imsg.Channel; - Config conf; + GuildConfig conf; using (var uow = DbHandler.UnitOfWork()) { conf = uow.GuildConfigs.For(channel.Guild.Id); diff --git a/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs b/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs similarity index 98% rename from src/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs rename to src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs index ccd20a36..4b2f4d31 100644 --- a/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs +++ b/src/NadekoBot/Modules/Administration/Commands/ServerGreetCommands.cs @@ -9,14 +9,14 @@ //// todo rewrite //namespace NadekoBot.Modules.Administration //{ -// internal class ServerGreetCommand : DiscordCommand +// public partial class ServerGreetCommands // { // public static ConcurrentDictionary AnnouncementsDictionary; // public static long Greeted = 0; -// public ServerGreetCommand(DiscordModule module) : base(module) +// public ServerGreetCommands(DiscordModule module) // { // AnnouncementsDictionary = new ConcurrentDictionary(); @@ -68,7 +68,7 @@ // catch { } // } -// private async void UserJoined(object sender, Discord.UserEventArgs e) +// private async Task UserJoined(object sender, Discord.UserEventArgs e) // { // try // { diff --git a/src/NadekoBot/Services/Database/Models/Config.cs b/src/NadekoBot/Services/Database/Models/GuildConfig.cs similarity index 89% rename from src/NadekoBot/Services/Database/Models/Config.cs rename to src/NadekoBot/Services/Database/Models/GuildConfig.cs index 05900703..b2d16bd5 100644 --- a/src/NadekoBot/Services/Database/Models/Config.cs +++ b/src/NadekoBot/Services/Database/Models/GuildConfig.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace NadekoBot.Services.Database.Models { - public class Config : DbEntity + public class GuildConfig : DbEntity { public ulong GuildId { get; set; } public bool DeleteMessageOnCommand { get; set; } diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index 7b698a59..8f2c2a1f 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -12,7 +12,7 @@ namespace NadekoBot.Services.Database { public DbSet Quotes { get; set; } public DbSet Donators { get; set; } - public DbSet GuildConfigs { get; set; } + public DbSet GuildConfigs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -34,7 +34,7 @@ namespace NadekoBot.Services.Database #region Config - var configEntity = modelBuilder.Entity(); + var configEntity = modelBuilder.Entity(); configEntity .HasIndex(c => c.GuildId) .IsUnique(); diff --git a/src/NadekoBot/Services/Database/Repositories/IConfigRepository.cs b/src/NadekoBot/Services/Database/Repositories/IConfigRepository.cs index 7d6d56d4..b1544711 100644 --- a/src/NadekoBot/Services/Database/Repositories/IConfigRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/IConfigRepository.cs @@ -8,8 +8,8 @@ using System.Threading.Tasks; namespace NadekoBot.Services.Database.Repositories { - public interface IConfigRepository : IRepository + public interface IConfigRepository : IRepository { - Config For(ulong guildId); + GuildConfig For(ulong guildId); } } diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/ConfigRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/ConfigRepository.cs index 7a0e2a75..6df83e3b 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/ConfigRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/ConfigRepository.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore; namespace NadekoBot.Services.Database.Repositories.Impl { - public class ConfigRepository : Repository, IConfigRepository + public class ConfigRepository : Repository, IConfigRepository { public ConfigRepository(DbContext context) : base(context) { @@ -18,16 +18,17 @@ namespace NadekoBot.Services.Database.Repositories.Impl /// /// /// - public Config For(ulong guildId) + public GuildConfig For(ulong guildId) { - var config = _set.Where(c => c.GuildId == guildId).FirstOrDefault(); + var config = _set.FirstOrDefault(c => c.GuildId == guildId); if (config == null) { - _set.Add((config = new Config + _set.Add((config = new GuildConfig { GuildId = guildId })); + _context.SaveChanges(); } return config; } diff --git a/src/NadekoBot/_Classes/DBHandler.cs b/src/NadekoBot/_Classes/DBHandler.cs deleted file mode 100644 index db8dee81..00000000 --- a/src/NadekoBot/_Classes/DBHandler.cs +++ /dev/null @@ -1,164 +0,0 @@ -using NadekoBot.DataModels; -using SQLite; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; - -namespace NadekoBot.Classes -{ - internal class DbHandler - { - public static DbHandler Instance { get; } = new DbHandler(); - - private string FilePath { get; } = "data/nadekobot.sqlite"; - - public SQLiteConnection Connection { get; set; } - - static DbHandler() { } - public DbHandler() - { - Connection = new SQLiteConnection(FilePath); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.Execute(Queries.TransactionTriggerQuery); - try - { - Connection.Execute(Queries.DeletePlaylistTriggerQuery); - } - catch (Exception ex) - { - Console.WriteLine(ex); - } - } - - internal T FindOne(Expression> p) where T : IDataModel, new() - { - return Connection.Table().Where(p).FirstOrDefault(); - - } - - internal IList FindAll(Expression> p) where T : IDataModel, new() - { - - return Connection.Table().Where(p).ToList(); - - } - - internal void DeleteWhere(Expression> p) where T : IDataModel, new() - { - var id = Connection.Table().Where(p).FirstOrDefault()?.Id; - if (id.HasValue) - Connection.Delete(id); - } - - internal HashSet GetAllRows() where T : IDataModel, new() - { - return new HashSet(Connection.Table()); - } - - internal CurrencyState GetStateByUserId(long id) - { - return Connection.Table().Where(x => x.UserId == id).FirstOrDefault(); - } - - internal T Delete(int id) where T : IDataModel, new() - { - var found = Connection.Find(id); - if (found != null) - Connection.Delete(found.Id); - return found; - } - - /// - /// Updates an existing object or creates a new one - /// - internal void Save(T o) where T : IDataModel, new() - { - var found = Connection.Find(o.Id); - if (found == null) - Connection.Insert(o, typeof(T)); - else - Connection.Update(o, typeof(T)); - } - - /// - /// Updates an existing object or creates a new one - /// - internal void SaveAll(IEnumerable ocol) where T : IDataModel, new() - { - foreach (var o in ocol) - Connection.InsertOrReplace(o); - } - - internal T GetRandom(Expression> p) where T : IDataModel, new() - { - var r = new Random(); - return Connection.Table().Where(p).ToList().OrderBy(x => r.Next()).FirstOrDefault(); - } - /// - /// - /// - /// Page number (0+) - /// - internal List GetPlaylistData(int num) - { - return Connection.Query( -@"SELECT mp.Name as 'Name',mp.Id as 'Id', mp.CreatorName as 'Creator', Count(*) as 'SongCnt' FROM MusicPlaylist as mp -INNER JOIN PlaylistSongInfo as psi -ON mp.Id = psi.PlaylistId -Group BY mp.Name -Order By mp.DateAdded desc -Limit 20 OFFSET ?", num * 20); - - } - - internal IEnumerable GetTopRichest(int n = 10) - { - return Connection.Table().OrderByDescending(cs => cs.Value).Take(n).ToList(); - } - } -} - -public class PlaylistData -{ - public string Name { get; set; } - public int Id { get; set; } - public string Creator { get; set; } - public int SongCnt { get; set; } -} - -public static class Queries -{ - public const string TransactionTriggerQuery = @" -CREATE TRIGGER IF NOT EXISTS OnTransactionAdded -AFTER INSERT ON CurrencyTransaction -BEGIN -INSERT OR REPLACE INTO CurrencyState (Id, UserId, Value, DateAdded) - VALUES (COALESCE((SELECT Id from CurrencyState where UserId = NEW.UserId),(SELECT COALESCE(MAX(Id),0)+1 from CurrencyState)), - NEW.UserId, - COALESCE((SELECT Value+New.Value FROM CurrencyState Where UserId = NEW.UserId),NEW.Value), - NEW.DateAdded); -END -"; - public const string DeletePlaylistTriggerQuery = @" -CREATE TRIGGER IF NOT EXISTS music_playlist -AFTER DELETE ON MusicPlaylist -FOR EACH ROW -BEGIN - DELETE FROM PlaylistSongInfo WHERE PlaylistId = OLD.Id; -END"; -}