2026-09-28 01:47:01 +04:00
|
|
|
using MemeVault.Domain.Aggregates.Collection;
|
|
|
|
|
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<Collection?> GetByIdAsync(Guid id, CancellationToken ct) =>
|
|
|
|
|
_db.Collections
|
|
|
|
|
.Include(c => c.Members)
|
|
|
|
|
.FirstOrDefaultAsync(x => x.Id == id, ct);
|
|
|
|
|
|
|
|
|
|
public Task<Collection?> GetByShareTokenAsync(string shareToken, CancellationToken ct) =>
|
|
|
|
|
_db.Collections
|
|
|
|
|
.Include(c => c.Members)
|
|
|
|
|
.FirstOrDefaultAsync(x => x.ShareToken == shareToken, ct);
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<Collection>> GetByOwnerAsync(long ownerTelegramId, CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
return await _db.Collections
|
|
|
|
|
.Include(c => c.Members)
|
|
|
|
|
.Where(x => x.OwnerTelegramId == ownerTelegramId)
|
2026-09-28 02:37:11 +04:00
|
|
|
.OrderBy(x => x.IsInbox ? 0 : 1) // Inbox всегда первым
|
|
|
|
|
.ThenBy(x => x.Name)
|
2026-09-28 01:47:01 +04:00
|
|
|
.ToListAsync(ct);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-28 02:37:11 +04:00
|
|
|
public Task<Collection?> GetInboxAsync(long ownerTelegramId, CancellationToken ct) =>
|
|
|
|
|
_db.Collections
|
|
|
|
|
.FirstOrDefaultAsync(x => x.OwnerTelegramId == ownerTelegramId && x.IsInbox, ct);
|
|
|
|
|
|
2026-09-28 01:47:01 +04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|