feat: item to collection
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.CopyMediaToCollection;
|
||||
|
||||
/// <summary>
|
||||
/// Копирует медиа-элемент в другую коллекцию.
|
||||
/// Оригинал остаётся на месте; создаётся новый элемент с теми же данными.
|
||||
/// Возвращает Id нового элемента или null, если исходный не найден / нет доступа.
|
||||
/// </summary>
|
||||
public sealed record CopyMediaToCollectionCommand(
|
||||
Guid SourceMediaItemId,
|
||||
Guid TargetCollectionId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<Guid?>;
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.CopyMediaToCollection;
|
||||
|
||||
public sealed class CopyMediaToCollectionCommandHandler(
|
||||
IMediaItemRepository mediaItems,
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<CopyMediaToCollectionCommand, Guid?>
|
||||
{
|
||||
public async Task<Guid?> Handle(CopyMediaToCollectionCommand request, CancellationToken ct)
|
||||
{
|
||||
var source = await mediaItems.GetByIdAsync(request.SourceMediaItemId, ct);
|
||||
if (source is null || source.OwnerTelegramId != request.RequestingTelegramId)
|
||||
return null;
|
||||
|
||||
var target = await collections.GetByIdAsync(request.TargetCollectionId, ct);
|
||||
if (target is null || target.OwnerTelegramId != request.RequestingTelegramId)
|
||||
throw new DomainException("Target collection not found.");
|
||||
|
||||
var copy = MediaItem.Create(
|
||||
source.OwnerTelegramId,
|
||||
target.Id,
|
||||
source.Source,
|
||||
source.Type,
|
||||
source.Caption,
|
||||
source.MediaGroupId,
|
||||
source.Tags);
|
||||
|
||||
await mediaItems.AddAsync(copy, ct);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
|
||||
return copy.Id;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.DeleteCollection;
|
||||
@@ -15,6 +16,9 @@ public sealed class DeleteCollectionCommandHandler(
|
||||
if (collection is null || collection.OwnerTelegramId != request.RequestingTelegramId)
|
||||
return false;
|
||||
|
||||
if (collection.IsInbox)
|
||||
throw new DomainException("Cannot delete the Inbox collection.");
|
||||
|
||||
await collections.RemoveAsync(collection, ct);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.BotUser;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.EnsureUser;
|
||||
@@ -8,11 +9,16 @@ namespace MemeVault.Application.Commands.EnsureUser;
|
||||
public sealed class EnsureUserCommandHandler : IRequestHandler<EnsureUserCommand>
|
||||
{
|
||||
private readonly IBotUserRepository _users;
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public EnsureUserCommandHandler(IBotUserRepository users, IUnitOfWork uow)
|
||||
public EnsureUserCommandHandler(
|
||||
IBotUserRepository users,
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
_users = users;
|
||||
_collections = collections;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
@@ -24,6 +30,10 @@ public sealed class EnsureUserCommandHandler : IRequestHandler<EnsureUserCommand
|
||||
{
|
||||
user = BotUser.Create(request.TelegramId, request.Username, request.FirstName);
|
||||
await _users.AddAsync(user, cancellationToken);
|
||||
|
||||
// Каждый новый пользователь получает системную коллекцию Inbox
|
||||
var inbox = Collection.CreateInbox(request.TelegramId);
|
||||
await _collections.AddAsync(inbox, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
+4
-15
@@ -1,15 +1,4 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.ManageCollectionItem;
|
||||
|
||||
public sealed record AddItemToCollectionCommand(
|
||||
Guid CollectionId,
|
||||
Guid MediaItemId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<bool>;
|
||||
|
||||
public sealed record RemoveItemFromCollectionCommand(
|
||||
Guid CollectionId,
|
||||
Guid MediaItemId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<bool>;
|
||||
// This file is intentionally empty.
|
||||
// Item-to-collection membership is now managed via UpdateMediaCommand (move)
|
||||
// and CopyMediaToCollectionCommand (copy).
|
||||
// See MediaController PATCH /{id} and POST /{id}/copy.
|
||||
|
||||
+3
-37
@@ -1,37 +1,3 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.ManageCollectionItem;
|
||||
|
||||
public sealed class AddItemToCollectionCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<AddItemToCollectionCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(AddItemToCollectionCommand request, CancellationToken ct)
|
||||
{
|
||||
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||
if (collection is null) return false;
|
||||
|
||||
collection.AddItem(request.MediaItemId, request.RequestingTelegramId);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class RemoveItemFromCollectionCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<RemoveItemFromCollectionCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(RemoveItemFromCollectionCommand request, CancellationToken ct)
|
||||
{
|
||||
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||
if (collection is null) return false;
|
||||
|
||||
collection.RemoveItem(request.MediaItemId, request.RequestingTelegramId);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// This file is intentionally empty.
|
||||
// Item-to-collection membership is now managed via UpdateMediaCommand (move)
|
||||
// and CopyMediaToCollectionCommand (copy).
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -5,10 +5,12 @@ namespace MemeVault.Application.Commands.UpdateMedia;
|
||||
/// <summary>
|
||||
/// Обновляет caption и/или теги медиа-элемента.
|
||||
/// Tags — полная замена: пустой список очищает все теги.
|
||||
/// CollectionId — если указан, перемещает элемент в указанную коллекцию.
|
||||
/// </summary>
|
||||
public sealed record UpdateMediaCommand(
|
||||
Guid MediaItemId,
|
||||
long RequestingTelegramId,
|
||||
string? Caption,
|
||||
IReadOnlyList<string> Tags
|
||||
IReadOnlyList<string> Tags,
|
||||
Guid? CollectionId = null
|
||||
) : IRequest<bool>;
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using MemeVault.Application.Common;
|
||||
|
||||
namespace MemeVault.Application.Commands.UpdateMedia;
|
||||
|
||||
public sealed class UpdateMediaCommandHandler(
|
||||
IMediaItemRepository mediaItems,
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<UpdateMediaCommand, bool>
|
||||
{
|
||||
@@ -26,6 +28,16 @@ public sealed class UpdateMediaCommandHandler(
|
||||
foreach (var raw in request.Tags)
|
||||
item.AddTag(new Tag(raw));
|
||||
|
||||
// Перемещение в другую коллекцию
|
||||
if (request.CollectionId.HasValue && request.CollectionId.Value != item.CollectionId)
|
||||
{
|
||||
var target = await collections.GetByIdAsync(request.CollectionId.Value, ct);
|
||||
if (target is null || target.OwnerTelegramId != request.RequestingTelegramId)
|
||||
throw new DomainException("Target collection not found.");
|
||||
|
||||
item.MoveToCollection(target.Id);
|
||||
}
|
||||
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user