diff --git a/src/NadekoBot/Migrations/20160824125525_QuoteMigration.cs b/src/NadekoBot/Migrations/20160824125525_QuoteMigration.cs deleted file mode 100644 index 22218352..00000000 --- a/src/NadekoBot/Migrations/20160824125525_QuoteMigration.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace NadekoBot.Migrations -{ - public partial class QuoteMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Quotes", - columns: table => new - { - Id = table.Column(nullable: false) - .Annotation("Autoincrement", true), - AuthorId = table.Column(nullable: false), - AuthorName = table.Column(nullable: false), - GuildId = table.Column(nullable: false), - Keyword = table.Column(nullable: false), - Text = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Quotes", x => x.Id); - }); - - migrationBuilder.CreateIndex( - name: "IX_Quotes_GuildId_Keyword", - table: "Quotes", - columns: new[] { "GuildId", "Keyword" }, - unique: true); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Quotes"); - } - } -} diff --git a/src/NadekoBot/Migrations/20160824125525_QuoteMigration.Designer.cs b/src/NadekoBot/Migrations/20160824184118_FirstMigration.Designer.cs similarity index 53% rename from src/NadekoBot/Migrations/20160824125525_QuoteMigration.Designer.cs rename to src/NadekoBot/Migrations/20160824184118_FirstMigration.Designer.cs index 1668813d..2e64b6d5 100644 --- a/src/NadekoBot/Migrations/20160824125525_QuoteMigration.Designer.cs +++ b/src/NadekoBot/Migrations/20160824184118_FirstMigration.Designer.cs @@ -8,14 +8,50 @@ using NadekoBot.Services.Database.Impl; namespace NadekoBot.Migrations { [DbContext(typeof(NadekoSqliteContext))] - [Migration("20160824125525_QuoteMigration")] - partial class QuoteMigration + [Migration("20160824184118_FirstMigration")] + partial class FirstMigration { protected override void BuildTargetModel(ModelBuilder modelBuilder) { modelBuilder .HasAnnotation("ProductVersion", "1.0.0-rtm-21431"); + modelBuilder.Entity("NadekoBot.Services.Database.Models.Config", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("DeleteMessageOnCommand"); + + b.Property("GuildId"); + + b.HasKey("Id"); + + b.HasIndex("GuildId") + .IsUnique(); + + b.ToTable("Configs"); + }); + + modelBuilder.Entity("NadekoBot.Services.Database.Models.Donator", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Amount"); + + b.Property("Name"); + + b.Property("UserId"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("Donators"); + }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b => { b.Property("Id") @@ -36,9 +72,6 @@ namespace NadekoBot.Migrations b.HasKey("Id"); - b.HasIndex("GuildId", "Keyword") - .IsUnique(); - b.ToTable("Quotes"); }); } diff --git a/src/NadekoBot/Migrations/20160824184118_FirstMigration.cs b/src/NadekoBot/Migrations/20160824184118_FirstMigration.cs new file mode 100644 index 00000000..ee8a450f --- /dev/null +++ b/src/NadekoBot/Migrations/20160824184118_FirstMigration.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace NadekoBot.Migrations +{ + public partial class FirstMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Configs", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Autoincrement", true), + DeleteMessageOnCommand = table.Column(nullable: false), + GuildId = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Configs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Donators", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Autoincrement", true), + Amount = table.Column(nullable: false), + Name = table.Column(nullable: true), + UserId = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Donators", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Quotes", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Autoincrement", true), + AuthorId = table.Column(nullable: false), + AuthorName = table.Column(nullable: false), + GuildId = table.Column(nullable: false), + Keyword = table.Column(nullable: false), + Text = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Quotes", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Configs_GuildId", + table: "Configs", + column: "GuildId", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Donators_UserId", + table: "Donators", + column: "UserId", + unique: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Configs"); + + migrationBuilder.DropTable( + name: "Donators"); + + migrationBuilder.DropTable( + name: "Quotes"); + } + } +} diff --git a/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs b/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs index a116fa4a..9328de80 100644 --- a/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs +++ b/src/NadekoBot/Migrations/NadekoSqliteContextModelSnapshot.cs @@ -15,6 +15,42 @@ namespace NadekoBot.Migrations modelBuilder .HasAnnotation("ProductVersion", "1.0.0-rtm-21431"); + modelBuilder.Entity("NadekoBot.Services.Database.Models.Config", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("DeleteMessageOnCommand"); + + b.Property("GuildId"); + + b.HasKey("Id"); + + b.HasIndex("GuildId") + .IsUnique(); + + b.ToTable("Configs"); + }); + + modelBuilder.Entity("NadekoBot.Services.Database.Models.Donator", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("Amount"); + + b.Property("Name"); + + b.Property("UserId"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("Donators"); + }); + modelBuilder.Entity("NadekoBot.Services.Database.Models.Quote", b => { b.Property("Id") @@ -35,9 +71,6 @@ namespace NadekoBot.Migrations b.HasKey("Id"); - b.HasIndex("GuildId", "Keyword") - .IsUnique(); - b.ToTable("Quotes"); }); } diff --git a/src/NadekoBot/Modules/Searches/Commands/CalcCommand.cs b/src/NadekoBot/Modules/Searches/Commands/CalcCommand.cs index 16457743..ad3b7401 100644 --- a/src/NadekoBot/Modules/Searches/Commands/CalcCommand.cs +++ b/src/NadekoBot/Modules/Searches/Commands/CalcCommand.cs @@ -43,7 +43,7 @@ namespace NadekoBot.Modules.Searches [LocalizedCommand, LocalizedDescription, LocalizedSummary] [RequireContext(ContextType.Guild)] - public async Task CalcOperations(IMessage msg) + public async Task CalcOps(IMessage msg) { StringBuilder builder = new StringBuilder(); var selection = typeof(Math).GetTypeInfo().GetMethods().Except(typeof(object).GetTypeInfo().GetMethods()).Select(x => diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index 82c81de2..d678b140 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -213,7 +213,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to ... + /// Looks up a localized string similar to .. /// public static string addquote_text { get { @@ -896,6 +896,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to Shows all available operations in .calc command. + /// + public static string calcops_desc { + get { + return ResourceManager.GetString("calcops_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `.calcops`. + /// + public static string calcops_summary { + get { + return ResourceManager.GetString("calcops_summary", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to calcops. + /// + public static string calcops_text { + get { + return ResourceManager.GetString("calcops_text", resourceCulture); + } + } + /// /// Looks up a localized string similar to Evaluate a mathematical expression.. /// @@ -1949,6 +1976,33 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to Deletes all quotes on a specified keyword.. + /// + public static string delallquotes_desc { + get { + return ResourceManager.GetString("delallquotes_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `.delallq`. + /// + public static string delallquotes_summary { + get { + return ResourceManager.GetString("delallquotes_summary", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to delallq. + /// + public static string delallquotes_text { + get { + return ResourceManager.GetString("delallquotes_text", resourceCulture); + } + } + /// /// Looks up a localized string similar to Deletes a custom reaction with given name (and index). **Bot Owner Only.**. /// @@ -2013,7 +2067,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to `..qdel abc`. + /// Looks up a localized string similar to `.delq abc`. /// public static string deletequote_summary { get { @@ -2022,11 +2076,11 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to ..qdel. + /// Looks up a localized string similar to deletequote. /// - public static string deletequtoe_text { + public static string deletequote_text { get { - return ResourceManager.GetString("deletequtoe_text", resourceCulture); + return ResourceManager.GetString("deletequote_text", resourceCulture); } } @@ -4730,33 +4784,6 @@ namespace NadekoBot.Resources { } } - /// - /// Looks up a localized string similar to Shows a random quote.. - /// - public static string quote_desc { - get { - return ResourceManager.GetString("quote_desc", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to `~quote`. - /// - public static string quote_summary { - get { - return ResourceManager.GetString("quote_summary", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to quote. - /// - public static string quote_text { - get { - return ResourceManager.GetString("quote_text", resourceCulture); - } - } - /// /// Looks up a localized string similar to Starts a new animal race.. /// @@ -6153,7 +6180,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to .... + /// Looks up a localized string similar to ... /// public static string showquote_text { get { diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index d63ba666..3d90ae9c 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1288,7 +1288,7 @@ `;acmdcds` - .. + . Adds a new quote with the specified name (single word) and message (no limit). @@ -1297,7 +1297,7 @@ `.. abc My message` - ... + .. Shows a random quote with a specified name. @@ -1305,14 +1305,14 @@ `... abc` - - ..qdel + + deletequote Deletes all quotes with the specified keyword. You have to either be bot owner or the creator of the quote to delete it. - `..qdel abc` + `.delq abc` rip @@ -2241,15 +2241,6 @@ `~# ff` - - quote - - - Shows a random quote. - - - `~quote` - catfact @@ -2619,4 +2610,22 @@ readme + + Shows all available operations in .calc command + + + `.calcops` + + + calcops + + + Deletes all quotes on a specified keyword. + + + `.delallq` + + + delallq + \ No newline at end of file diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index 0273efa3..8114e6d9 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -10,7 +10,9 @@ namespace NadekoBot.Services.Database { public abstract class NadekoContext : DbContext { - public DbSet Quotes { get; } + public DbSet Quotes { get; set; } + public DbSet Donators { get; set; } + public DbSet Configs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -39,6 +41,9 @@ namespace NadekoBot.Services.Database #endregion } - protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder); + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Filename=./data/NadekoBot.sqlite"); + } } } diff --git a/src/NadekoBot/Services/Database/Repositories/Extensions.cs b/src/NadekoBot/Services/Database/Repositories/Extensions.cs deleted file mode 100644 index ae1587bb..00000000 --- a/src/NadekoBot/Services/Database/Repositories/Extensions.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using NadekoBot.Services.Database.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading.Tasks; - -namespace NadekoBot.Services.Database.Repositories -{ - //some guy from stack overflow wrotethis, thanks to him :D - public static class Extensions - { - public static TEntity Find(this DbSet set, params object[] keyValues) where TEntity : DbEntity - { - var context = ((IInfrastructure)set).GetService(); - - var entityType = context.Model.FindEntityType(typeof(TEntity)); - var key = entityType.FindPrimaryKey(); - - var entries = context.ChangeTracker.Entries(); - - var i = 0; - foreach (var property in key.Properties) - { - entries = entries.Where(e => e.Property(property.Name).CurrentValue == keyValues[i]); - i++; - } - - var entry = entries.FirstOrDefault(); - if (entry != null) - { - // Return the local object if it exists. - return entry.Entity; - } - - // set.Where(x => x.Id == keyValues[0]); - var parameter = Expression.Parameter(typeof(TEntity), "x"); - var query = set.Where((Expression>) - Expression.Lambda( - Expression.Equal( - Expression.Property(parameter, "Id"), - Expression.Constant(keyValues[0])), - parameter)); - - // Look in the database - return query.FirstOrDefault(); - } - } -} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/DonatorsRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/DonatorsRepository.cs index b0fc3cd1..5cce62ae 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/DonatorsRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/DonatorsRepository.cs @@ -16,19 +16,21 @@ namespace NadekoBot.Services.Database.Repositories.Impl public Donator AddOrUpdateDonator(ulong userId, string name, int amount) { - var donator = _set.Find(userId); + var donator = _set.Where(d => d.UserId == userId).FirstOrDefault(); if (donator == null) { _set.Add(donator = new Donator { Amount = amount, - UserId = userId + UserId = userId, + Name = name }); } else { donator.Amount += amount; + donator.Name = name; _set.Update(donator); } diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs index a2e95121..7207f455 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/QuoteRepository.cs @@ -18,10 +18,10 @@ namespace NadekoBot.Services.Database.Repositories.Impl public IEnumerable GetAllQuotesByKeyword(string keyword) => _set.Where(q => q.Keyword == keyword); - public Task GetRandomQuoteByKeywordAsync(ulong guildId, string text) + public Task GetRandomQuoteByKeywordAsync(ulong guildId, string keyword) { var rng = new Random(); - return _set.Where(q => q.Text == text).OrderBy(q => rng.Next()).FirstOrDefaultAsync(); + return _set.Where(q => q.Keyword == keyword).OrderBy(q => rng.Next()).FirstOrDefaultAsync(); } } } diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/Repository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/Repository.cs index caaeddf8..cf35dec6 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/Repository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/Repository.cs @@ -26,13 +26,13 @@ namespace NadekoBot.Services.Database.Repositories.Impl _set.AddRange(objs); public T Get(int id) => - _set.Find(id); + _set.FirstOrDefault(e => e.Id == id); public IEnumerable GetAll() => _set.ToList(); public void Remove(int id) => - _set.Remove(_set.Find(id)); + _set.Remove(this.Get(id)); public void Remove(T obj) => _set.Remove(obj); diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index 3c2d60f4..9eec7d35 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -23,7 +23,7 @@ namespace NadekoBot.Services.Database public UnitOfWork(NadekoContext context) { - + _context = context; } public int Complete() =>