Skip to content

Commit

Permalink
(#403) users: udpate block user command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 2, 2024
1 parent b705a82 commit a04a8ff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Convey.CQRS.Commands;
using MiniSpace.Services.Students.Core.Repositories;
using MiniSpace.Services.Students.Core.Entities;
using MiniSpace.Services.Students.Application.Exceptions;
using MiniSpace.Services.Students.Application.Services;
using MiniSpace.Services.Students.Core.Entities;
using MiniSpace.Services.Students.Core.Repositories;
using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,11 +13,15 @@ public class BlockUserHandler : ICommandHandler<BlockUser>
{
private readonly IBlockedUsersRepository _blockedUsersRepository;
private readonly IStudentRepository _studentRepository;
private readonly IEventMapper _eventMapper;
private readonly IMessageBroker _messageBroker;

public BlockUserHandler(IBlockedUsersRepository blockedUsersRepository, IStudentRepository studentRepository)
public BlockUserHandler(IBlockedUsersRepository blockedUsersRepository, IStudentRepository studentRepository, IEventMapper eventMapper, IMessageBroker messageBroker)
{
_blockedUsersRepository = blockedUsersRepository;
_studentRepository = studentRepository;
_eventMapper = eventMapper;
_messageBroker = messageBroker;
}

public async Task HandleAsync(BlockUser command, CancellationToken cancellationToken = default)
Expand All @@ -34,14 +39,25 @@ public async Task HandleAsync(BlockUser command, CancellationToken cancellationT
throw new StudentNotFoundException(command.BlockedUserId);
}

var existingBlockedUser = await _blockedUsersRepository.GetAsync(command.BlockerId, command.BlockedUserId);
if (existingBlockedUser != null)
var blockedUsersAggregate = await _blockedUsersRepository.GetAsync(command.BlockerId);
if (blockedUsersAggregate == null)
{
blockedUsersAggregate = new BlockedUsers(command.BlockerId);
}

blockedUsersAggregate.BlockUser(command.BlockedUserId);

if (!blockedUsersAggregate.BlockedUsersList.Any())
{
await _blockedUsersRepository.AddAsync(blockedUsersAggregate);
}
else
{
throw new UserAlreadyBlockedException(command.BlockerId, command.BlockedUserId);
await _blockedUsersRepository.UpdateAsync(blockedUsersAggregate);
}

var blockedUserEntity = new BlockedUser(command.BlockerId, command.BlockedUserId, DateTime.UtcNow);
await _blockedUsersRepository.AddAsync(blockedUserEntity);
var events = _eventMapper.MapAll(blockedUsersAggregate.Events);
await _messageBroker.PublishAsync(events.ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Convey.CQRS.Events;
using System;

namespace MiniSpace.Services.Students.Application.Events
{
public class UserBlocked : IEvent
{
public Guid BlockerId { get; }
public Guid BlockedUserId { get; }

public UserBlocked(Guid blockerId, Guid blockedUserId)
{
BlockerId = blockerId;
BlockedUserId = blockedUserId;
}
}
}

0 comments on commit a04a8ff

Please sign in to comment.