mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 01:38:27 -04:00
dev: Most cleanup logic moved to the service, improved some commands, possible bugs
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user