public async Task <IActionResult> GetFollowersAsync(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(StatusCode(500, $"requested user not found")); } var model = _mapper.Map <ICollection <Network>, IEnumerable <FollowerViewModel> >(requestedUser.Followers); var requesterUsername = User.Identity.Name; if (requestedUser is null) { _logger.LogError(LoggingEvents.GetItem, $"Username '{requestedUsername}' not found at GetUserProfileAsync action"); return(StatusCode(500, $"requested user not found")); } if (requesterUsername.Equals(requestedUsername)) { // Own profile requested... UserNetworkHelpers.SetupFollowersCollection(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.SetupFollowersCollection(requestingUser, model); return(Ok(model)); } } catch (Exception ex) { _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred"); return(StatusCode(500, "an unexpected error occurred")); } }
public void SetupFollowersCollection_ReturnsArgumentNullException_WhenFollowersIsNull() { ApplicationUser requestingUser = new ApplicationUser(); IEnumerable <FollowerViewModel> followers = null; // Act & Assert var ex = Assert.Throws <ArgumentNullException>(() => UserNetworkHelpers.SetupFollowersCollection(requestingUser, followers)); Assert.Equal("The followers collection is null (Parameter 'followers')", ex.Message); }