public async Task FriendshipCombinationExistsQueryHandler_ShouldReturnFalse_WhenCombinationDoesNotExist()
        {
            // Arrange
            FriendshipCombinationExistsQuery request = new FriendshipCombinationExistsQuery
            {
                RequesterId = 8761,
                AddresseeId = 242
            };

            _unitOfWorkMock
            .Setup(m => m.Friendships.CombinationExists(request.RequesterId, request.AddresseeId, It.IsAny <CancellationToken>()))
            .ReturnsAsync(false);

            FriendshipCombinationExistsQuery.Handler handler = new FriendshipCombinationExistsQuery.Handler(_unitOfWorkMock.Object);

            // Act
            bool combinationExists = await handler.Handle(request);

            // Assert
            Assert.False(combinationExists);
        }
Exemplo n.º 2
0
        public async Task <ActionResult <FriendshipResource> > RequestFriendship([FromBody] RequestFriendshipBody body, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Get the current user id
            int requesterId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            // Check if requester + addressee id are the same
            if (requesterId == body.AddresseeId)
            {
                return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource
                {
                    StatusCode = StatusCodes.Status403Forbidden,
                    Message = "You cannot create a friendship with yourself"
                }));
            }

            // Check if the addressed user exists
            UserExistsQuery existsQuery = new UserExistsQuery {
                UserId = body.AddresseeId
            };

            bool userExists = await _mediator.Send(existsQuery, cancellationToken);

            if (!userExists)
            {
                return(NotFound(new ErrorResource
                {
                    StatusCode = StatusCodes.Status404NotFound,
                    Message = $"User with ID '{body.AddresseeId}' does not exist"
                }));
            }

            // Check if there is already a friendship with the given user combination
            FriendshipCombinationExistsQuery combinationExistsQuery = new FriendshipCombinationExistsQuery
            {
                RequesterId = requesterId,
                AddresseeId = body.AddresseeId
            };

            bool combinationExists = await _mediator.Send(combinationExistsQuery, cancellationToken);

            if (combinationExists)
            {
                return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource
                {
                    StatusCode = StatusCodes.Status403Forbidden,
                    Message = $"There is already a friendship with user {body.AddresseeId}"
                }));
            }

            // Create friendship
            RequestFriendshipCommand command = _mapper.Map <RequestFriendshipBody, RequestFriendshipCommand>(body);

            FriendshipResource friendship = await _mediator.Send(command, cancellationToken);

            return(CreatedAtAction(nameof(GetFriendshipById), new { friendshipId = friendship.FriendshipId }, friendship));
        }