Files
TelegramMemeVault/src/MemeVault.Application/Commands/SetCollectionInbox/SetCollectionInboxCommandHandler.cs
T

33 lines
1.1 KiB
C#
Raw Normal View History

2026-10-07 02:17:30 +04:00
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,
2026-10-07 02:17:30 +04:00
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;
2026-10-07 02:17:30 +04:00
if (user.InboxCollectionId == target.Id)
2026-10-07 02:59:23 +04:00
throw new InboxProtectionException("Collection is already the Inbox.");
2026-10-07 02:17:30 +04:00
user.SetInboxCollection(target.Id);
await users.UpdateAsync(user, ct);
2026-10-07 02:17:30 +04:00
await unitOfWork.SaveChangesAsync(ct);
return true;
}
}