예제 #1
0
        public ActionResult AddFriend(ProfileViewModel model)
        {
            //Get the current users email
            string email = User.Identity.GetUserName();

            //Find the user using the email
            Contact contact = _contactService.GetContactByEmail(email);

            //find the friend
            Contact contactFriend = _contactService.GetContactById(model.ContactId);

            //find the list of friends of the current logged-in user
            List <Friend> friendList = _contactService.GetUsersFriends(contact.ContactId);

            //check if the current logged-on user has the person already added as a friend
            if (IsAlreadyAFriend(contact.ContactId, contactFriend, friendList))
            {
                ViewData.ModelState.AddModelError("CurrentFriend", "You have already added them to the list");
                return(RedirectToAction("FriendList", "Friends", new { id = contact.ContactId }));
            }

            //if the person's not on the list, initialize a friend object
            Friend friend = new Friend
            {
                ContactID       = contact.ContactId,
                FriendContactID = contactFriend.ContactId
            };

            _contactService.AddFriend(friend);

            //redirect them to the list with the friend added
            return(RedirectToAction("FriendList", "Friends", new { id = contact.ContactId }));
        }