/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
docs/final-readme
ConsoleBot/Infrastructure/DataAccess/SqlToDoRepository.cs
148 строк
5 KB
d.alekseev
feat: нотификации и фоновые задачи доставки уведомлений
26 фев 2026, 10:55
26 фев 2026, 10:55
4338916
Код
Авторство
О чём код?
using LinqToDB; using LinqToDB.Async; using SmartMenuBot.Core.DataAccess; using SmartMenuBot.Core.Entities; namespace SmartMenuBot.Infrastructure.DataAccess { public class SqlToDoRepository(IDataContextFactory<ToDoDataContext> factory) : IToDoRepository { public async Task AddAsync(ToDoItem item, CancellationToken ct) { ArgumentNullException.ThrowIfNull(item); using var dbContext = factory.CreateDataContext(); var model = ModelMapper.MapToModel(item); await dbContext.InsertAsync(model, token: ct); } public async Task<int> CountActiveAsync(Guid userId, CancellationToken ct) { using var dbContext = factory.CreateDataContext(); return await dbContext.ToDoItems .CountAsync(i => i.UserId == userId && i.State == (int)ToDoItemState.Active, ct); } public async Task DeleteAsync(Guid id, CancellationToken ct) { using var dbContext = factory.CreateDataContext(); await dbContext.ToDoItems .Where(i => i.Id == id) .DeleteAsync(ct); } public async Task<bool> ExistsByNameAsync(Guid userId, string name, CancellationToken ct) { var normalizedName = name.ToLowerInvariant(); using var dbContext = factory.CreateDataContext(); return await dbContext.ToDoItems .AnyAsync(i => i.UserId == userId && i.Name.ToLower() == normalizedName, ct); } public async Task<ToDoItem?> GetAsync(Guid id, CancellationToken ct) { using var dbContext = factory.CreateDataContext(); var model = await dbContext.ToDoItems .LoadWith(i => i.User) .LoadWith(i => i.List) .LoadWith(i => i.List!.User) .FirstOrDefaultAsync(i => i.Id == id, ct); return model is null ? null : ModelMapper.MapFromModel(model); } public async Task<IReadOnlyList<ToDoItem>> GetActiveByUserIdAsync(Guid userId, CancellationToken ct) { using var dbContext = factory.CreateDataContext(); var models = await dbContext.ToDoItems .LoadWith(i => i.User) .LoadWith(i => i.List) .LoadWith(i => i.List!.User) .Where(i => i.UserId == userId && i.State == (int)ToDoItemState.Active) .OrderBy(i => i.CreatedAt) .ToListAsync(ct); return models .Select(ModelMapper.MapFromModel) .ToList() .AsReadOnly(); } public async Task<IReadOnlyList<ToDoItem>> GetAllByUserIdAsync(Guid userId, CancellationToken ct) { using var dbContext = factory.CreateDataContext(); var models = await dbContext.ToDoItems .LoadWith(i => i.User) .LoadWith(i => i.List) .LoadWith(i => i.List!.User) .Where(i => i.UserId == userId) .OrderBy(i => i.CreatedAt) .ToListAsync(ct); return models .Select(ModelMapper.MapFromModel) .ToList() .AsReadOnly(); } public async Task<IReadOnlyList<ToDoItem>> GetActiveWithDeadline(Guid userId, DateTime from, DateTime to, CancellationToken ct) { using var dbContext = factory.CreateDataContext(); var models = await dbContext.ToDoItems .LoadWith(i => i.User) .LoadWith(i => i.List) .LoadWith(i => i.List!.User) .Where(i => i.UserId == userId && i.State == (int)ToDoItemState.Active && i.Deadline >= from && i.Deadline < to) .OrderBy(i => i.Deadline) .ToListAsync(ct); return models .Select(ModelMapper.MapFromModel) .ToList() .AsReadOnly(); } public async Task<IReadOnlyList<ToDoItem>> FindAsync(Guid userId, Func<ToDoItem, bool> predicate, CancellationToken ct) { ArgumentNullException.ThrowIfNull(predicate); using var dbContext = factory.CreateDataContext(); var models = await dbContext.ToDoItems .LoadWith(i => i.User) .LoadWith(i => i.List) .LoadWith(i => i.List!.User) .Where(i => i.UserId == userId) .OrderBy(i => i.CreatedAt) .ToListAsync(ct); return models .Select(ModelMapper.MapFromModel) .Where(predicate) .ToList() .AsReadOnly(); } public async Task UpdateAsync(ToDoItem item, CancellationToken ct) { ArgumentNullException.ThrowIfNull(item); using var dbContext = factory.CreateDataContext(); var model = ModelMapper.MapToModel(item); await dbContext.UpdateAsync(model, token: ct); } } }