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,
|
2026-10-07 02:45:37 +04:00
|
|
|
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;
|
|
|
|
|
|
2026-10-07 02:45:37 +04:00
|
|
|
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
|
|
|
|
|
if (user is null) return false;
|
2026-10-07 02:17:30 +04:00
|
|
|
|
2026-10-07 02:45:37 +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
|
|
|
|
2026-10-07 02:45:37 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|