/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/Core/Services/Domain/ToDoService.cs
89 строк
4 KB
d.alekseev
feat: постраничная навигация задач и callback-управление карточкой задачи
15 фев 2026, 15:04
15 фев 2026, 15:04
69f4d23
Код
Авторство
О чём код?
using SmartMenuBot.Core.Entities; using SmartMenuBot.Core.Exceptions; using SmartMenuBot.Core.Services.Interfaces; using SmartMenuBot.Core.DataAccess; namespace SmartMenuBot.Core.Services.Domain { public class ToDoService(ITaskLimitsManager limitsManager, IToDoRepository toDoRepository) : IToDoService { public async Task<ToDoItem> AddAsync(ToDoUser user, string name, DateTime deadline, ToDoList? list, CancellationToken ct) { ArgumentNullException.ThrowIfNull(user); if (deadline == default) throw new ArgumentException("Дедлайн не может быть пустой датой", nameof(deadline)); var limits = await limitsManager.GetLimitsAsync(ct); if (!limits.IsInitialized) throw new InvalidOperationException("Лимиты не установлены. Используйте /setlimits <кол-во> <длина>."); if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Наименование задачи не может быть пустым", nameof(name)); if (await toDoRepository.CountActiveAsync(user.UserId, ct) >= limits.Count) throw new TaskCountLimitException(limits.Count); string trimmedName = name.Trim(); if (trimmedName.Length > limits.Length) throw new TaskLengthLimitException(trimmedName.Length, limits.Length); if (await toDoRepository.ExistsByNameAsync(user.UserId, trimmedName, ct)) throw new DuplicateTaskException(trimmedName); var newItem = new ToDoItem(user, trimmedName, deadline, list); await toDoRepository.AddAsync(newItem, ct); return newItem; } public async Task<IReadOnlyList<ToDoItem>> GetAllByUserIdAsync(Guid userId, CancellationToken ct) { return await toDoRepository.GetAllByUserIdAsync(userId, ct); } public async Task<IReadOnlyList<ToDoItem>> GetActiveByUserIdAsync(Guid userId, CancellationToken ct) { return await toDoRepository.GetActiveByUserIdAsync(userId, ct); } public async Task<IReadOnlyList<ToDoItem>> GetByUserIdAndListAsync(Guid userId, Guid? listId, CancellationToken ct) { var allItems = await toDoRepository.GetAllByUserIdAsync(userId, ct); if (listId is null) return allItems.Where(item => item.List is null).ToList().AsReadOnly(); return allItems .Where(item => item.List?.Id == listId.Value) .ToList() .AsReadOnly(); } public async Task<ToDoItem?> GetAsync(Guid toDoItemId, CancellationToken ct) { return await toDoRepository.GetAsync(toDoItemId, ct); } public async Task MarkCompletedAsync(Guid id, CancellationToken ct) { var item = await toDoRepository.GetAsync(id, ct); if (item is null) return; item.Complete(); await toDoRepository.UpdateAsync(item, ct); } public async Task DeleteAsync(Guid id, CancellationToken ct) { await toDoRepository.DeleteAsync(id, ct); } public async Task<IReadOnlyList<ToDoItem>> FindAsync(ToDoUser user, string namePrefix, CancellationToken ct) { return await toDoRepository.FindAsync( user.UserId, (ToDoItem item) => item.Name.StartsWith(namePrefix, StringComparison.OrdinalIgnoreCase), ct); } } }