Most cleanup logic moved to the service, improved some commands, possible bugs

This commit is contained in:
Toastie 2024-08-20 14:24:14 +12:00
parent afd5be89d1
commit b78f9dfd8c
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
3 changed files with 90 additions and 79 deletions

View file

@ -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));
}

View file

@ -85,16 +85,7 @@ public partial class Utility
keyword = keyword.ToUpperInvariant();
Quote quote;
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();
//}
}
var quote = await _service.GetQuoteByKeywordAsync(ctx.Guild.Id, keyword);
if (quote is null)
return;
@ -110,6 +101,7 @@ public partial class Utility
.SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteShow(kwum quoteId)
@ -154,7 +146,7 @@ public partial class Utility
{
var msg = sm.Data.Components.FirstOrDefault()?.Value;
if (!string.IsNullOrWhiteSpace(msg))
if (!string.IsNullOrWhiteSpace(msg))
await QuoteEdit(id, msg);
}
);
@ -186,27 +178,30 @@ public partial class Utility
.SendAsync();
}
private async Task QuoteSearchinternalAsync(string? keyword, string textOrAuthor)
private async Task QuoteSearchInternalAsync(string? keyword, string textOrAuthor)
{
if (string.IsNullOrWhiteSpace(textOrAuthor))
return;
keyword = keyword?.ToUpperInvariant();
Quote quote;
await using (var uow = _db.GetDbContext())
{
quote = await uow.Set<Quote>().SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor);
}
if (quote is null)
return;
var quotes = await _service.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor);
await Response()
.Confirm($"`{new kwum(quote.Id)}` 💬 ",
quote.Keyword.ToLowerInvariant()
+ ": "
+ quote.Text.SanitizeAllMentions())
.Paginated()
.Items(quotes)
.PageSize(1)
.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();
}
@ -214,13 +209,13 @@ public partial class Utility
[RequireContext(ContextType.Guild)]
[Priority(0)]
public Task QuoteSearch(string textOrAuthor)
=> QuoteSearchinternalAsync(null, textOrAuthor);
=> QuoteSearchInternalAsync(null, textOrAuthor);
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(1)]
public Task QuoteSearch(string keyword, [Leftover] string textOrAuthor)
=> QuoteSearchinternalAsync(keyword, textOrAuthor);
=> QuoteSearchInternalAsync(keyword, textOrAuthor);
[Cmd]
[RequireContext(ContextType.Guild)]
@ -229,14 +224,7 @@ public partial class Utility
if (quoteId < 0)
return;
Quote quote;
var repCtx = new ReplacementContext(Context);
await using (var uow = _db.GetDbContext())
{
quote = uow.Set<Quote>().GetById(quoteId);
}
var quote = await _service.GetQuoteByIdAsync(quoteId);
if (quote is null || quote.GuildId != ctx.Guild.Id)
{
@ -249,6 +237,7 @@ public partial class Utility
+ ":\n";
var repCtx = new ReplacementContext(Context);
var text = SmartText.CreateFrom(quote.Text);
text = await repSvc.ReplaceAsync(text, repCtx);
await Response()
@ -257,6 +246,7 @@ public partial class Utility
.SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteAdd(string keyword, [Leftover] string text)
@ -388,7 +378,7 @@ public partial class Utility
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();
}
@ -401,11 +391,7 @@ public partial class Utility
[UserPerm(GuildPerm.Administrator)]
public async Task QuotesExport()
{
IEnumerable<Quote> quotes;
await using (var uow = _db.GetDbContext())
{
quotes = uow.Set<Quote>().GetForGuild(ctx.Guild.Id).ToList();
}
var quotes = _service.GetForGuild(ctx.Guild.Id).ToList();
var exprsDict = quotes.GroupBy(x => x.Keyword)
.ToDictionary(x => x.Key, x => x.Select(ExportedQuote.FromModel));

View file

@ -59,4 +59,68 @@ public sealed class QuoteService : IQuoteService, IEService
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);
}
}