using MemeVault.Domain.Aggregates.Collection; using MemeVault.Domain.Enums; using MemeVault.Domain.Repositories; using Microsoft.EntityFrameworkCore; namespace MemeVault.Infrastructure.Persistence.Repositories; public sealed class CollectionRepository : ICollectionRepository { private readonly AppDbContext _db; public CollectionRepository(AppDbContext db) => _db = db; public Task GetByIdAsync(Guid id, CancellationToken ct) => _db.Collections .Include(c => c.Members) .FirstOrDefaultAsync(x => x.Id == id, ct); public Task GetByShareTokenAsync(string shareToken, CancellationToken ct) => _db.Collections .Include(c => c.Members) .FirstOrDefaultAsync(x => x.ShareToken == shareToken, ct); public async Task> GetByOwnerAsync(long ownerTelegramId, CancellationToken ct) { return await _db.Collections .Include(c => c.Members) .Where(c => c.Members.Any(m => m.TelegramId == ownerTelegramId && m.Role == CollectionRole.Owner)) .OrderBy(x => x.IsInbox ? 0 : 1) // Inbox всегда первым .ThenBy(x => x.Name) .ToListAsync(ct); } public async Task> GetByMemberAsync(long telegramId, CancellationToken ct) { return await _db.Collections .Include(c => c.Members) .Where(c => c.Members.Any(m => m.TelegramId == telegramId)) .OrderBy(x => x.IsInbox ? 0 : 1) // Inbox всегда первым .ThenBy(x => x.Name) .ToListAsync(ct); } public Task GetInboxAsync(long ownerTelegramId, CancellationToken ct) => _db.Collections .Include(c => c.Members) .FirstOrDefaultAsync(c => c.IsInbox && c.Members.Any(m => m.TelegramId == ownerTelegramId && m.Role == CollectionRole.Owner), ct); public async Task AddAsync(Collection collection, CancellationToken ct) => await _db.Collections.AddAsync(collection, ct); public Task UpdateAsync(Collection collection, CancellationToken ct) { _db.Collections.Update(collection); return Task.CompletedTask; } public Task RemoveAsync(Collection collection, CancellationToken ct) { _db.Collections.Remove(collection); return Task.CompletedTask; } }