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

34 lines
1.2 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,
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;
if (target.IsInbox)
throw new DomainException("Collection is already the Inbox.");
// Снимаем флаг с текущего Inbox
var currentInbox = await collections.GetInboxAsync(request.RequestingTelegramId, ct);
currentInbox?.DemoteFromInbox();
// Промоция целевой коллекции
target.PromoteToInbox(request.RequestingTelegramId);
await unitOfWork.SaveChangesAsync(ct);
return true;
}
}