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 { public async Task Handle(UpdateCollectionCommand request, CancellationToken ct) { var collection = await collections.GetByIdAsync(request.CollectionId, ct); // Если коллекции нет или пользователь — не участник, возвращаем 404. // Если роль ниже Manager — domain.Rename() бросит DomainException. 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; } }