1
Fork 0
mirror of https://gitlab.com/Kwoth/nadekobot.git synced 2024-10-02 12:09:07 +00:00

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

This commit is contained in:
Kwoth 2024-08-19 23:55:35 +00:00
parent 9aaf062d78
commit c31c2e8d8e
3 changed files with 90 additions and 79 deletions

View file

@ -1,39 +0,0 @@
#nullable disable
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db.Models;
namespace NadekoBot.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

@ -70,7 +70,7 @@ public partial class Utility
var list = quotes.Select(q => $"`{new kwum(q.Id)}` {Format.Bold(q.Keyword),-20} by {q.AuthorName}")
.Join("\n");
await Response()
.Confirm(GetText(strs.quotes_page(page + 1)), list)
.SendAsync();
@ -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)
@ -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, INService
return await q.Skip(15 * page).Take(15).ToArrayAsyncLinqToDB();
}
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);
}
}