Files
TelegramMemeVault/src/MemeVault.Application/Commands/SetCollectionInbox/SetCollectionInboxCommandHandler.cs
T
2026-10-07 02:59:23 +04:00

33 lines
1.1 KiB
C#

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<SetCollectionInboxCommand, bool>
{
public async Task<bool> 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;
}
}