Ejemplo n.º 1
0
        public ActionResult Create(CommentBindingModel model)
        {
            if (model == null || !this.ModelState.IsValid)
            {
                this.AddNotification("Invalid input data", NotificationType.ERROR);
                return this.RedirectToAction("Details", "Snippets");
            }

            var userId = this.User.Identity.GetUserId();

            var comment = new Comment
                {
                    AuthorId = userId,
                    Content = model.Content,
                    CreatedOn = DateTime.Now,
                    SnippetId = model.SnippetId
                };

            this.Data.Comments.Add(comment);
            this.Data.SaveChanges();

            var commentView = this.Data.Comments.GetAll()
                                  .Where(c => c.Id == comment.Id)
                                  .ProjectTo<CommentViewModel>()
                                  .FirstOrDefault();

            this.AddNotification("Your Comment was accepted", NotificationType.SUCCESS);
            return this.PartialView("_CommentRow", commentView);
        }
        public ActionResult Create(int id, CommentBindingModel model)
        {
            var snippet = this.Data.Snippets.Find(id);
            if (snippet == null)
            {
                TempData["messages_err"] = "Could not find snippet";
                return RedirectToAction("Index", "Home");
            }

            if (model != null && this.ModelState.IsValid)
            {
                var autorId = this.User.Identity.GetUserId();
                var comment = new Comment()
                {
                    Content = model.Content,
                    CreationDate = DateTime.Now,
                    Snippet = snippet,
                    Author = this.Data.Users.Find(autorId)
                };
                this.Data.Comments.Add(comment);
                this.Data.SaveChanges();
                TempData["messages_succ"] = "Successfully added comment";
            }
            return RedirectToAction("Details", "Snippets", new {id = id});
        }
        public ActionResult Post(ModelWrapper m)
        {
            var userId = User.Identity.GetUserId();

            var comment = new Comment
            {
                Content = m.CommentBindingModel.Content,
                Author = this.Data.Users.All().FirstOrDefault(u => u.Id == userId),
                CreatedOn = DateTime.Now,
                Snippet = this.Data.Snippets.All().FirstOrDefault(s => s.Id == m.CommentBindingModel.SnippetId)
            };

            this.Data.Comments.Add(comment);
            this.Data.SaveChanges();

            return RedirectToAction("Details","Snippets", new {id = m.CommentBindingModel.SnippetId});
        }
        public ActionResult EditCommentDetails(int id, Comment commentModel)
        {
            var comment = this.Data.Comments.Find(id);
            if (comment == null)
            {
                return this.HttpNotFound();
            }

            var snippetId = comment.Snippet.Id;

            if (ModelState.IsValid)
            {
                comment.Content = commentModel.Content;
                
                this.Data.SaveChanges();

                this.TempData["Message"] = "The comment was updated successfully.";
                return RedirectToAction("SnippetDetails", "Snippets", new { snippetId = snippetId });
            }

            return this.View(comment);
        }
        public ActionResult AddComment(Comment commentModel, int snippetId)
        {
            if (commentModel != null && this.ModelState.IsValid)
            {
                var comment = new Comment()
                {
                    CreationTime = DateTime.Now,
                    Content = commentModel.Content,
                    Author = this.Data.Users.All().FirstOrDefault(u => u.UserName == this.UserProfile.UserName),
                    Snippet = this.Data.Snippets.Find(snippetId)
                };

                this.Data.Comments.Add(comment);
                this.Data.SaveChanges();

                var commentDb = this.Data.Comments.All()
                    .FirstOrDefault(c => c.Id == comment.Id);
                var model = Mapper.Map<Comment, CommentViewModel>(commentDb);

                return this.PartialView("DisplayTemplates/CommentViewModel", model);
            }

            return this.Json("Error");
        }