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

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