public void SetupFollowingCollection_ReturnsArgumentNullException_WhenFollowingIsNull()
        {
            ApplicationUser requestingUser             = new ApplicationUser();
            IEnumerable <FollowingViewModel> following = null;

            // Act & Assert
            var ex = Assert.Throws <ArgumentNullException>(() => UserNetworkHelpers.SetupFollowingCollection(requestingUser, following));

            Assert.Equal("The following collection is null (Parameter 'following')", ex.Message);
        }
Exemple #2
0
        public async Task <IActionResult> GetFollowingAsync(string requestedUsername)
        {
            if (string.IsNullOrEmpty(requestedUsername))
            {
                _logger.LogError(LoggingEvents.GetListNotFound, "The search criterion is null or empty");
                return(BadRequest("No search criterion"));
            }

            try
            {
                var requestedUser = await _userManager.GetUserWithNetworkAsync(requestedUsername);

                if (requestedUser is null)
                {
                    _logger.LogError(LoggingEvents.GetItem, $"Username '{requestedUsername}' not found at GetUserProfileAsync action");
                    return(NotFound("Requested user not found"));
                }

                var model = _mapper.Map <ICollection <Network>, IEnumerable <FollowingViewModel> >(requestedUser.Following);

                var requesterUsername = User.Identity.Name;

                if (requesterUsername.Equals(requestedUsername))
                {
                    // Own profile requested...
                    UserNetworkHelpers.SetupFollowingCollection(requestedUser, model);
                    return(Ok(model));
                }
                else
                {
                    // Other user's profile requested...
                    var requestingUser = await _userManager.GetUserWithNetworkAsync(requesterUsername);

                    if (requestingUser is null)
                    {
                        _logger.LogError(LoggingEvents.GetItem, $"Username '{requesterUsername}' not found at GetUserProfileAsync action");
                        return(StatusCode(500, $"requesting user not found"));
                    }

                    UserNetworkHelpers.SetupFollowingCollection(requestingUser, model);

                    return(Ok(model));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred");
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }