public async Task Handle(DeleteFriendshipByFriendIdCommand command, CancellationToken cancellationToken)
        {
            if (command.UserId == null || command.FriendId == null)
            {
                throw new ChatApiException("Could not delete friendship", new[] { "UserId or FriendId is null" });
            }

            var spec1 = new FriendshipSpecification(command.UserId, command.FriendId);
            var spec2 = new FriendshipSpecification(command.FriendId, command.UserId);

            var(friendships1, _) = await _queries.GetFriendships(spec1);

            var(friendships2, _) = await _queries.GetFriendships(spec2);

            var friendship = friendships1.FirstOrDefault() ?? friendships2.FirstOrDefault();

            if (friendship == null)
            {
                throw new ChatApiException("Could not delete friendship", new[] { "Friendship not found" }, 404);
            }

            _friendshipRepository.DeleteById(friendship.Id);
            await _friendshipRepository.UnitOfWork.SaveChangesAsync();

            _logger.LogInformation($"Friendship between {command.UserId} and {command.UserId} deleted");
            _eventBus.Publish(new NotifyUsersIntegrationEvent(new [] { command.UserId, command.FriendId }, "FriendshipDeleted", new { friendship }));
        }
예제 #2
0
        public async Task <IActionResult> GetFriendships(QueryDto query)
        {
            var authHelper = new AuthHelperBuilder()
                             .AllowSystem()
                             .RequirePermissions("users.manage")
                             .Build();

            if (!authHelper.Authorize(_identityService))
            {
                return(Unauthorized());
            }

            var spec = new FriendshipSpecification(query);

            var(friendships, count) = await _friendshipQueries.GetFriendships(spec);

            return(Ok(new ArrayResponse <FriendshipDto>(friendships, count)));
        }
예제 #3
0
        public async Task <FriendshipDto> GetFriendshipById(string id)
        {
            var spec = new FriendshipSpecification(id);

            return((await GetFriendships(spec)).Item1.FirstOrDefault());
        }