Ejemplo n.º 1
0
        public ActionResult Index()
        {
            var userId      = User.Identity.GetUserId();
            var userContext = new UserDbContext();

            var friendsContext = new FriendsDbContext();
            var friends        = friendsContext.Friends.Where(u => u.UserId == userId);
            var friendList     = new List <FriendsViewModel>();

            foreach (var friend in friends)
            {
                var user = userContext.Users.FirstOrDefault(u => u.UserId == friend.FriendId);
                friendList.Add(new FriendsViewModel {
                    UserId        = friend.FriendId,
                    ProfilePicUrl = user.ProfilePicUrl,
                    FullName      = user.FirstName + ' ' + user.LastName,
                    BirthYear     = user.BirthDate.Value.Year,
                    Category      = friend.Category
                });
            }

            var requestsContext = new FriendRequestDbContext();
            var requests        = requestsContext.FriendRequests.Where(u => u.FriendId == userId);
            var requestList     = new List <FriendsViewModel>();

            foreach (var request in requests)
            {
                var user = userContext.Users.FirstOrDefault(u => u.UserId == request.UserId);
                requestList.Add(new FriendsViewModel {
                    UserId        = request.UserId,
                    ProfilePicUrl = user.ProfilePicUrl,
                    FullName      = user.FirstName + ' ' + user.LastName,
                    BirthYear     = user.BirthDate.Value.Year,
                });
            }

            var friendCollection = new FriendCollectionViewModel {
                Friends  = friendList,
                Requests = requestList
            };

            ViewBag.CategoryList = GetList.Categories();
            return(View(friendCollection));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(FriendsModel model, string id)
        {
            var friendsContext = new FriendsDbContext();
            var userId         = User.Identity.GetUserId();
            var friendship     = friendsContext.Friends.FirstOrDefault(f => f.UserId == userId && f.FriendId == id);

            if (model.Category != null)
            {
                friendship.Category = model.Category;
                friendsContext.SaveChanges();
                return(RedirectToAction("Index"));
            }

            var userContext = new UserDbContext();
            var friend      = userContext.Users.FirstOrDefault(u => u.UserId == id);
            var fullName    = friend.FirstName + " " + friend.LastName;

            ViewBag.Name         = fullName;
            ViewBag.CategoryList = GetList.Categories();
            return(View(friendship));
        }