Db stuff, quotes implemented?
This commit is contained in:
parent
03ac084437
commit
2a2d26513a
@ -8,8 +8,8 @@ using NadekoBot.Services.Database.Impl;
|
|||||||
namespace NadekoBot.Migrations
|
namespace NadekoBot.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(NadekoSqliteContext))]
|
[DbContext(typeof(NadekoSqliteContext))]
|
||||||
[Migration("20160824013005_FirstMigration")]
|
[Migration("20160824125525_QuoteMigration")]
|
||||||
partial class FirstMigration
|
partial class QuoteMigration
|
||||||
{
|
{
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
@ -21,14 +21,24 @@ namespace NadekoBot.Migrations
|
|||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
.ValueGeneratedOnAdd();
|
.ValueGeneratedOnAdd();
|
||||||
|
|
||||||
b.Property<string>("Keyword");
|
b.Property<ulong>("AuthorId");
|
||||||
|
|
||||||
b.Property<string>("Text");
|
b.Property<string>("AuthorName")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
b.Property<string>("UserName");
|
b.Property<ulong>("GuildId");
|
||||||
|
|
||||||
|
b.Property<string>("Keyword")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Property<string>("Text")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("GuildId", "Keyword")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
b.ToTable("Quotes");
|
b.ToTable("Quotes");
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||||||
|
|
||||||
namespace NadekoBot.Migrations
|
namespace NadekoBot.Migrations
|
||||||
{
|
{
|
||||||
public partial class FirstMigration : Migration
|
public partial class QuoteMigration : Migration
|
||||||
{
|
{
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
@ -14,14 +14,22 @@ namespace NadekoBot.Migrations
|
|||||||
{
|
{
|
||||||
Id = table.Column<int>(nullable: false)
|
Id = table.Column<int>(nullable: false)
|
||||||
.Annotation("Autoincrement", true),
|
.Annotation("Autoincrement", true),
|
||||||
Keyword = table.Column<string>(nullable: true),
|
AuthorId = table.Column<ulong>(nullable: false),
|
||||||
Text = table.Column<string>(nullable: true),
|
AuthorName = table.Column<string>(nullable: false),
|
||||||
UserName = table.Column<string>(nullable: true)
|
GuildId = table.Column<ulong>(nullable: false),
|
||||||
|
Keyword = table.Column<string>(nullable: false),
|
||||||
|
Text = table.Column<string>(nullable: false)
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_Quotes", x => x.Id);
|
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)
|
protected override void Down(MigrationBuilder migrationBuilder)
|
@ -20,14 +20,24 @@ namespace NadekoBot.Migrations
|
|||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
.ValueGeneratedOnAdd();
|
.ValueGeneratedOnAdd();
|
||||||
|
|
||||||
b.Property<string>("Keyword");
|
b.Property<ulong>("AuthorId");
|
||||||
|
|
||||||
b.Property<string>("Text");
|
b.Property<string>("AuthorName")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
b.Property<string>("UserName");
|
b.Property<ulong>("GuildId");
|
||||||
|
|
||||||
|
b.Property<string>("Keyword")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Property<string>("Text")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
b.HasKey("Id");
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("GuildId", "Keyword")
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
b.ToTable("Quotes");
|
b.ToTable("Quotes");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -313,16 +313,6 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
|||||||
await channel.SendMessageAsync("💢 Failed finding a definition for that tag.").ConfigureAwait(false);
|
await channel.SendMessageAsync("💢 Failed finding a definition for that tag.").ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//todo DB
|
|
||||||
//[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
|
||||||
//[RequireContext(ContextType.Guild)]
|
|
||||||
//public async Task Quote(IMessage imsg)
|
|
||||||
//{
|
|
||||||
// var channel = (ITextChannel)imsg.Channel;
|
|
||||||
|
|
||||||
// var quote = NadekoBot.Config.Quotes[rng.Next(0, NadekoBot.Config.Quotes.Count)].ToString();
|
|
||||||
// await channel.SendMessageAsync(quote).ConfigureAwait(false);
|
|
||||||
//}
|
|
||||||
|
|
||||||
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
|
114
src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs
Normal file
114
src/NadekoBot/Modules/Utility/Commands/QuoteCommands.cs
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using NadekoBot.Attributes;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
using NadekoBot.Services.Database;
|
||||||
|
using NadekoBot.Services.Database.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace NadekoBot.Modules.Utility.Commands
|
||||||
|
{
|
||||||
|
public partial class Utility
|
||||||
|
{
|
||||||
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task Quote(IMessage imsg, string keyword)
|
||||||
|
{
|
||||||
|
var channel = imsg.Channel as ITextChannel;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(keyword))
|
||||||
|
return;
|
||||||
|
|
||||||
|
Quote quote;
|
||||||
|
using (var uow = DbHandler.Instance.GetUnitOfWork())
|
||||||
|
{
|
||||||
|
quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(channel.Guild.Id, keyword);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (quote == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await channel.SendMessageAsync(":megaphone: " + quote.Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task AddQuote(IMessage imsg, string keyword, [Remainder] string text)
|
||||||
|
{
|
||||||
|
var channel = imsg.Channel as ITextChannel;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
|
||||||
|
return;
|
||||||
|
|
||||||
|
keyword = keyword.ToUpperInvariant();
|
||||||
|
|
||||||
|
using (var uow = DbHandler.UnitOfWork())
|
||||||
|
{
|
||||||
|
uow.Quotes.Add(new Quote
|
||||||
|
{
|
||||||
|
AuthorId = imsg.Author.Id,
|
||||||
|
AuthorName = imsg.Author.Username,
|
||||||
|
GuildId = channel.Guild.Id,
|
||||||
|
Keyword = keyword,
|
||||||
|
Text = text,
|
||||||
|
});
|
||||||
|
await uow.CompleteAsync();
|
||||||
|
await channel.SendMessageAsync("`Quote added.`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task DelQuote(IMessage imsg, string keyword)
|
||||||
|
{
|
||||||
|
var channel = imsg.Channel as ITextChannel;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(keyword))
|
||||||
|
return;
|
||||||
|
|
||||||
|
keyword = keyword.ToUpperInvariant();
|
||||||
|
|
||||||
|
using (var uow = DbHandler.UnitOfWork())
|
||||||
|
{
|
||||||
|
var q = await uow.Quotes.GetRandomQuoteByKeywordAsync(channel.Guild.Id, keyword);
|
||||||
|
|
||||||
|
if (q == null)
|
||||||
|
{
|
||||||
|
await channel.SendMessageAsync("`No quotes found.`");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uow.Quotes.Remove(q);
|
||||||
|
await uow.CompleteAsync();
|
||||||
|
}
|
||||||
|
await channel.SendMessageAsync("`Deleted a random quote.`");
|
||||||
|
}
|
||||||
|
|
||||||
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task DelAllQuotes(IMessage imsg, string keyword)
|
||||||
|
{
|
||||||
|
var channel = imsg.Channel as ITextChannel;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(keyword))
|
||||||
|
return;
|
||||||
|
|
||||||
|
keyword = keyword.ToUpperInvariant();
|
||||||
|
|
||||||
|
using (var uow = DbHandler.UnitOfWork())
|
||||||
|
{
|
||||||
|
var quotes = uow.Quotes.GetAllQuotesByKeyword(keyword);
|
||||||
|
|
||||||
|
uow.Quotes.RemoveRange(quotes.Select(q => q.Id).ToArray());//wtf?!
|
||||||
|
|
||||||
|
await uow.CompleteAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
await channel.SendMessageAsync($"`Deleted all quotes with '{keyword}' keyword`");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ namespace NadekoBot.Services.Database
|
|||||||
public interface IUnitOfWork : IDisposable
|
public interface IUnitOfWork : IDisposable
|
||||||
{
|
{
|
||||||
IQuoteRepository Quotes { get; }
|
IQuoteRepository Quotes { get; }
|
||||||
Task<int> Complete();
|
int Complete();
|
||||||
|
Task<int> CompleteAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using NadekoBot.Services.Database.Models;
|
||||||
|
|
||||||
namespace NadekoBot.Services.Database.Impl
|
namespace NadekoBot.Services.Database.Impl
|
||||||
{
|
{
|
||||||
|
@ -9,8 +9,13 @@ namespace NadekoBot.Services.Database.Models
|
|||||||
{
|
{
|
||||||
public class Quote : DbEntity
|
public class Quote : DbEntity
|
||||||
{
|
{
|
||||||
public string UserName { get; set; }
|
public ulong GuildId { get; set; }
|
||||||
|
[Required]
|
||||||
public string Keyword { get; set; }
|
public string Keyword { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string AuthorName { get; set; }
|
||||||
|
public ulong AuthorId { get; set; }
|
||||||
|
[Required]
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,29 @@ namespace NadekoBot.Services.Database
|
|||||||
{
|
{
|
||||||
public DbSet<Quote> Quotes { get; }
|
public DbSet<Quote> Quotes { get; }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#region QUOTES
|
||||||
|
//// guildid and keyword are unique pair
|
||||||
|
var quoteEntity = modelBuilder.Entity<Quote>();
|
||||||
|
//quoteEntity
|
||||||
|
// .HasAlternateKey(q => q.GuildId)
|
||||||
|
// .HasName("AK_GuildId_Keyword");
|
||||||
|
|
||||||
|
//quoteEntity
|
||||||
|
// .HasAlternateKey(q => q.Keyword)
|
||||||
|
// .HasName("AK_GuildId_Keyword");
|
||||||
|
|
||||||
|
quoteEntity
|
||||||
|
.HasIndex(q => new { q.GuildId, q.Keyword })
|
||||||
|
.IsUnique();
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder);
|
protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ namespace NadekoBot.Services.Database.Repositories
|
|||||||
{
|
{
|
||||||
public interface IQuoteRepository : IRepository<Quote>
|
public interface IQuoteRepository : IRepository<Quote>
|
||||||
{
|
{
|
||||||
IEnumerable<Quote> GetQuotesByText(string text);
|
IEnumerable<Quote> GetAllQuotesByKeyword(string keyword);
|
||||||
Task<Quote> GetRandomQuoteByTextAsync(string text);
|
Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ namespace NadekoBot.Services.Database.Repositories
|
|||||||
|
|
||||||
void Remove(int id);
|
void Remove(int id);
|
||||||
void Remove(T obj);
|
void Remove(T obj);
|
||||||
void RemoveRange(params T[] ids);
|
void RemoveRange(params int[] ids);
|
||||||
|
|
||||||
void Update(T obj);
|
void Update(T obj);
|
||||||
void UpdateRange(params T[] objs);
|
void UpdateRange(params T[] objs);
|
||||||
|
@ -15,10 +15,10 @@ namespace NadekoBot.Services.Database.Repositories.Impl
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Quote> GetQuotesByText(string text) =>
|
public IEnumerable<Quote> GetAllQuotesByKeyword(string keyword) =>
|
||||||
_set.Where(q => q.Text == text);
|
_set.Where(q => q.Keyword == keyword);
|
||||||
|
|
||||||
public Task<Quote> GetRandomQuoteByTextAsync(string text)
|
public Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string text)
|
||||||
{
|
{
|
||||||
var rng = new Random();
|
var rng = new Random();
|
||||||
return _set.Where(q => q.Text == text).OrderBy(q => rng.Next()).FirstOrDefaultAsync();
|
return _set.Where(q => q.Text == text).OrderBy(q => rng.Next()).FirstOrDefaultAsync();
|
||||||
|
@ -21,7 +21,10 @@ namespace NadekoBot.Services.Database
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<int> Complete() =>
|
public int Complete() =>
|
||||||
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
public Task<int> CompleteAsync() =>
|
||||||
_context.SaveChangesAsync();
|
_context.SaveChangesAsync();
|
||||||
|
|
||||||
private bool disposed = false;
|
private bool disposed = false;
|
||||||
|
@ -38,5 +38,8 @@ namespace NadekoBot.Services
|
|||||||
|
|
||||||
public UnitOfWork GetUnitOfWork() =>
|
public UnitOfWork GetUnitOfWork() =>
|
||||||
new UnitOfWork(GetDbContext());
|
new UnitOfWork(GetDbContext());
|
||||||
|
|
||||||
|
public static UnitOfWork UnitOfWork() =>
|
||||||
|
DbHandler.Instance.GetUnitOfWork();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user