Initial commit
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
using MediatR;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Commands.ManageCollectionMember;
|
||||
|
||||
public sealed class ManageCollectionMemberCommandHandler
|
||||
: IRequestHandler<ManageCollectionMemberCommand>,
|
||||
IRequestHandler<RemoveCollectionMemberCommand>
|
||||
{
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public ManageCollectionMemberCommandHandler(ICollectionRepository collections, IUnitOfWork uow)
|
||||
{
|
||||
_collections = collections;
|
||||
_uow = uow;
|
||||
}
|
||||
|
||||
public async Task Handle(ManageCollectionMemberCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var collection = await _collections.GetByIdAsync(request.CollectionId, cancellationToken)
|
||||
?? throw new DomainException($"Collection {request.CollectionId} not found.");
|
||||
|
||||
collection.AddMember(request.TargetTelegramId, request.Role, request.RequestingTelegramId);
|
||||
await _collections.UpdateAsync(collection, cancellationToken);
|
||||
await _uow.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task Handle(RemoveCollectionMemberCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var collection = await _collections.GetByIdAsync(request.CollectionId, cancellationToken)
|
||||
?? throw new DomainException($"Collection {request.CollectionId} not found.");
|
||||
|
||||
collection.RemoveMember(request.TargetTelegramId, request.RequestingTelegramId);
|
||||
await _collections.UpdateAsync(collection, cancellationToken);
|
||||
await _uow.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user