feat: item to collection
This commit is contained in:
@@ -26,6 +26,10 @@ public sealed class CollectionConfiguration : IEntityTypeConfiguration<Collectio
|
||||
.HasColumnName("description")
|
||||
.HasMaxLength(500);
|
||||
|
||||
builder.Property(x => x.IsInbox)
|
||||
.HasColumnName("is_inbox")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Visibility)
|
||||
.HasColumnName("visibility")
|
||||
.HasConversion<string>()
|
||||
@@ -43,12 +47,11 @@ public sealed class CollectionConfiguration : IEntityTypeConfiguration<Collectio
|
||||
builder.HasIndex(x => x.OwnerTelegramId)
|
||||
.HasDatabaseName("ix_collections_owner");
|
||||
|
||||
// MediaItemIds как массив UUID
|
||||
builder.Property<Guid[]>("_mediaItemIdsArray")
|
||||
.HasColumnName("media_item_ids")
|
||||
.HasColumnType("uuid[]");
|
||||
|
||||
builder.Ignore(x => x.MediaItemIds);
|
||||
// Уникальность Inbox на пользователя
|
||||
builder.HasIndex(x => new { x.OwnerTelegramId, x.IsInbox })
|
||||
.HasFilter("is_inbox = true")
|
||||
.IsUnique()
|
||||
.HasDatabaseName("ix_collections_inbox_unique");
|
||||
|
||||
// Участники — owned entity
|
||||
builder.OwnsMany(x => x.Members, m =>
|
||||
|
||||
@@ -22,6 +22,10 @@ public sealed class MediaItemConfiguration : IEntityTypeConfiguration<MediaItem>
|
||||
.HasColumnName("owner_telegram_id")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.CollectionId)
|
||||
.HasColumnName("collection_id")
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Type)
|
||||
.HasColumnName("type")
|
||||
.HasConversion<string>()
|
||||
@@ -42,8 +46,6 @@ public sealed class MediaItemConfiguration : IEntityTypeConfiguration<MediaItem>
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
// ── MediaSource Value Object → two nullable columns ───────────────────
|
||||
// When Source is null, both columns are NULL in DB.
|
||||
// EF Core reads them back as null when the owned entity is optional.
|
||||
builder.OwnsOne(x => x.Source, src =>
|
||||
{
|
||||
src.Property(s => s.Origin)
|
||||
@@ -58,8 +60,6 @@ public sealed class MediaItemConfiguration : IEntityTypeConfiguration<MediaItem>
|
||||
});
|
||||
|
||||
// ── Tags: private List<Tag> _tags ↔ text[] in PostgreSQL ─────────────
|
||||
// EF Core accesses the backing field "_tags" directly via reflection.
|
||||
// ValueConverter converts between List<Tag> and string[].
|
||||
var tagsConverter = new ValueConverter<List<Tag>, string[]>(
|
||||
tags => tags.Select(t => t.Value).ToArray(),
|
||||
strs => strs.Select(s => new Tag(s)).ToList());
|
||||
@@ -77,13 +77,15 @@ public sealed class MediaItemConfiguration : IEntityTypeConfiguration<MediaItem>
|
||||
.HasConversion(tagsConverter, tagsComparer)
|
||||
.IsRequired();
|
||||
|
||||
// Ignore the public IReadOnlyList<Tag> Tags wrapper — persistence goes through _tags
|
||||
builder.Ignore(x => x.Tags);
|
||||
|
||||
// ── Indices ───────────────────────────────────────────────────────────
|
||||
builder.HasIndex(x => x.OwnerTelegramId)
|
||||
.HasDatabaseName("ix_media_items_owner");
|
||||
|
||||
builder.HasIndex(x => x.CollectionId)
|
||||
.HasDatabaseName("ix_media_items_collection");
|
||||
|
||||
// GIN index on the tags array for @> and && operators
|
||||
builder.HasIndex("_tags")
|
||||
.HasDatabaseName("ix_media_items_tags")
|
||||
|
||||
@@ -25,10 +25,15 @@ public sealed class CollectionRepository : ICollectionRepository
|
||||
return await _db.Collections
|
||||
.Include(c => c.Members)
|
||||
.Where(x => x.OwnerTelegramId == ownerTelegramId)
|
||||
.OrderBy(x => x.Name)
|
||||
.OrderBy(x => x.IsInbox ? 0 : 1) // Inbox всегда первым
|
||||
.ThenBy(x => x.Name)
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
public Task<Collection?> GetInboxAsync(long ownerTelegramId, CancellationToken ct) =>
|
||||
_db.Collections
|
||||
.FirstOrDefaultAsync(x => x.OwnerTelegramId == ownerTelegramId && x.IsInbox, ct);
|
||||
|
||||
public async Task AddAsync(Collection collection, CancellationToken ct) =>
|
||||
await _db.Collections.AddAsync(collection, ct);
|
||||
|
||||
|
||||
@@ -22,6 +22,14 @@ public sealed class MediaItemRepository : IMediaItemRepository
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MediaItem>> GetByCollectionAsync(Guid collectionId, CancellationToken ct)
|
||||
{
|
||||
return await _db.MediaItems
|
||||
.Where(x => x.CollectionId == collectionId)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<MediaItem>> GetByIdsAsync(IEnumerable<Guid> ids, CancellationToken ct)
|
||||
{
|
||||
var list = ids.ToList();
|
||||
@@ -36,10 +44,8 @@ public sealed class MediaItemRepository : IMediaItemRepository
|
||||
int limit,
|
||||
CancellationToken ct)
|
||||
{
|
||||
// Базовая фильтрация по владельцу
|
||||
var sql = _db.MediaItems.Where(x => x.OwnerTelegramId == ownerTelegramId);
|
||||
|
||||
// Если нет тегов и нет текста — вернуть последние
|
||||
if (query.IsEmpty)
|
||||
{
|
||||
return await sql
|
||||
@@ -48,17 +54,12 @@ public sealed class MediaItemRepository : IMediaItemRepository
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
// Тег-фильтрация через сырой SQL (PostgreSQL array operators)
|
||||
// EF Core не умеет @> и && для text[] из коробки без Npgsql Extensions
|
||||
// Используем FromSqlRaw для гибкости
|
||||
|
||||
var parameters = new List<object>();
|
||||
var conditions = new List<string> { "owner_telegram_id = {0}" };
|
||||
parameters.Add(ownerTelegramId);
|
||||
|
||||
if (query.RequiredTags.Count > 0)
|
||||
{
|
||||
// tags @> ARRAY['тег1','тег2'] — содержит все обязательные теги
|
||||
var idx = parameters.Count;
|
||||
parameters.Add(query.RequiredTags.ToArray());
|
||||
conditions.Add($"tags @> {{{idx}}}");
|
||||
@@ -66,7 +67,6 @@ public sealed class MediaItemRepository : IMediaItemRepository
|
||||
|
||||
if (query.ExcludedTags.Count > 0)
|
||||
{
|
||||
// NOT (tags && ARRAY['тег3']) — не пересекается с исключёнными
|
||||
var idx = parameters.Count;
|
||||
parameters.Add(query.ExcludedTags.ToArray());
|
||||
conditions.Add($"NOT (tags && {{{idx}}})");
|
||||
@@ -74,7 +74,6 @@ public sealed class MediaItemRepository : IMediaItemRepository
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(query.TextQuery))
|
||||
{
|
||||
// Простой ILIKE по caption; для production заменить на to_tsvector
|
||||
var idx = parameters.Count;
|
||||
parameters.Add($"%{query.TextQuery}%");
|
||||
conditions.Add($"caption ILIKE {{{idx}}}");
|
||||
|
||||
Reference in New Issue
Block a user