using MediatR; using MemeVault.Application.Common; using MemeVault.Domain.Enums; using MemeVault.Domain.Exceptions; using MemeVault.Domain.Repositories; namespace MemeVault.Application.Commands.SetCollectionInbox; public sealed class SetCollectionInboxCommandHandler( ICollectionRepository collections, IBotUserRepository users, IUnitOfWork unitOfWork) : IRequestHandler { public async Task Handle(SetCollectionInboxCommand request, CancellationToken ct) { var target = await collections.GetByIdAsync(request.CollectionId, ct); if (target is null || target.GetRole(request.RequestingTelegramId) != CollectionRole.Owner) return false; var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct); if (user is null) return false; if (user.InboxCollectionId == target.Id) throw new InboxProtectionException("Collection is already the Inbox."); user.SetInboxCollection(target.Id); await users.UpdateAsync(user, ct); await unitOfWork.SaveChangesAsync(ct); return true; } }