feat: много запланированного
- клонирование коллекции - добавление паблик-коллекций - регенерация паблик-ссылок
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.CloneCollection;
|
||||
|
||||
/// <param name="SourceCollectionId">ID коллекции-источника.</param>
|
||||
/// <param name="RequestingTelegramId">Telegram ID пользователя, создающего клон.</param>
|
||||
/// <returns>ID новой коллекции, null если источник не найден или нет доступа.</returns>
|
||||
public sealed record CloneCollectionCommand(
|
||||
Guid SourceCollectionId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<Guid?>;
|
||||
@@ -0,0 +1,54 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.CloneCollection;
|
||||
|
||||
public sealed class CloneCollectionCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IMediaItemRepository mediaItems,
|
||||
IBotUserRepository users,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<CloneCollectionCommand, Guid?>
|
||||
{
|
||||
private const int CollectionOwnerLimit = 5;
|
||||
|
||||
public async Task<Guid?> Handle(CloneCollectionCommand request, CancellationToken ct)
|
||||
{
|
||||
var source = await collections.GetByIdAsync(request.SourceCollectionId, ct);
|
||||
if (source is null || !source.CanView(request.RequestingTelegramId))
|
||||
return null;
|
||||
|
||||
// Проверяем квоту коллекций у клонирующего пользователя
|
||||
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
|
||||
var owned = await collections.GetByOwnerAsync(request.RequestingTelegramId, ct);
|
||||
if (owned.Count(c => c.Id != user?.InboxCollectionId) >= CollectionOwnerLimit)
|
||||
throw new CollectionQuotaExceededException(CollectionOwnerLimit);
|
||||
|
||||
// Создаём новую коллекцию с теми же именем и описанием
|
||||
var clone = Collection.Create(request.RequestingTelegramId, source.Name, source.Description);
|
||||
await collections.AddAsync(clone, ct);
|
||||
|
||||
// Копируем все медиа-элементы
|
||||
var items = await mediaItems.GetByCollectionAsync(request.SourceCollectionId, ct);
|
||||
foreach (var item in items)
|
||||
{
|
||||
var copy = MediaItem.Create(
|
||||
request.RequestingTelegramId,
|
||||
clone.Id,
|
||||
item.Source,
|
||||
item.Type,
|
||||
item.Caption,
|
||||
mediaGroupId: null, // клон не связан с оригинальным альбомом
|
||||
item.Tags);
|
||||
|
||||
await mediaItems.AddAsync(copy, ct);
|
||||
}
|
||||
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return clone.Id;
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -15,7 +15,11 @@ public sealed class CopyMediaToCollectionCommandHandler(
|
||||
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)
|
||||
if (source is null)
|
||||
return null;
|
||||
|
||||
var sourceCollection = await collections.GetByIdAsync(source.CollectionId, ct);
|
||||
if (sourceCollection is null || !sourceCollection.CanView(request.RequestingTelegramId))
|
||||
return null;
|
||||
|
||||
var target = await collections.GetByIdAsync(request.TargetCollectionId, ct);
|
||||
|
||||
+5
-2
@@ -9,11 +9,13 @@ namespace MemeVault.Application.Commands.CreateCollection;
|
||||
public sealed class CreateCollectionCommandHandler : IRequestHandler<CreateCollectionCommand, Guid>
|
||||
{
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IBotUserRepository _users;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public CreateCollectionCommandHandler(ICollectionRepository collections, IUnitOfWork uow)
|
||||
public CreateCollectionCommandHandler(ICollectionRepository collections, IBotUserRepository users, IUnitOfWork uow)
|
||||
{
|
||||
_collections = collections;
|
||||
_users = users;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
@@ -21,8 +23,9 @@ public sealed class CreateCollectionCommandHandler : IRequestHandler<CreateColle
|
||||
{
|
||||
const int CollectionOwnerLimit = 5;
|
||||
var owned = await _collections.GetByOwnerAsync(request.OwnerTelegramId, cancellationToken);
|
||||
var user = await _users.GetByTelegramIdAsync(request.OwnerTelegramId, cancellationToken);
|
||||
// Inbox не считается — пользовательские коллекции только
|
||||
if (owned.Count(c => !c.IsInbox) >= CollectionOwnerLimit)
|
||||
if (owned.Count(c => c.Id != user?.InboxCollectionId) >= CollectionOwnerLimit)
|
||||
throw new CollectionQuotaExceededException(CollectionOwnerLimit);
|
||||
|
||||
var collection = Collection.Create(request.OwnerTelegramId, request.Name, request.Description);
|
||||
|
||||
+7
-1
@@ -1,12 +1,14 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.DeleteCollection;
|
||||
|
||||
public sealed class DeleteCollectionCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IBotUserRepository users,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<DeleteCollectionCommand, bool>
|
||||
{
|
||||
@@ -16,7 +18,11 @@ public sealed class DeleteCollectionCommandHandler(
|
||||
if (collection is null || collection.GetRole(request.RequestingTelegramId) != CollectionRole.Owner)
|
||||
return false;
|
||||
|
||||
collection.Delete(request.RequestingTelegramId); // бросает DomainException если IsInbox
|
||||
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
|
||||
if (user?.InboxCollectionId == collection.Id)
|
||||
throw new DomainException("Cannot delete the Inbox collection.");
|
||||
|
||||
collection.Delete(request.RequestingTelegramId);
|
||||
await collections.RemoveAsync(collection, ct);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
|
||||
@@ -5,26 +5,23 @@ using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.DeleteMedia;
|
||||
|
||||
public sealed class DeleteMediaCommandHandler : IRequestHandler<DeleteMediaCommand>
|
||||
public sealed class DeleteMediaCommandHandler(
|
||||
IMediaItemRepository mediaItems,
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork uow)
|
||||
: IRequestHandler<DeleteMediaCommand>
|
||||
{
|
||||
private readonly IMediaItemRepository _mediaItems;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public DeleteMediaCommandHandler(IMediaItemRepository mediaItems, IUnitOfWork uow)
|
||||
{
|
||||
_mediaItems = mediaItems;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task Handle(DeleteMediaCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var item = await _mediaItems.GetByIdAsync(request.MediaItemId, cancellationToken)
|
||||
?? throw new DomainException($"MediaItem {request.MediaItemId} not found.");
|
||||
var item = await mediaItems.GetByIdAsync(request.MediaItemId, cancellationToken);
|
||||
if (item is null)
|
||||
return;
|
||||
|
||||
if (item.OwnerTelegramId != request.RequestingTelegramId)
|
||||
throw new DomainException("You are not the owner of this item.");
|
||||
var collection = await collections.GetByIdAsync(item.CollectionId, cancellationToken);
|
||||
if (collection is null || !collection.CanWrite(request.RequestingTelegramId))
|
||||
throw new DomainException("You don't have permission to delete this item.");
|
||||
|
||||
await _mediaItems.RemoveAsync(item, cancellationToken);
|
||||
await _uow.SaveChangesAsync(cancellationToken);
|
||||
await mediaItems.RemoveAsync(item, cancellationToken);
|
||||
await uow.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ public sealed class EnsureUserCommandHandler : IRequestHandler<EnsureUserCommand
|
||||
// Каждый новый пользователь получает системную коллекцию Inbox
|
||||
var inbox = Collection.CreateInbox(request.TelegramId);
|
||||
await _collections.AddAsync(inbox, cancellationToken);
|
||||
user.SetInboxCollection(inbox.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.JoinPublicCollection;
|
||||
|
||||
/// <param name="CollectionId">ID публичной коллекции.</param>
|
||||
/// <param name="RequestingTelegramId">Telegram ID вступающего пользователя.</param>
|
||||
/// <returns>true — вступил или уже был участником; false — коллекция не найдена.</returns>
|
||||
public sealed record JoinPublicCollectionCommand(
|
||||
Guid CollectionId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<bool>;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.JoinPublicCollection;
|
||||
|
||||
public sealed class JoinPublicCollectionCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<JoinPublicCollectionCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(JoinPublicCollectionCommand request, CancellationToken ct)
|
||||
{
|
||||
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||
if (collection is null)
|
||||
return false;
|
||||
|
||||
// TryJoinPublic бросит DomainException, если Visibility != Public
|
||||
var added = collection.TryJoinPublic(request.RequestingTelegramId);
|
||||
if (added)
|
||||
{
|
||||
await collections.UpdateAsync(collection, ct);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MemeVault.Application.Commands.RegenerateShareLink;
|
||||
|
||||
/// <summary>Перегенерировать share-токен коллекции. Требует роль Manager или Owner.</summary>
|
||||
/// <returns>Новый токен, null если коллекция не найдена.</returns>
|
||||
public sealed record RegenerateShareLinkCommand(
|
||||
Guid CollectionId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<string?>;
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.RegenerateShareLink;
|
||||
|
||||
public sealed class RegenerateShareLinkCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<RegenerateShareLinkCommand, string?>
|
||||
{
|
||||
public async Task<string?> Handle(RegenerateShareLinkCommand request, CancellationToken ct)
|
||||
{
|
||||
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
|
||||
if (collection is null)
|
||||
return null;
|
||||
|
||||
// RegenerateShareLink бросит DomainException при Private или недостаточной роли
|
||||
var token = collection.RegenerateShareLink(request.RequestingTelegramId);
|
||||
|
||||
await collections.UpdateAsync(collection, ct);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -11,15 +11,18 @@ public sealed class SaveMediaCommandHandler : IRequestHandler<SaveMediaCommand,
|
||||
{
|
||||
private readonly IMediaItemRepository _mediaItems;
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IBotUserRepository _users;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public SaveMediaCommandHandler(
|
||||
IMediaItemRepository mediaItems,
|
||||
ICollectionRepository collections,
|
||||
IBotUserRepository users,
|
||||
IUnitOfWork uow)
|
||||
{
|
||||
_mediaItems = mediaItems;
|
||||
_collections = collections;
|
||||
_users = users;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
@@ -38,9 +41,10 @@ public sealed class SaveMediaCommandHandler : IRequestHandler<SaveMediaCommand,
|
||||
}
|
||||
else
|
||||
{
|
||||
var inbox = await _collections.GetInboxAsync(request.OwnerTelegramId, cancellationToken)
|
||||
?? throw new DomainException("User inbox not found.");
|
||||
collectionId = inbox.Id;
|
||||
var user = await _users.GetByTelegramIdAsync(request.OwnerTelegramId, cancellationToken);
|
||||
if (user?.InboxCollectionId is null)
|
||||
throw new DomainException("User inbox not found.");
|
||||
collectionId = user.InboxCollectionId.Value;
|
||||
}
|
||||
|
||||
const int CollectionMediaLimit = 120;
|
||||
|
||||
+7
-8
@@ -8,6 +8,7 @@ namespace MemeVault.Application.Commands.SetCollectionInbox;
|
||||
|
||||
public sealed class SetCollectionInboxCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IBotUserRepository users,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<SetCollectionInboxCommand, bool>
|
||||
{
|
||||
@@ -17,16 +18,14 @@ public sealed class SetCollectionInboxCommandHandler(
|
||||
if (target is null || target.GetRole(request.RequestingTelegramId) != CollectionRole.Owner)
|
||||
return false;
|
||||
|
||||
if (target.IsInbox)
|
||||
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
|
||||
if (user is null) return false;
|
||||
|
||||
if (user.InboxCollectionId == target.Id)
|
||||
throw new DomainException("Collection is already the Inbox.");
|
||||
|
||||
// Снимаем флаг с текущего Inbox
|
||||
var currentInbox = await collections.GetInboxAsync(request.RequestingTelegramId, ct);
|
||||
currentInbox?.DemoteFromInbox();
|
||||
|
||||
// Промоция целевой коллекции
|
||||
target.PromoteToInbox(request.RequestingTelegramId);
|
||||
|
||||
user.SetInboxCollection(target.Id);
|
||||
await users.UpdateAsync(user, ct);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.UpdateCollection;
|
||||
|
||||
public sealed class UpdateCollectionCommandHandler(
|
||||
ICollectionRepository collections,
|
||||
IBotUserRepository users,
|
||||
IUnitOfWork unitOfWork)
|
||||
: IRequestHandler<UpdateCollectionCommand, bool>
|
||||
{
|
||||
@@ -17,6 +19,10 @@ public sealed class UpdateCollectionCommandHandler(
|
||||
if (collection is null || collection.GetRole(request.RequestingTelegramId) is null)
|
||||
return false;
|
||||
|
||||
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
|
||||
if (user?.InboxCollectionId == collection.Id)
|
||||
throw new DomainException("Cannot rename the Inbox collection.");
|
||||
|
||||
collection.Rename(request.Name, request.RequestingTelegramId);
|
||||
await unitOfWork.SaveChangesAsync(ct);
|
||||
return true;
|
||||
|
||||
@@ -15,8 +15,11 @@ public sealed class UpdateMediaCommandHandler(
|
||||
public async Task<bool> Handle(UpdateMediaCommand request, CancellationToken ct)
|
||||
{
|
||||
var item = await mediaItems.GetByIdAsync(request.MediaItemId, ct);
|
||||
if (item is null)
|
||||
return false;
|
||||
|
||||
if (item is null || item.OwnerTelegramId != request.RequestingTelegramId)
|
||||
var sourceCollection = await collections.GetByIdAsync(item.CollectionId, ct);
|
||||
if (sourceCollection is null || !sourceCollection.CanWrite(request.RequestingTelegramId))
|
||||
return false;
|
||||
|
||||
item.UpdateCaption(request.Caption);
|
||||
|
||||
Reference in New Issue
Block a user