feat: item to collection

This commit is contained in:
2026-09-28 02:37:11 +04:00
parent 5238bfd103
commit 50fe29f4f0
29 changed files with 732 additions and 156 deletions
@@ -6,6 +6,7 @@ namespace MemeVault.Application.Commands.SaveMedia;
/// <summary>
/// Команда: сохранить медиа-элемент для пользователя.
/// Если CollectionId не указан, элемент попадает в Inbox.
/// Возвращает Id созданного элемента.
/// </summary>
public sealed record SaveMediaCommand(
@@ -14,5 +15,6 @@ public sealed record SaveMediaCommand(
MediaType MediaType,
string? Caption,
IReadOnlyList<string> Tags,
string? MediaGroupId = null
string? MediaGroupId = null,
Guid? CollectionId = null
) : IRequest<Guid>;
@@ -1,6 +1,7 @@
using MediatR;
using MemeVault.Application.Common;
using MemeVault.Domain.Aggregates.MediaItem;
using MemeVault.Domain.Exceptions;
using MemeVault.Domain.Repositories;
using MemeVault.Domain.ValueObjects;
@@ -9,11 +10,16 @@ namespace MemeVault.Application.Commands.SaveMedia;
public sealed class SaveMediaCommandHandler : IRequestHandler<SaveMediaCommand, Guid>
{
private readonly IMediaItemRepository _mediaItems;
private readonly ICollectionRepository _collections;
private readonly IUnitOfWork _uow;
public SaveMediaCommandHandler(IMediaItemRepository mediaItems, IUnitOfWork uow)
public SaveMediaCommandHandler(
IMediaItemRepository mediaItems,
ICollectionRepository collections,
IUnitOfWork uow)
{
_mediaItems = mediaItems;
_collections = collections;
_uow = uow;
}
@@ -21,8 +27,25 @@ public sealed class SaveMediaCommandHandler : IRequestHandler<SaveMediaCommand,
{
var tags = request.Tags.Select(t => new Tag(t));
// Определяем целевую коллекцию: явно указанная или Inbox
Guid collectionId;
if (request.CollectionId.HasValue)
{
var col = await _collections.GetByIdAsync(request.CollectionId.Value, cancellationToken);
if (col is null || col.OwnerTelegramId != request.OwnerTelegramId)
throw new DomainException("Target collection not found.");
collectionId = col.Id;
}
else
{
var inbox = await _collections.GetInboxAsync(request.OwnerTelegramId, cancellationToken)
?? throw new DomainException("User inbox not found.");
collectionId = inbox.Id;
}
var item = MediaItem.Create(
request.OwnerTelegramId,
collectionId,
request.Source,
request.MediaType,
request.Caption,