/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/TelegramBot/Callbacks/Implementations/ShowCompletedCallbackCommand.cs
89 строк
3 KB
d.alekseev
feat: постраничная навигация задач и callback-управление карточкой задачи
15 фев 2026, 15:04
15 фев 2026, 15:04
69f4d23
Код
Авторство
О чём код?
using Telegram.Bot; using SmartMenuBot.Core.Entities; using SmartMenuBot.Core.Services.Interfaces; using SmartMenuBot.TelegramBot.Callbacks; using SmartMenuBot.TelegramBot.Callbacks.Interfaces; using SmartMenuBot.TelegramBot.Dto; using SmartMenuBot.TelegramBot.Helpers; namespace SmartMenuBot.TelegramBot.Callbacks.Implementations { public class ShowCompletedCallbackCommand( ITelegramBotClient botClient, IUserService userService, IToDoService toDoService) : ICallbackCommand { private const int PageSize = 5; public string Action => "show_completed"; public bool CanExecute(CallbackCommandContext context) { return string.Equals(context.Dto.Action, Action, StringComparison.OrdinalIgnoreCase); } public async Task ExecuteAsync(CallbackCommandContext context) { if (context.Query.Message is null) return; var ct = context.CancellationToken; var user = await userService.GetUserAsync(context.Query.From.Id, ct); if (user is null) { await botClient.SendMessage( context.Query.Message.Chat.Id, "Для использования этой функции необходимо авторизоваться командой /start", replyMarkup: ReplyKeyboards.PreRegistration, cancellationToken: ct); return; } var dto = PagedListCallbackDto.FromString(context.Query.Data ?? string.Empty); var taskList = await toDoService.GetByUserIdAndListAsync(user.UserId, dto.ToDoListId, ct); var ordered = taskList .Where(i => i.State == ToDoItemState.Completed) .OrderBy(i => i.CreatedAt) .ToList(); var taskButtons = ordered .Select(item => new KeyValuePair<string, string>( $"{item.Name} | срок: {item.Deadline:dd.MM.yyyy}", new ToDoItemCallbackDto { Action = "showtask", ToDoItemId = item.Id }.ToString())) .ToList(); var totalPages = taskButtons.Count == 0 ? 0 : (int)Math.Ceiling(taskButtons.Count / (double)PageSize); var normalizedPage = totalPages == 0 ? 0 : Math.Clamp(dto.Page, 0, totalPages - 1); var keyboard = InlinePaginationKeyboardBuilder.Build( taskButtons, new PagedListCallbackDto { Action = dto.Action, ToDoListId = dto.ToDoListId, Page = normalizedPage }, PageSize); var text = taskButtons.Count == 0 ? "Задач нет" : $"Выполненные задачи (стр. {normalizedPage + 1}/{totalPages}):"; await botClient.EditMessageText( context.Query.Message.Chat.Id, context.Query.Message.MessageId, text, replyMarkup: keyboard, cancellationToken: ct); } } }