Most cleanup logic moved to the service, improved some commands, possible bugs
This commit is contained in:
parent
afd5be89d1
commit
b78f9dfd8c
3 changed files with 90 additions and 79 deletions
|
@ -1,39 +0,0 @@
|
||||||
#nullable disable
|
|
||||||
using LinqToDB.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using EllieBot.Db.Models;
|
|
||||||
|
|
||||||
namespace EllieBot.Db;
|
|
||||||
|
|
||||||
public static class QuoteExtensions
|
|
||||||
{
|
|
||||||
public static IEnumerable<Quote> GetForGuild(this DbSet<Quote> quotes, ulong guildId)
|
|
||||||
=> quotes.AsQueryable().Where(x => x.GuildId == guildId);
|
|
||||||
|
|
||||||
public static async Task<Quote> GetRandomQuoteByKeywordAsync(
|
|
||||||
this DbSet<Quote> quotes,
|
|
||||||
ulong guildId,
|
|
||||||
string keyword)
|
|
||||||
{
|
|
||||||
return (await quotes.AsQueryable().Where(q => q.GuildId == guildId && q.Keyword == keyword).ToArrayAsync())
|
|
||||||
.RandomOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<Quote> SearchQuoteKeywordTextAsync(
|
|
||||||
this DbSet<Quote> quotes,
|
|
||||||
ulong guildId,
|
|
||||||
string keyword,
|
|
||||||
string text)
|
|
||||||
{
|
|
||||||
return (await quotes.AsQueryable()
|
|
||||||
.Where(q => q.GuildId == guildId
|
|
||||||
&& (keyword == null || q.Keyword == keyword)
|
|
||||||
&& (EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%")
|
|
||||||
|| EF.Functions.Like(q.AuthorName, text)))
|
|
||||||
.ToArrayAsync())
|
|
||||||
.RandomOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void RemoveAllByKeyword(this DbSet<Quote> quotes, ulong guildId, string keyword)
|
|
||||||
=> quotes.RemoveRange(quotes.AsQueryable().Where(x => x.GuildId == guildId && x.Keyword.ToUpper() == keyword));
|
|
||||||
}
|
|
|
@ -85,16 +85,7 @@ public partial class Utility
|
||||||
|
|
||||||
keyword = keyword.ToUpperInvariant();
|
keyword = keyword.ToUpperInvariant();
|
||||||
|
|
||||||
Quote quote;
|
var quote = await _service.GetQuoteByKeywordAsync(ctx.Guild.Id, keyword);
|
||||||
await using (var uow = _db.GetDbContext())
|
|
||||||
{
|
|
||||||
quote = await uow.Set<Quote>().GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword);
|
|
||||||
//if (quote is not null)
|
|
||||||
//{
|
|
||||||
// quote.UseCount += 1;
|
|
||||||
// uow.Complete();
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (quote is null)
|
if (quote is null)
|
||||||
return;
|
return;
|
||||||
|
@ -110,6 +101,7 @@ public partial class Utility
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
public async Task QuoteShow(kwum quoteId)
|
public async Task QuoteShow(kwum quoteId)
|
||||||
|
@ -154,7 +146,7 @@ public partial class Utility
|
||||||
{
|
{
|
||||||
var msg = sm.Data.Components.FirstOrDefault()?.Value;
|
var msg = sm.Data.Components.FirstOrDefault()?.Value;
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(msg))
|
if (!string.IsNullOrWhiteSpace(msg))
|
||||||
await QuoteEdit(id, msg);
|
await QuoteEdit(id, msg);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -186,27 +178,30 @@ public partial class Utility
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task QuoteSearchinternalAsync(string? keyword, string textOrAuthor)
|
private async Task QuoteSearchInternalAsync(string? keyword, string textOrAuthor)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(textOrAuthor))
|
if (string.IsNullOrWhiteSpace(textOrAuthor))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
keyword = keyword?.ToUpperInvariant();
|
keyword = keyword?.ToUpperInvariant();
|
||||||
|
|
||||||
Quote quote;
|
var quotes = await _service.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor);
|
||||||
await using (var uow = _db.GetDbContext())
|
|
||||||
{
|
|
||||||
quote = await uow.Set<Quote>().SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (quote is null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Confirm($"`{new kwum(quote.Id)}` 💬 ",
|
.Paginated()
|
||||||
quote.Keyword.ToLowerInvariant()
|
.Items(quotes)
|
||||||
+ ": "
|
.PageSize(1)
|
||||||
+ quote.Text.SanitizeAllMentions())
|
.Page((pageQuotes, _) =>
|
||||||
|
{
|
||||||
|
var quote = pageQuotes[0];
|
||||||
|
|
||||||
|
var text = quote.Keyword.ToLowerInvariant() + ": " + quote.Text;
|
||||||
|
|
||||||
|
return _sender.CreateEmbed()
|
||||||
|
.WithOkColor()
|
||||||
|
.WithTitle($"{new kwum(quote.Id)} 💬 ")
|
||||||
|
.WithDescription(text);
|
||||||
|
})
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,13 +209,13 @@ public partial class Utility
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[Priority(0)]
|
[Priority(0)]
|
||||||
public Task QuoteSearch(string textOrAuthor)
|
public Task QuoteSearch(string textOrAuthor)
|
||||||
=> QuoteSearchinternalAsync(null, textOrAuthor);
|
=> QuoteSearchInternalAsync(null, textOrAuthor);
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[Priority(1)]
|
[Priority(1)]
|
||||||
public Task QuoteSearch(string keyword, [Leftover] string textOrAuthor)
|
public Task QuoteSearch(string keyword, [Leftover] string textOrAuthor)
|
||||||
=> QuoteSearchinternalAsync(keyword, textOrAuthor);
|
=> QuoteSearchInternalAsync(keyword, textOrAuthor);
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
|
@ -229,14 +224,7 @@ public partial class Utility
|
||||||
if (quoteId < 0)
|
if (quoteId < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Quote quote;
|
var quote = await _service.GetQuoteByIdAsync(quoteId);
|
||||||
|
|
||||||
var repCtx = new ReplacementContext(Context);
|
|
||||||
|
|
||||||
await using (var uow = _db.GetDbContext())
|
|
||||||
{
|
|
||||||
quote = uow.Set<Quote>().GetById(quoteId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (quote is null || quote.GuildId != ctx.Guild.Id)
|
if (quote is null || quote.GuildId != ctx.Guild.Id)
|
||||||
{
|
{
|
||||||
|
@ -249,6 +237,7 @@ public partial class Utility
|
||||||
+ ":\n";
|
+ ":\n";
|
||||||
|
|
||||||
|
|
||||||
|
var repCtx = new ReplacementContext(Context);
|
||||||
var text = SmartText.CreateFrom(quote.Text);
|
var text = SmartText.CreateFrom(quote.Text);
|
||||||
text = await repSvc.ReplaceAsync(text, repCtx);
|
text = await repSvc.ReplaceAsync(text, repCtx);
|
||||||
await Response()
|
await Response()
|
||||||
|
@ -257,6 +246,7 @@ public partial class Utility
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
public async Task QuoteAdd(string keyword, [Leftover] string text)
|
public async Task QuoteAdd(string keyword, [Leftover] string text)
|
||||||
|
@ -388,7 +378,7 @@ public partial class Utility
|
||||||
|
|
||||||
await using (var uow = _db.GetDbContext())
|
await using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
uow.Set<Quote>().RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant());
|
await _service.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant());
|
||||||
|
|
||||||
await uow.SaveChangesAsync();
|
await uow.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
@ -401,11 +391,7 @@ public partial class Utility
|
||||||
[UserPerm(GuildPerm.Administrator)]
|
[UserPerm(GuildPerm.Administrator)]
|
||||||
public async Task QuotesExport()
|
public async Task QuotesExport()
|
||||||
{
|
{
|
||||||
IEnumerable<Quote> quotes;
|
var quotes = _service.GetForGuild(ctx.Guild.Id).ToList();
|
||||||
await using (var uow = _db.GetDbContext())
|
|
||||||
{
|
|
||||||
quotes = uow.Set<Quote>().GetForGuild(ctx.Guild.Id).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
var exprsDict = quotes.GroupBy(x => x.Keyword)
|
var exprsDict = quotes.GroupBy(x => x.Keyword)
|
||||||
.ToDictionary(x => x.Key, x => x.Select(ExportedQuote.FromModel));
|
.ToDictionary(x => x.Key, x => x.Select(ExportedQuote.FromModel));
|
||||||
|
|
|
@ -59,4 +59,68 @@ public sealed class QuoteService : IQuoteService, IEService
|
||||||
|
|
||||||
return await q.Skip(15 * page).Take(15).ToArrayAsync();
|
return await q.Skip(15 * page).Take(15).ToArrayAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Quote?> GetQuoteByKeywordAsync(ulong guildId, string keyword)
|
||||||
|
{
|
||||||
|
await using var uow = _db.GetDbContext();
|
||||||
|
var quotes = await uow.GetTable<Quote>()
|
||||||
|
.Where(q => q.GuildId == guildId && q.Keyword == keyword)
|
||||||
|
.ToArrayAsyncLinqToDB();
|
||||||
|
|
||||||
|
return quotes.RandomOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IReadOnlyCollection<Quote>> SearchQuoteKeywordTextAsync(
|
||||||
|
ulong guildId,
|
||||||
|
string? keyword,
|
||||||
|
string text)
|
||||||
|
{
|
||||||
|
keyword = keyword?.ToUpperInvariant();
|
||||||
|
await using var uow = _db.GetDbContext();
|
||||||
|
|
||||||
|
var quotes = await uow.GetTable<Quote>()
|
||||||
|
.Where(q => q.GuildId == guildId
|
||||||
|
&& (keyword == null || q.Keyword == keyword))
|
||||||
|
.ToArrayAsync();
|
||||||
|
|
||||||
|
var toReturn = new List<Quote>(quotes.Length);
|
||||||
|
|
||||||
|
foreach (var q in quotes)
|
||||||
|
{
|
||||||
|
if (q.AuthorName.Contains(text, StringComparison.InvariantCultureIgnoreCase)
|
||||||
|
|| q.Text.Contains(text, StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
toReturn.Add(q);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Quote> GetForGuild(ulong guildId)
|
||||||
|
{
|
||||||
|
using var uow = _db.GetDbContext();
|
||||||
|
var quotes = uow.GetTable<Quote>()
|
||||||
|
.Where(x => x.GuildId == guildId);
|
||||||
|
return quotes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<int> RemoveAllByKeyword(ulong guildId, string keyword)
|
||||||
|
{
|
||||||
|
keyword = keyword.ToUpperInvariant();
|
||||||
|
|
||||||
|
using var uow = _db.GetDbContext();
|
||||||
|
|
||||||
|
var count = uow.GetTable<Quote>()
|
||||||
|
.Where(x => x.GuildId == guildId && x.Keyword == keyword)
|
||||||
|
.DeleteAsync();
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Quote> GetQuoteByIdAsync(kwum quoteId)
|
||||||
|
{
|
||||||
|
await using var uow = _db.GetDbContext();
|
||||||
|
return uow.Set<Quote>().GetById(quoteId);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue