diff --git a/Geekbot.net/Database/DatabaseContext.cs b/Geekbot.net/Database/DatabaseContext.cs index 78701b8..e4fab28 100644 --- a/Geekbot.net/Database/DatabaseContext.cs +++ b/Geekbot.net/Database/DatabaseContext.cs @@ -14,7 +14,9 @@ namespace Geekbot.net.Database public DbSet Rolls { get; set; } public DbSet Messages { get; set; } public DbSet Slaps { get; set; } -// public DbSet UserSettings { get; set; } -// public DbSet RoleSelfService { get; set; } + public DbSet Globals { get; set; } + + // public DbSet UserSettings { get; set; } + // public DbSet RoleSelfService { get; set; } } } \ No newline at end of file diff --git a/Geekbot.net/Database/Models/GlobalsModel.cs b/Geekbot.net/Database/Models/GlobalsModel.cs new file mode 100644 index 0000000..63261fc --- /dev/null +++ b/Geekbot.net/Database/Models/GlobalsModel.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Geekbot.net.Database.Models +{ + public class GlobalsModel + { + [Key] + public int Id { get; set; } + + [Required] + public string Name { get; set; } + + [Required] + public string Value { get; set; } + + public string Meta { get; set; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Database/RedisMigration.cs b/Geekbot.net/Database/RedisMigration.cs index 6c8e0d0..1cd73b0 100644 --- a/Geekbot.net/Database/RedisMigration.cs +++ b/Geekbot.net/Database/RedisMigration.cs @@ -8,6 +8,7 @@ using Geekbot.net.Commands.Utils.Quote; using Geekbot.net.Database.Models; using Geekbot.net.Lib.Extensions; using Geekbot.net.Lib.Logger; +using MtgApiManager.Lib.Model; using Newtonsoft.Json; using StackExchange.Redis; @@ -172,8 +173,6 @@ namespace Geekbot.net.Database { try { - // drop the guild qounter - if(q.Name.ToString() != "0") continue; var user = new MessagesModel() { GuildId = guild.Id.AsLong(), @@ -242,16 +241,18 @@ namespace Geekbot.net.Database { try { - _database.Users.Add(new UserModel() - { - UserId = user.Id.AsLong(), - Username = user.Username, - Discriminator = user.Discriminator, - AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), - IsBot = user.IsBot, - Joined = user.CreatedAt, - UsedNames = new [] {user.Username} - }); + var namesSerialized = _redis.HashGet($"User:{user.Id}", "UsedNames").ToString(); + var names = Utf8Json.JsonSerializer.Deserialize(namesSerialized); + _database.Users.AddIfNotExists(new UserModel() + { + UserId = user.Id.AsLong(), + Username = user.Username, + Discriminator = user.Discriminator, + AvatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 1024), + IsBot = user.IsBot, + Joined = user.CreatedAt, + UsedNames = names + }, model => model.UserId.Equals(user.Id.AsLong())); _database.SaveChanges(); } catch diff --git a/Geekbot.net/Lib/Extensions/DbSetExtensions.cs b/Geekbot.net/Lib/Extensions/DbSetExtensions.cs new file mode 100644 index 0000000..c731fc0 --- /dev/null +++ b/Geekbot.net/Lib/Extensions/DbSetExtensions.cs @@ -0,0 +1,17 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; + +namespace Geekbot.net.Lib.Extensions +{ + public static class DbSetExtensions + { + public static EntityEntry AddIfNotExists(this DbSet dbSet, T entity, Expression> predicate = null) where T : class, new() + { + var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any(); + return !exists ? dbSet.Add(entity) : null; + } + } +} \ No newline at end of file