Exemple #1
0
        // GET: Home
        public ActionResult Index()
        {
            var vm       = new ViewModelComments();
            var comments = vm.Comments.Take(5);

            return(View(comments));
        }
        public ActionResult _CommentListById(int id)
        {
            var vm       = new ViewModelComments();
            var comments = vm.Comments.Where(c => c.ID == id);

            return(View("_CommentList", comments));
        }
        public async Task <IActionResult> Comment(ViewModelComments comments)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Json(new
                {
                    status = 404
                }));
            }

            if (!ModelState.IsValid)
            {
                return(Json(new
                {
                    status = 400
                }));
            }
            else
            {
                try
                {
                    string appUserId = _userManager.GetUserId(HttpContext.User);
                    ViewBag.UserId = appUserId;
                    var user = await _userManager.FindByIdAsync(appUserId);

                    if (user != null)
                    {
                        Comment comment = new Comment();
                        comment.Message   = comments.Text;
                        comment.AppUser   = user;
                        comment.AppUserId = appUserId;
                        comment.Name      = comments.Name;
                        comment.Website   = comments.Website;
                        comment.Email     = comments.Email;
                        comment.DateTime  = DateTime.Now;

                        await _context.Comments.AddAsync(comment);

                        await _context.SaveChangesAsync();

                        return(Json(new
                        {
                            status = 200,
                            data = comment
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            status = 400
                        }));
                    }
                }
                catch (Exception ex)
                {
                    return(Json(new
                    {
                        status = 500,
                        error = ex.Message
                    }));
                }
            }
        }
        public async Task <IActionResult> ReplyComment(int id, ViewModelComments replyComment)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Json(new
                {
                    status = 404
                }));
            }

            if (!ModelState.IsValid)
            {
                return(Json(new
                {
                    status = 400
                }));
            }
            else
            {
                string appUserId = _userManager.GetUserId(HttpContext.User);
                var    user      = await _userManager.FindByIdAsync(appUserId);

                if (user != null)
                {
                    try
                    {
                        Reply reply = new Reply();
                        reply.Message   = replyComment.Text;
                        reply.AppUser   = user;
                        reply.AppUserId = appUserId;
                        reply.CommentId = id;
                        reply.Name      = replyComment.Name;
                        reply.Website   = replyComment.Website;
                        reply.Email     = replyComment.Email;
                        reply.DateTime  = DateTime.Now;
                        await _context.Replies.AddAsync(reply);

                        await _context.SaveChangesAsync();

                        return(Json(new
                        {
                            status = 200,
                            data = reply
                        }));
                    }
                    catch (Exception ex)
                    {
                        return(Json(new
                        {
                            status = 500,
                            error = ex.Message
                        }));
                    }
                }
                else
                {
                    return(Json(new
                    {
                        status = 400
                    }));
                }
            }
        }