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
|
||||
{
|
||||
[DbContext(typeof(NadekoSqliteContext))]
|
||||
[Migration("20160824013005_FirstMigration")]
|
||||
partial class FirstMigration
|
||||
[Migration("20160824125525_QuoteMigration")]
|
||||
partial class QuoteMigration
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
@ -21,14 +21,24 @@ namespace NadekoBot.Migrations
|
||||
b.Property<int>("Id")
|
||||
.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.HasIndex("GuildId", "Keyword")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Quotes");
|
||||
});
|
||||
}
|
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace NadekoBot.Migrations
|
||||
{
|
||||
public partial class FirstMigration : Migration
|
||||
public partial class QuoteMigration : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
@ -14,14 +14,22 @@ namespace NadekoBot.Migrations
|
||||
{
|
||||
Id = table.Column<int>(nullable: false)
|
||||
.Annotation("Autoincrement", true),
|
||||
Keyword = table.Column<string>(nullable: true),
|
||||
Text = table.Column<string>(nullable: true),
|
||||
UserName = table.Column<string>(nullable: true)
|
||||
AuthorId = table.Column<ulong>(nullable: false),
|
||||
AuthorName = table.Column<string>(nullable: false),
|
||||
GuildId = table.Column<ulong>(nullable: false),
|
||||
Keyword = table.Column<string>(nullable: false),
|
||||
Text = table.Column<string>(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)
|
@ -20,14 +20,24 @@ namespace NadekoBot.Migrations
|
||||
b.Property<int>("Id")
|
||||
.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.HasIndex("GuildId", "Keyword")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Quotes");
|
||||
});
|
||||
}
|
||||
|
@ -313,16 +313,6 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
||||
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]
|
||||
[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
|
||||
{
|
||||
IQuoteRepository Quotes { get; }
|
||||
Task<int> Complete();
|
||||
int Complete();
|
||||
Task<int> CompleteAsync();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Services.Database.Models;
|
||||
|
||||
namespace NadekoBot.Services.Database.Impl
|
||||
{
|
||||
|
@ -9,8 +9,13 @@ namespace NadekoBot.Services.Database.Models
|
||||
{
|
||||
public class Quote : DbEntity
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
public ulong GuildId { get; set; }
|
||||
[Required]
|
||||
public string Keyword { get; set; }
|
||||
[Required]
|
||||
public string AuthorName { get; set; }
|
||||
public ulong AuthorId { get; set; }
|
||||
[Required]
|
||||
public string Text { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,29 @@ namespace NadekoBot.Services.Database
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ namespace NadekoBot.Services.Database.Repositories
|
||||
{
|
||||
public interface IQuoteRepository : IRepository<Quote>
|
||||
{
|
||||
IEnumerable<Quote> GetQuotesByText(string text);
|
||||
Task<Quote> GetRandomQuoteByTextAsync(string text);
|
||||
IEnumerable<Quote> GetAllQuotesByKeyword(string keyword);
|
||||
Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string keyword);
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ namespace NadekoBot.Services.Database.Repositories
|
||||
|
||||
void Remove(int id);
|
||||
void Remove(T obj);
|
||||
void RemoveRange(params T[] ids);
|
||||
void RemoveRange(params int[] ids);
|
||||
|
||||
void Update(T obj);
|
||||
void UpdateRange(params T[] objs);
|
||||
|
@ -15,10 +15,10 @@ namespace NadekoBot.Services.Database.Repositories.Impl
|
||||
{
|
||||
}
|
||||
|
||||
public IEnumerable<Quote> GetQuotesByText(string text) =>
|
||||
_set.Where(q => q.Text == text);
|
||||
public IEnumerable<Quote> GetAllQuotesByKeyword(string keyword) =>
|
||||
_set.Where(q => q.Keyword == keyword);
|
||||
|
||||
public Task<Quote> GetRandomQuoteByTextAsync(string text)
|
||||
public Task<Quote> GetRandomQuoteByKeywordAsync(ulong guildId, string text)
|
||||
{
|
||||
var rng = new Random();
|
||||
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();
|
||||
|
||||
private bool disposed = false;
|
||||
|
@ -38,5 +38,8 @@ namespace NadekoBot.Services
|
||||
|
||||
public UnitOfWork GetUnitOfWork() =>
|
||||
new UnitOfWork(GetDbContext());
|
||||
|
||||
public static UnitOfWork UnitOfWork() =>
|
||||
DbHandler.Instance.GetUnitOfWork();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user