Fixed config not updating

This commit is contained in:
Kwoth 2016-08-25 01:41:11 +02:00
parent 4b080590b2
commit c09a851f74
10 changed files with 17 additions and 180 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -9,14 +9,14 @@
//// todo rewrite
//namespace NadekoBot.Modules.Administration
//{
// internal class ServerGreetCommand : DiscordCommand
// public partial class ServerGreetCommands
// {
// public static ConcurrentDictionary<ulong, AnnounceControls> AnnouncementsDictionary;
// public static long Greeted = 0;
// public ServerGreetCommand(DiscordModule module) : base(module)
// public ServerGreetCommands(DiscordModule module)
// {
// AnnouncementsDictionary = new ConcurrentDictionary<ulong, AnnounceControls>();
@ -68,7 +68,7 @@
// catch { }
// }
// private async void UserJoined(object sender, Discord.UserEventArgs e)
// private async Task UserJoined(object sender, Discord.UserEventArgs e)
// {
// try
// {

View File

@ -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; }

View File

@ -12,7 +12,7 @@ namespace NadekoBot.Services.Database
{
public DbSet<Quote> Quotes { get; set; }
public DbSet<Donator> Donators { get; set; }
public DbSet<Config> GuildConfigs { get; set; }
public DbSet<GuildConfig> GuildConfigs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -34,7 +34,7 @@ namespace NadekoBot.Services.Database
#region Config
var configEntity = modelBuilder.Entity<Config>();
var configEntity = modelBuilder.Entity<GuildConfig>();
configEntity
.HasIndex(c => c.GuildId)
.IsUnique();

View File

@ -8,8 +8,8 @@ using System.Threading.Tasks;
namespace NadekoBot.Services.Database.Repositories
{
public interface IConfigRepository : IRepository<Config>
public interface IConfigRepository : IRepository<GuildConfig>
{
Config For(ulong guildId);
GuildConfig For(ulong guildId);
}
}

View File

@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore;
namespace NadekoBot.Services.Database.Repositories.Impl
{
public class ConfigRepository : Repository<Config>, IConfigRepository
public class ConfigRepository : Repository<GuildConfig>, IConfigRepository
{
public ConfigRepository(DbContext context) : base(context)
{
@ -18,16 +18,17 @@ namespace NadekoBot.Services.Database.Repositories.Impl
/// </summary>
/// <param name="guildId"></param>
/// <returns></returns>
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;
}

View File

@ -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<Stats>();
Connection.CreateTable<Command>();
Connection.CreateTable<Announcement>();
Connection.CreateTable<Request>();
Connection.CreateTable<TypingArticle>();
Connection.CreateTable<CurrencyState>();
Connection.CreateTable<CurrencyTransaction>();
Connection.CreateTable<Donator>();
Connection.CreateTable<UserPokeTypes>();
Connection.CreateTable<UserQuote>();
Connection.CreateTable<Reminder>();
Connection.CreateTable<SongInfo>();
Connection.CreateTable<PlaylistSongInfo>();
Connection.CreateTable<MusicPlaylist>();
Connection.CreateTable<Incident>();
Connection.Execute(Queries.TransactionTriggerQuery);
try
{
Connection.Execute(Queries.DeletePlaylistTriggerQuery);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
internal T FindOne<T>(Expression<Func<T, bool>> p) where T : IDataModel, new()
{
return Connection.Table<T>().Where(p).FirstOrDefault();
}
internal IList<T> FindAll<T>(Expression<Func<T, bool>> p) where T : IDataModel, new()
{
return Connection.Table<T>().Where(p).ToList();
}
internal void DeleteWhere<T>(Expression<Func<T, bool>> p) where T : IDataModel, new()
{
var id = Connection.Table<T>().Where(p).FirstOrDefault()?.Id;
if (id.HasValue)
Connection.Delete<T>(id);
}
internal HashSet<T> GetAllRows<T>() where T : IDataModel, new()
{
return new HashSet<T>(Connection.Table<T>());
}
internal CurrencyState GetStateByUserId(long id)
{
return Connection.Table<CurrencyState>().Where(x => x.UserId == id).FirstOrDefault();
}
internal T Delete<T>(int id) where T : IDataModel, new()
{
var found = Connection.Find<T>(id);
if (found != null)
Connection.Delete<T>(found.Id);
return found;
}
/// <summary>
/// Updates an existing object or creates a new one
/// </summary>
internal void Save<T>(T o) where T : IDataModel, new()
{
var found = Connection.Find<T>(o.Id);
if (found == null)
Connection.Insert(o, typeof(T));
else
Connection.Update(o, typeof(T));
}
/// <summary>
/// Updates an existing object or creates a new one
/// </summary>
internal void SaveAll<T>(IEnumerable<T> ocol) where T : IDataModel, new()
{
foreach (var o in ocol)
Connection.InsertOrReplace(o);
}
internal T GetRandom<T>(Expression<Func<T, bool>> p) where T : IDataModel, new()
{
var r = new Random();
return Connection.Table<T>().Where(p).ToList().OrderBy(x => r.Next()).FirstOrDefault();
}
/// <summary>
///
/// </summary>
/// <param name="num">Page number (0+)</param>
/// <returns></returns>
internal List<PlaylistData> GetPlaylistData(int num)
{
return Connection.Query<PlaylistData>(
@"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<CurrencyState> GetTopRichest(int n = 10)
{
return Connection.Table<CurrencyState>().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";
}