47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
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)
|
||
|
|
.OrderBy(x => x.Name)
|
||
|
|
.ToListAsync(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;
|
||
|
|
}
|
||
|
|
}
|