Пример #1
0
        private async Task <List <UserTweetViewModel> > GetFollowingUsersAndFollowers(string searchString)
        {
            User loggedUser = await _userManager.GetUserAsync(User);

            _logger.LogInfo($"Controller : ManageFollowers | Action : Search | User : {loggedUser.Id} ");

            IEnumerable <User> USERS = new List <User>();

            if (string.IsNullOrWhiteSpace(searchString))
            {
                USERS = await _unitofWork.UserRepository.FindByConditionAync(p => !p.Id.Equals(loggedUser.Id));
            }
            else
            {
                USERS = await _unitofWork.UserRepository
                        .FindByConditionAync(p => (p.FirstName.Contains(searchString) || p.LastName.Contains(searchString) || p.Location.Contains(searchString)) && p.Id != loggedUser.Id);
            }

            List <UserTweetViewModel> AllUsersVM = new List <UserTweetViewModel>();

            if (USERS != null)
            {
                foreach (var followedUser in USERS)
                {
                    List <Tweet> userTweets = await _unitofWork.TweetRepository
                                              .FindByConditionAync(p => p.UserId == followedUser.Id) as List <Tweet>;

                    //fr each user in AspNet users find if logged in user is following him/her. This will return one single user only
                    List <UserFollower> ListFollowedUsers = await GetExistingRecordForUserFollowed(loggedUser.Id, followedUser.Id);

                    List <UserFollower> tempFollowerCount = await _unitofWork.UserFollowerRepository.FindByConditionAync(p => p.FollowerId.Equals(followedUser.Id) && p.FollowAction.Equals(FollowActions.Following)) as List <UserFollower>;

                    List <UserFollower> tempFollowingCount = await _unitofWork.UserFollowerRepository.FindByConditionAync(p => p.UserId.Equals(followedUser.Id) && p.FollowAction.Equals(FollowActions.Following)) as List <UserFollower>;


                    UserTweetViewModel userViewModel = new UserTweetViewModel()
                    {
                        UserId                  = followedUser.Id,
                        UserFullName            = followedUser.FirstName + " " + followedUser.LastName,
                        FollowersCount          = tempFollowerCount.Count,
                        FollowingCount          = tempFollowingCount.Count,
                        TotalTweetCount         = userTweets.Count,
                        UserAccountCreationDate = $"{followedUser.AccountCreationDate.ToString("MMMM")} {followedUser.AccountCreationDate.Year}",
                        UserBirthDay            = $"{followedUser.BirthDay.Value.ToString("MMMM")} {followedUser.BirthDay.Value.Day}",
                        UserAvatarUrl           = followedUser.AvatarUrl,
                        UserBiography           = followedUser.Biography,
                        UserLocation            = followedUser.Location,
                        TwitterHandle           = followedUser.UserName,
                        FollowFlag              = ListFollowedUsers.Where(p => p.FollowAction == FollowActions.Following).Count() > 0 ? true : false
                    };

                    AllUsersVM.Add(userViewModel);
                }
            }

            return(AllUsersVM);
        }
        public async Task <IActionResult> DisplayTweet()
        {
            //Get Logged in user details
            User loggedUser = await GetLoggedInUserDetails();


            _logger.LogInfo($"Get all tweets home page for user: {loggedUser.UserName}");

            _logger.LogInfo($"Controller : UserTweet | Action : DisplayTweet | User : {loggedUser.Id}");

            List <TweetViewModel> TweetVMList = await GetTweetsForDisplaying(loggedUser);

            List <UserFollower> tempFollowerCount = await _unitofWork.UserFollowerRepository.FindByConditionAync(p => p.FollowerId.Equals(loggedUser.Id) && p.FollowAction.Equals(FollowActions.Following)) as List <UserFollower>;

            List <UserFollower> tempFollowingCount = await _unitofWork.UserFollowerRepository.FindByConditionAync(p => p.UserId.Equals(loggedUser.Id) && p.FollowAction.Equals(FollowActions.Following)) as List <UserFollower>;


            UserTweetViewModel userViewModel = new UserTweetViewModel()
            {
                UserId         = loggedUser.Id,
                UserFullName   = loggedUser.FirstName + " " + loggedUser.LastName,
                FollowersCount = tempFollowerCount.Count,
                FollowingCount = tempFollowingCount.Count,
                //TotalTweetCount = TweetVMList == null ? 0 : TweetVMList.Count(),
                UserAccountCreationDate = $"{loggedUser.AccountCreationDate.ToString("MMMM")} {loggedUser.AccountCreationDate.Year}",
                UserBirthDay            = $"{loggedUser.BirthDay.Value.ToString("MMMM")} {loggedUser.BirthDay.Value.Day}",
                UserAvatarUrl           = loggedUser.AvatarUrl,
                UserBiography           = loggedUser.Biography,
                UserLocation            = loggedUser.Location,
                TwitterHandle           = loggedUser.UserName,
                TweetVM = new TweetViewModel()
            };

            userViewModel.OwnTweets = TweetVMList;

            _logger.LogInfo($"Returning data: {userViewModel.TotalTweetCount}");

            return(View(userViewModel));
        }