Пример #1
0
        public JsonResult AddComment(int id, string content)
        {
            string message = string.Empty;

            PostComment _postComment = new PostComment();

            string _loginUser = User.Identity.Name.ToString();

            User _CurrUser = db.Users.Where(x => x.UserName == _loginUser).FirstOrDefault();

            if (!String.IsNullOrEmpty(content))
            {
                int _postId = id;
                string _postCommentContent = content;

                Post _post = db.Posts.Where(x => x.PostId == _postId).FirstOrDefault();

                if (_post != null)
                {
                    _post.TotalComments += 1;

                    _postComment.Post = _post;
                    _postComment.PostId = _postId;
                    _postComment.Content = _postCommentContent;
                    //_postComment.CreatedBy = _loginUser;
                    _postComment.CreatedDate = DateTime.Now;
                    //_postComment.PerantFlagId = 1;

                    _postComment.User = _CurrUser;
                }

                try
                {
                    //post counter ++
                    db.Posts.Attach(_post);
                    db.Entry(_post).State = EntityState.Modified;

                    db.PostComments.Add(_postComment);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }

            }
            else
            {
                message = "error!";
            }

            return Json(new
            {
                Html = this.RenderPartialView("_PostComment", _postComment),
                Message = message
            }, JsonRequestBehavior.AllowGet);
        }
Пример #2
0
        public ActionResult Index()
        {
            var _posts = db.Posts.ToList().OrderByDescending(x => x.PostId);

            var pcom = _posts.FirstOrDefault().PostComments.ToList();
            string str1 = "";
            string content1 = "";
            string content2 = "";
            string content3 = "";
            string content4 = "";
            string strConFin = "";
            string comRpl;
            PostComment PComPrev = new PostComment();
            PostComment PComNext = new PostComment();
            bool IsLast = false;
            foreach (var post in _posts.ToList())
            {

                foreach (var itemComment in post.PostComments.ToList())
                {

                    foreach (var itemInn in itemComment.PostCommentChilds.ToList())
                    {
                        //Comment Level 2 <Post Comment Reply>
                        var pCom = itemInn;

                        for (; pCom.PostCommentChilds.Count() > 0; )
                        {
                            //pCom = pCom;
                            if (!IsLast)
                            {
                                content1 = pCom.Content;
                                strConFin = content1;
                            }
                            else
                            {
                                pCom = pCom.PostCommentChilds.FirstOrDefault();
                                content1 = pCom.Content;
                                strConFin = content1;
                                break;
                            }

                            str1 = "";
                            foreach (var itemRepInn in pCom.PostCommentChilds)
                            {
                                PComPrev = pCom;

                                if (itemRepInn.PostCommentChilds.Count() > 0)
                                {
                                    pCom = itemRepInn;
                                }
                                else
                                {
                                    IsLast = true;
                                    strConFin = itemRepInn.Content;

                                }
                                //pCom = itemRepInn;

                                PComNext = pCom;
                                str1 = "";
                                comRpl = pCom.Content;
                            }
                            content3 = PComPrev.Content;
                            content4 = PComNext.Content;

                        }
                        comRpl = pCom.Content;
                        str1 = "";

                    }
                }

            }

            ///////////////////////////////////////////////////////////
            ////////PostComment pComReply = new PostComment();

            ////////string strConFin = "";
            ////////string stComment = "";
            ////////string str1 = "";
            ////////string tt = "";

            ////////foreach (var post in _posts.ToList())
            ////////{

            ////////    foreach (var itemComment in post.PostComments.ToList())
            ////////    {
            ////////        //Comment Level 1 <Post Comment>
            ////////        tt = itemComment.Content;
            ////////        str1 = "";

            ////////        foreach (var itemInn in itemComment.PostCommentChilds.ToList())
            ////////        {
            ////////            //Comment Level 2 <Post Comment Reply>
            ////////            var pCom = itemInn;
            ////////            bool IsLast = false;
            ////////            for (; pCom.PostCommentChilds.Count() > 0 || pCom.PostCommentChilds.Count() == 0; )
            ////////            {
            ////////                if (!IsLast)
            ////////                {
            ////////                    pComReply = pCom;

            ////////                }
            ////////                else
            ////////                {
            ////////                    pCom = pCom.PostCommentChilds.FirstOrDefault();
            ////////                    pComReply = pCom;

            ////////                }
            ////////                //Comment Reply Display Level
            ////////                stComment = pComReply.Content;
            ////////                str1 = "";

            ////////                if (pCom.PostCommentChilds.Count() == 0)
            ////////                {
            ////////                    IsLast = true;
            ////////                }

            ////////                if (IsLast)
            ////////                {
            ////////                    break;
            ////////                }

            ////////                foreach (var itemRepInn in pCom.PostCommentChilds)
            ////////                {
            ////////                    if (itemRepInn.PostCommentChilds.Count() > 0)
            ////////                    {
            ////////                        pCom = itemRepInn;
            ////////                    }
            ////////                    else
            ////////                    {
            ////////                        IsLast = true;
            ////////                    }
            ////////                }

            ////////            }
            ////////            str1 = "";

            ////////        }
            ////////    }

            ////////}
            /////////////////////////////////////////////////////////////////////////////

            return View(_posts);
        }
Пример #3
0
        public JsonResult AddCommentToComment(int postId, int comId, string content)
        {
            string message = string.Empty;

            PostComment _postComment = new PostComment();
            PostComment postComChild = new PostComment();

            //PostComment _postCommentView = new PostComment();

            string _loginUser = User.Identity.Name.ToString();

            User _CurrUser = db.Users.Where(x => x.UserName == _loginUser).FirstOrDefault();

            if (!String.IsNullOrEmpty(content))
            {
                int _postId = postId;
                int _commentId = comId;
                string _postCommentContent = content;

                _postComment = db.PostComments.Where(x => x.CommentId == _commentId).FirstOrDefault();

                Post _post = db.Posts.Where(x => x.PostId == _postId).FirstOrDefault();

                postComChild.Content = _postCommentContent;
                postComChild.CreatedDate = DateTime.Now;
                //pComChild.Child = _postComment;
                //pComChild.PostCommentChilds = _postComment;
                //pComChild.Post = _post;
                //pComChild.PostId = _post.PostId;

                //_postComment.PostId = _postId;
                //_postComment.Content = _postCommentContent;
                ////_postComment.CreatedBy = _loginUser;
                //_postComment.CreatedDate = DateTime.Now;

                postComChild.User = _CurrUser;

                //List<PostComment> lstPComChild = new List<PostComment>();
                //lstPComChild.Add(pComChild);

                //_tempCom.PostCommentChilds = lstPComChild;
                //PostCommentChilds
                //pComChild.PostCommentChilds = _postComment;

                postComChild.PerantPostComment = _postComment;

                //if (_postComment.PostComments==null)
                //{
                //    _postComment.PostComments = new List<PostComment>() {pComChild };
                //}
                //else
                //{
                //    _postComment.PostComments.Add(pComChild);
                //}

                //_postCommentView.CommentId = _postComment.CommentId;
                //_postCommentView.Content = _postComment.Content;
                //_postCommentView.CreatedDate = _postComment.CreatedDate;
                //_postCommentView.PostId = postComChild.PostId;
                ////_postCommentView.PerantFlagId = _postComment.PerantFlagId;
                //_postCommentView.User = _postComment.User;
                //_postCommentView.PostCommentChilds = lstPComChild;

                try
                {
                    //db.PostComments.Attach(_tempCom);
                    //db.Entry(_tempCom).State = EntityState.Modified;

                    //db.PostComments.Add(_postComment);

                    //db.PostComments.Add(pComChild);

                    //db.PostComments.Attach(_postComment);
                    //db.Entry(_postComment).State = EntityState.Modified;

                    _post.TotalComments += 1;

                    //post counter ++
                    db.Posts.Attach(_post);
                    db.Entry(_post).State = EntityState.Modified;

                    _postComment.PostCommentChilds.Add(postComChild);

                    db.SaveChanges();

                    //do  not insert just for display
                    postComChild.PostId = _post.PostId;
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }

            }
            else
            {
                message = "error!";
            }

            return Json(new
            {
                Html = this.RenderPartialView("_PostCommentReplay", postComChild),
                Message = message
            }, JsonRequestBehavior.AllowGet);
        }