// GET: Comments/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var comment = await _context.Comments.FindAsync(id);

            if (comment == null)
            {
                return(NotFound());
            }

            var user = await this._userManager.GetUserAsync(User);

            ViewModel             = new CommentTagFriendsViewModel();
            ViewModel.CurrentUser = user;
            ViewModel.Comment     = comment;
            ViewModel.Tagged      = GetTaggedFriends(comment.Id, user.Id);

            /*If this method is invoked before GetTaggedFriends,
             * there will add all of the current user`s friends.
             * Let`s get that user x is already tagged from the creation of the comment.
             * It does not make sense the current user to be allowed to tag user x twice.*/
            ViewModel.UserFriends = GetUserFriends(user);

            return(View(ViewModel));
        }
        public async Task <IActionResult> Edit([FromForm] CommentTagFriendsViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = await this._userManager.GetUserAsync(User);

                    ViewModel.CurrentUser = user;

                    var comment = await this._context.Comments
                                  .Include(i => i.TaggedUsers)
                                  .FirstOrDefaultAsync(i => i.Id == viewModel.Comment.Id);

                    var post = await this._context.Posts
                               .FirstOrDefaultAsync(i => i.PostId == _postId);

                    comment.AuthorId        = user.Id;
                    comment.Author          = user;
                    comment.CommentedPostId = post.PostId;
                    comment.CommentedPost   = post;
                    comment.Content         = viewModel.Comment.Content;

                    //Local TagFriend entities
                    var tagFriendEntities = TagFriendEntities(comment);
                    //Connected TagFriend entities (In the db)
                    var commentTagFriendEntities = comment.TaggedUsers;
                    //If there is a mismatch between any record in the Local collection will be deleted from the Connected collection
                    RemoveTaggedFriendRecords(commentTagFriendEntities, tagFriendEntities);
                    //If there is a mismatch between any record in the Connected collection will be added from the Local collection
                    AddLocalTaggedFriends(commentTagFriendEntities, tagFriendEntities);

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CommentExists(viewModel.Comment.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                ViewModel = new CommentTagFriendsViewModel();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewModel));
        }
        // GET: Comments/Create
        public async Task <IActionResult> Create(int?id)
        {
            if (id != null)
            {
                _postId = (int)id;
            }

            var user = await this._userManager.GetUserAsync(User);

            ViewModel = new CommentTagFriendsViewModel()
            {
                CurrentUser = user,
                UserFriends = GetUserFriends(user)
            };

            return(View(ViewModel));
        }
        public async Task <IActionResult> Create([FromForm] CommentTagFriendsViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                //Get current user
                var user = await this._userManager.GetUserAsync(User);

                ViewModel.CurrentUser = user;
                var comment = new Comment()
                {
                    Author      = user,
                    AuthorId    = user.Id,
                    DatePosted  = DateTime.Now,
                    Content     = viewModel.Comment.Content,
                    TaggedUsers = TagFriendEntities()
                };

                //Get the post
                var post = await this._context.Posts.FirstOrDefaultAsync(i => i.PostId == _postId);

                if (post == null)
                {
                    return(NotFound());
                }

                comment.CommentedPost   = post;
                comment.CommentedPostId = post.PostId;

                this._context.Comments.Add(comment);
                await _context.SaveChangesAsync();

                ViewModel = new CommentTagFriendsViewModel();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewModel));
        }