コード例 #1
0
        /**
         * Purpose: Method that is used to return the /Profile/Friends that shows all of a users friends
         *           view and sets properties of the ProfileFriendsViewModel
         * Arguments:
         *      int id - the userId of the profile being visited
         * Return:
         *      the view for the Profile/Friends view
         */
        public IActionResult Friends([FromRoute] int id)
        {
            ProfileFriendsViewModel model = new ProfileFriendsViewModel(context, id);

            List <Relationship> relationships = context.Relationship.Where(r => r.ReciverUserId == id || r.SenderUserId == id).ToList();

            model.Friends = new List <User> {
            };

            foreach (Relationship r in relationships)
            {
                User friend;
                if (r.ReciverUserId == id && r.Status == 1)
                {
                    friend = context.User.Where(u => u.UserId == r.SenderUserId).SingleOrDefault();
                    model.Friends.Add(friend);
                }
                else if (r.SenderUserId == id && r.Status == 1)
                {
                    friend = context.User.Where(u => u.UserId == r.ReciverUserId).SingleOrDefault();
                    model.Friends.Add(friend);
                }
            }

            model.Friends.OrderBy(f => f.FirstName);

            return(View(model));
        }
コード例 #2
0
        public IActionResult Friends(string name, int page)
        {
            var model = new ProfileFriendsViewModel();

            model.Name = name;
            model.Page = page == 0 ? 1 : page;

            Guid userId;
            var  userIdClaim = this.User.Claims.FirstOrDefault(f => f.Type == ClaimTypes.NameIdentifier);

            var user = this.usersManager.GetItem(Guid.TryParse(userIdClaim.Value, out userId) ? userId : Guid.Empty) as User;

            if (user != null)
            {
                var friendRequests       = (this.friendRequestsManager.GetItems() as IEnumerable <FriendRequest>).Where(fr => fr.ReceiverId == user.Id || fr.SenderId == user.Id);
                var oneWayFriendRequests = (this.friendRequestsManager.GetItems() as IEnumerable <FriendRequest>).Where(fr => fr.ReceiverId == user.Id);

                var approvedFriendRequests = friendRequests.Where(fr => fr.Status == FriendRequestStatusType.Approved);
                var pendingFriendRequests  = oneWayFriendRequests.Where(fr => fr.Status == FriendRequestStatusType.Pending);

                model.ApprovedFriendRequests = new List <FriendRequestViewModel>();
                foreach (var request in approvedFriendRequests)
                {
                    User requestUser = null;

                    if (request.ReceiverId != user.Id)
                    {
                        requestUser = this.usersManager.GetItem(request.ReceiverId) as User;
                    }
                    else if (request.SenderId != user.Id)
                    {
                        requestUser = this.usersManager.GetItem(request.SenderId) as User;
                    }

                    if (requestUser != null)
                    {
                        model.ApprovedFriendRequests.Add(new FriendRequestViewModel()
                        {
                            FriendRequestId = request.Id,
                            UserId          = requestUser.Id,
                            Name            = $"{requestUser.FirstName} {requestUser.LastName}",
                            Email           = requestUser.Email
                        });
                    }
                }

                model.PendingFriendRequests = new List <FriendRequestViewModel>();
                foreach (var request in pendingFriendRequests)
                {
                    User requestUser = null;

                    if (request.ReceiverId != user.Id)
                    {
                        requestUser = this.usersManager.GetItem(request.ReceiverId) as User;
                    }
                    else if (request.SenderId != user.Id)
                    {
                        requestUser = this.usersManager.GetItem(request.SenderId) as User;
                    }

                    if (requestUser != null)
                    {
                        model.PendingFriendRequests.Add(new FriendRequestViewModel()
                        {
                            FriendRequestId = request.Id,
                            UserId          = requestUser.Id,
                            Name            = $"{requestUser.FirstName} {requestUser.LastName}",
                            Email           = requestUser.Email
                        });
                    }
                }

                if (!string.IsNullOrEmpty(name))
                {
                    model.ApprovedFriendRequests = model.ApprovedFriendRequests.Where(r => r.Name.ToLower().Contains(name.ToLower())).ToList();
                }

                model.TotalCount = model.ApprovedFriendRequests.Count();
                model.TotalPages = (model.TotalCount + pageSize - 1) / pageSize;
                model.Page       = model.TotalPages >= model.Page ? model.Page : model.TotalPages;
            }

            return(View(model));
        }