public void GetFollowingUserNames_ReturnsNullReferenceException_WhenArgumentIsNull()
        {
            // Act & Assert
            var ex = Assert.Throws <NullReferenceException>(() => UserNetworkHelpers.GetFollowingUserNames(null));

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

                var requestingUser = await _userManager.GetUserWithNetworkAsync(User.Identity.Name);

                if (requestingUser is null)
                {
                    _logger.LogError(LoggingEvents.GetItemNotFound, "The user was not found");
                    return(StatusCode(500, $"requesting user not found"));
                }

                var followingUsernamesList = UserNetworkHelpers.GetFollowingUserNames(requestingUser.Following);
                followingUsernamesList.Add(requestingUser.UserName);

                //var users = await _SearchBirdersToFollowAsync(searchCriterion, followingUsernamesList);
                var users = await _userManager.GetUsersAsync(user => user.NormalizedUserName.Contains(searchCriterion.ToUpper()) && !followingUsernamesList.Contains(user.UserName));

                return(Ok(_mapper.Map <IEnumerable <ApplicationUser>, IEnumerable <NetworkUserViewModel> >(users)));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred");
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }
        public void GetFollowingUserNames_ReturnsEmptyCollection_WhenInputCollectionIsEmpty()
        {
            // Arrange
            var emptyInputCollection = new List <Network>();

            // Act
            var result = UserNetworkHelpers.GetFollowingUserNames(emptyInputCollection);

            // Assert
            Assert.IsType <List <String> >(result);
            Assert.Empty(result);
        }
        public void GetFollowingUserNames_ReturnsCollection_WhenInputCollectionLengthIsGreaterThan0(int length)
        {
            // Arrange
            var testCollection = GetDynamicNetworkCollection(length);

            // Act
            var result = UserNetworkHelpers.GetFollowingUserNames(testCollection);

            // Assert
            Assert.IsType <List <String> >(result);
            Assert.NotEmpty(result);
            Assert.Equal(length, result.Count);
            Assert.Equal("Test 1", result[0]);
            Assert.Equal("Test " + length.ToString(), result[length - 1]);
        }
예제 #5
0
        public async Task <IActionResult> GetNetworkSummaryAsync()
        {
            try
            {
                var requestingUser = await _userManager.GetUserWithNetworkAsync(User.Identity.Name);

                if (requestingUser is null)
                {
                    _logger.LogError(LoggingEvents.GetItemNotFound, "requesting user not found");
                    return(StatusCode(500, "requesting user not found"));
                }

                var model = new NetworkSummaryDto
                {
                    FollowersCount = requestingUser.Followers.Count,

                    FollowingCount = requestingUser.Following.Count
                };

                var followersNotBeingFollowed = UserNetworkHelpers.GetFollowersNotBeingFollowedUserNames(requestingUser);

                if (followersNotBeingFollowed.Any())
                {
                    // ToDo: needs to be paged or Take(x)
                    var users = await _userManager.GetUsersAsync(user => followersNotBeingFollowed.Contains(user.UserName));

                    model.SuggestedUsersToFollow = _mapper.Map <IEnumerable <ApplicationUser>, IEnumerable <NetworkUserViewModel> >(users);
                    return(Ok(model));
                }
                else
                {
                    // ToDo: needs to be paged or Take(x)
                    var followingUsernamesList = UserNetworkHelpers.GetFollowingUserNames(requestingUser.Following);
                    var users = await _userManager.GetUsersAsync(user => !followingUsernamesList.Contains(user.UserName) && user.UserName != requestingUser.UserName);

                    model.SuggestedUsersToFollow = _mapper.Map <IEnumerable <ApplicationUser>, IEnumerable <NetworkUserViewModel> >(users);
                    return(Ok(model));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred");
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }
        public async Task <IActionResult> GetObservationsFeedAsync(int pageIndex, ObservationFeedFilter filter)
        {
            try
            {
                if (filter == ObservationFeedFilter.Own)
                {
                    var userObservations = await _observationQueryService.GetPagedObservationsFeedAsync(o => o.ApplicationUser.UserName == User.Identity.Name, pageIndex, pageSize);

                    if (userObservations is null)
                    {
                        _logger.LogWarning(LoggingEvents.GetListNotFound, "User Observations list was null at GetObservationsFeedAsync()");
                        return(StatusCode(500, $"User observations object is null"));
                    }

                    if (userObservations.TotalItems > 0 || pageIndex > 1)
                    {
                        // _profilePhotosService.GetThumbnailUrl(userObservations.Items);
                        userObservations.ReturnFilter = ObservationFeedFilter.Own;
                        return(Ok(userObservations));
                    }
                }


                /*
                 * if network observations is requested, or
                 * if user's own observations is request BUT user has no observations
                 */
                if (filter == ObservationFeedFilter.Network || filter == ObservationFeedFilter.Own)
                {
                    var requestingUserAndNetwork = await _userManager.GetUserWithNetworkAsync(User.Identity.Name);

                    if (requestingUserAndNetwork is null)
                    {
                        _logger.LogWarning(LoggingEvents.GetItemNotFound, "Requesting user not found");
                        return(StatusCode(500, $"requesting user not found"));
                    }

                    var followingUsernamesList = UserNetworkHelpers.GetFollowingUserNames(requestingUserAndNetwork.Following);

                    followingUsernamesList.Add(requestingUserAndNetwork.UserName);

                    var networkObservations = await _observationQueryService.GetPagedObservationsFeedAsync(o => followingUsernamesList.Contains(o.ApplicationUser.UserName), pageIndex, pageSize);

                    if (networkObservations is null)
                    {
                        _logger.LogWarning(LoggingEvents.GetListNotFound, "Network observations list is null");
                        return(StatusCode(500, $"Network observations object is null"));
                    }

                    if (networkObservations.TotalItems > 0 || pageIndex > 1)
                    {
                        // move to ObservationQueryService?
                        //_profilePhotosService.GetThumbnailUrl(networkObservations.Items);
                        networkObservations.ReturnFilter = ObservationFeedFilter.Network;
                        return(Ok(networkObservations));
                    }
                }


                /*
                 * if public observations is requested, or
                 * if user's own or network observations is request BUT user/network has no observations
                 */
                var publicObservations = await _observationQueryService.GetPagedObservationsFeedAsync(pl => pl.SelectedPrivacyLevel == PrivacyLevel.Public, pageIndex, pageSize);

                if (publicObservations is null)
                {
                    _logger.LogWarning(LoggingEvents.GetListNotFound, "Observations list is null");
                    return(StatusCode(500, $"Public observations object is null"));
                }

                //_profilePhotosService.GetThumbnailUrl(publicObservations.Items);
                publicObservations.ReturnFilter = ObservationFeedFilter.Public;
                return(Ok(publicObservations));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.GetListNotFound, ex, "An error occurred getting the observations feed");
                return(StatusCode(500, "An unexpected error occurred"));
            }
        }