mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	dev: Started cleanup of quote commands. Moving logic to the service
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
using LinqToDB.EntityFrameworkCore;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using NadekoBot.Db.Models;
 | 
			
		||||
 | 
			
		||||
@@ -9,21 +10,6 @@ public static class QuoteExtensions
 | 
			
		||||
    public static IEnumerable<Quote> GetForGuild(this DbSet<Quote> quotes, ulong guildId)
 | 
			
		||||
        => quotes.AsQueryable().Where(x => x.GuildId == guildId);
 | 
			
		||||
 | 
			
		||||
    public static IReadOnlyCollection<Quote> GetGroup(
 | 
			
		||||
        this DbSet<Quote> quotes,
 | 
			
		||||
        ulong guildId,
 | 
			
		||||
        int page,
 | 
			
		||||
        OrderType order)
 | 
			
		||||
    {
 | 
			
		||||
        var q = quotes.AsQueryable().Where(x => x.GuildId == guildId);
 | 
			
		||||
        if (order == OrderType.Keyword)
 | 
			
		||||
            q = q.OrderBy(x => x.Keyword);
 | 
			
		||||
        else
 | 
			
		||||
            q = q.OrderBy(x => x.Id);
 | 
			
		||||
 | 
			
		||||
        return q.Skip(15 * page).Take(15).ToArray();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static async Task<Quote> GetRandomQuoteByKeywordAsync(
 | 
			
		||||
        this DbSet<Quote> quotes,
 | 
			
		||||
        ulong guildId,
 | 
			
		||||
@@ -45,7 +31,7 @@ public static class QuoteExtensions
 | 
			
		||||
                                        && (EF.Functions.Like(q.Text.ToUpper(), $"%{text.ToUpper()}%")
 | 
			
		||||
                                            || EF.Functions.Like(q.AuthorName, text)))
 | 
			
		||||
                            .ToArrayAsync())
 | 
			
		||||
               .RandomOrDefault();
 | 
			
		||||
            .RandomOrDefault();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void RemoveAllByKeyword(this DbSet<Quote> quotes, ulong guildId, string keyword)
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ namespace NadekoBot.Modules.Utility;
 | 
			
		||||
public partial class Utility
 | 
			
		||||
{
 | 
			
		||||
    [Group]
 | 
			
		||||
    public partial class QuoteCommands : NadekoModule
 | 
			
		||||
    public partial class QuoteCommands : NadekoModule<QuoteService>
 | 
			
		||||
    {
 | 
			
		||||
        private const string PREPEND_EXPORT =
 | 
			
		||||
            """
 | 
			
		||||
@@ -60,23 +60,20 @@ public partial class Utility
 | 
			
		||||
            if (page < 0)
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            IEnumerable<Quote> quotes;
 | 
			
		||||
            await using (var uow = _db.GetDbContext())
 | 
			
		||||
            var quotes = await _service.GetAllQuotesAsync(ctx.Guild.Id, page, order);
 | 
			
		||||
 | 
			
		||||
            if (quotes.Count == 0)
 | 
			
		||||
            {
 | 
			
		||||
                quotes = uow.Set<Quote>().GetGroup(ctx.Guild.Id, page, order);
 | 
			
		||||
                await Response().Error(strs.quotes_page_none).SendAsync();
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (quotes.Any())
 | 
			
		||||
            {
 | 
			
		||||
                await Response()
 | 
			
		||||
                      .Confirm(GetText(strs.quotes_page(page + 1)),
 | 
			
		||||
                          string.Join("\n",
 | 
			
		||||
                              quotes.Select(q
 | 
			
		||||
                                  => $"`{new kwum(q.Id)}` {Format.Bold(q.Keyword.SanitizeAllMentions()),-20} by {q.AuthorName.SanitizeAllMentions()}")))
 | 
			
		||||
                      .SendAsync();
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
                await Response().Error(strs.quotes_page_none).SendAsync();
 | 
			
		||||
            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();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Cmd]
 | 
			
		||||
@@ -156,8 +153,8 @@ public partial class Utility
 | 
			
		||||
                async (sm) =>
 | 
			
		||||
                {
 | 
			
		||||
                    var msg = sm.Data.Components.FirstOrDefault()?.Value;
 | 
			
		||||
                    
 | 
			
		||||
                    if(!string.IsNullOrWhiteSpace(msg)) 
 | 
			
		||||
 | 
			
		||||
                    if (!string.IsNullOrWhiteSpace(msg))
 | 
			
		||||
                        await QuoteEdit(id, msg);
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
@@ -306,7 +303,7 @@ public partial class Utility
 | 
			
		||||
                                      .UpdateWithOutputAsync((del, ins) => ins);
 | 
			
		||||
 | 
			
		||||
                q = result.FirstOrDefault();
 | 
			
		||||
                
 | 
			
		||||
 | 
			
		||||
                await uow.SaveChangesAsync();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ public sealed class QuoteService : IQuoteService, INService
 | 
			
		||||
    {
 | 
			
		||||
        _db = db;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Delete all quotes created by the author in a guild
 | 
			
		||||
    /// </summary>
 | 
			
		||||
@@ -24,9 +24,39 @@ public sealed class QuoteService : IQuoteService, INService
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        var deleted = await ctx.GetTable<Quote>()
 | 
			
		||||
            .Where(x => x.GuildId == guildId && x.AuthorId == userId)
 | 
			
		||||
            .DeleteAsync();
 | 
			
		||||
                               .Where(x => x.GuildId == guildId && x.AuthorId == userId)
 | 
			
		||||
                               .DeleteAsync();
 | 
			
		||||
 | 
			
		||||
        return deleted;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Delete all quotes in a guild
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    /// <param name="guildId">ID of the guild</param>
 | 
			
		||||
    /// <returns>Number of deleted qutoes</returns>
 | 
			
		||||
    public async Task<int> DeleteAllQuotesAsync(ulong guildId)
 | 
			
		||||
    {
 | 
			
		||||
        await using var ctx = _db.GetDbContext();
 | 
			
		||||
        var deleted = await ctx.GetTable<Quote>()
 | 
			
		||||
                               .Where(x => x.GuildId == guildId)
 | 
			
		||||
                               .DeleteAsync();
 | 
			
		||||
 | 
			
		||||
        return deleted;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task<IReadOnlyCollection<Quote>> GetAllQuotesAsync(ulong guildId, int page, OrderType order)
 | 
			
		||||
    {
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        var q = uow.Set<Quote>()
 | 
			
		||||
                   .ToLinqToDBTable()
 | 
			
		||||
                   .Where(x => x.GuildId == guildId);
 | 
			
		||||
 | 
			
		||||
        if (order == OrderType.Keyword)
 | 
			
		||||
            q = q.OrderBy(x => x.Keyword);
 | 
			
		||||
        else
 | 
			
		||||
            q = q.OrderBy(x => x.Id);
 | 
			
		||||
 | 
			
		||||
        return await q.Skip(15 * page).Take(15).ToArrayAsyncLinqToDB();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user