public async Task <IViewComponentResult> InvokeAsync(string postId) { if (string.IsNullOrEmpty(postId)) { return(Content("Not found.")); } var post = await _postManager.FindByIdAsync(postId); if (post == null) { return(Content("Not found.")); } var postViewModel = post.ToModel(); postViewModel.EnableComment = postViewModel.EnableComment && _commentsSettings.EnableComments; var comments = await _commentManager.GetCommentsByPostId(postId, true); var commentModels = comments.Select(t => t.ToModel(post.EnableComment && _commentsSettings.EnableComments && _commentsSettings.CommentNestingEnabled)).ToList(); var results = commentModels.Format(); var form = new CommentFormViewModel() { PostId = post.Id }; LoadLastCommentFormUser(form); return(View(new Tuple <PostViewModel, IList <CommentViewModel>, CommentFormViewModel>(postViewModel, results, form))); }
public async Task <IActionResult> Create(int id, string returnUrl) { bool isSupplementExisting = await this.supplementService.IsSupplementExistingById(id, false); if (!isSupplementExisting) { TempData.AddErrorMessage(string.Format(EntityNotFound, SupplementEntity)); return(this.RedirectToHomeIndex()); } string username = User.Identity.Name; bool isUserRestricted = await this.userService.IsUserRestricted(username); if (isUserRestricted) { TempData.AddErrorMessage(UserRestrictedErrorMessage); return(this.RedirectToHomeIndex()); } CommentFormViewModel model = new CommentFormViewModel() { SupplementId = id }; return(View(model)); }
public async Task <IActionResult> SaveComment(CommentFormViewModel comment) { if (!ModelState.IsValid) { return(Post(comment.PostId)); } // comment if (comment.CommentId == 0) { _commentRepository.AddComment(comment.PostId, new Comment { UserName = User.Identity.Name, Message = comment.Message }); await _commentRepository.SaveChangesAsync(); } // sub-comment else { _subCommentRepository.AddSubComment(comment.CommentId, new SubComment { UserName = User.Identity.Name, Message = comment.Message }); await _subCommentRepository.SaveChangesAsync(); } return(RedirectToAction(nameof(Post), new { id = comment.PostId })); }
///id is articleId public ActionResult AddComment(int id, CommentFormViewModel commentModel) { UserEntity currentUser = userService.GetByLogin(Request.GetCurrentUserLogin()); if (currentUser.Ban) { return(RedirectToAction("BanPage", "Account")); } if (ModelState.IsValid) { if (commentModel.CommentId == 0) { //add new comment commentService.Create(new CommentEntity() { ArticleId = id, AuthorId = userService.GetByLogin(Request.GetCurrentUserLogin()).Id, Message = commentModel.Comment }); } else//edit comment { commentService.Edit(commentModel.CommentId, commentModel.Comment); } } return(Redirect(@"~/Article/Show/" + id.ToString())); }
// GET: CommentForms/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CommentForm commentForm = db.CommentForms.Find(id); if (commentForm == null) { return(HttpNotFound()); } var viewModel = new CommentFormViewModel { CommentForm = commentForm, //We don't want to pull back all the procedures, just the one with the //same priority as our comment Procedure = (from p in db.Procedures where p.Priority == commentForm.Priority select p).First() }; return(View(viewModel)); }
// GET: CommentForm/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CommentFormModel commentFormModel = db.CommentFormModels.Find(id); if (commentFormModel == null) { return(HttpNotFound()); } //Daniel: We can populate our new view model entity with a linq query. We have a lot of flexibility var viewModel = new CommentFormViewModel { //We can reuse the comment form from a couple of lines above CommentForm = commentFormModel, //We don't want to pull back all the procedures, just the one with the same priority as our comment Procedure = (from p in db.ProcedureModels where p.Priority == commentFormModel.Priority select p).First() }; //Daniel: We can no longer pass back a comment form. We need to pass back our new view model return(View(viewModel)); // This was was we retunred before //return View(commentFormModel); }
public async Task <IViewComponentResult> InvokeAsync(int postId) { if (postId <= 0) { return(Content("Not found.")); } var post = await _postService.GetByIdAsync(postId); if (post == null) { return(Content("Not found.")); } var postViewModel = _postFactory.ToModel(post, new PostModel()); postViewModel.EnableComment = postViewModel.EnableComment && _commentsSettings.EnableComments; var comments = await _commentService.GetCommentsByPostId(postId, true); var commentModels = comments.Select(t => t.ToViewModel(_commentsSettings.CommentNestingEnabled)).ToList(); var results = commentModels.Format(); var form = new CommentFormViewModel() { PostId = post.Id, EnableCaptchaCode = _commentsSettings.EnableFormVerificationCode, }; LoadLastCommentFormUser(form); return(View(new Tuple <PostModel, IList <CommentViewModel>, CommentFormViewModel>(postViewModel, results, form))); }
// GET: Comments/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Comment comment = db.Comments.Find(id); if (comment == null) { return(HttpNotFound()); } //return View(comment); var viewModel = new CommentFormViewModel { Comment = comment, //We don't want to pull back all the procedures, just one with same priority as out comment //Use LINQ to select (p is a variable) Procedure = (from p in db.Procedures where p.Priority == comment.Priority select p).First() //select first match }; return(View(viewModel)); }
public async Task <IActionResult> SendComment(string id, CommentFormViewModel viewModel) { if (ModelState.IsValid) { string userId = (await _userManager.FindByNameAsync(User.Identity.Name)).Id; var comment = new Comment() { ProductId = id, UserId = userId, Text = viewModel.Text, Status = 0 }; await _db.CommentRepository.AddAsync(comment); await _db.SaveAsync(); ViewBag.SendComment = true; } else { ViewBag.SendComment = false; } string host = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}"; var productName = (await _db.ProductRepository.GetAsync(id))?.Name; string title = Regex.Replace(Regex.Replace(productName.Replace(" ", "-").Trim().ToLower(), "[^\\w]", "-"), "[-]{2,}", "-"); string path = "/Product/" + id + "/" + title; path = String.Join( "/", path.Split("/").Select(s => System.Net.WebUtility.UrlEncode(s)) ); return(Redirect(host + path)); }
public async Task <IActionResult> AddComment(CommentFormViewModel comment) { if (_settingsService.GetReCaptchaEnabled()) { var reCaptchaIsValid = await VerifyReCaptchaToken(HttpContext.Request.Form["g-recaptcha-response"], _settingsService.GetReCaptchaPrivateKey()); if (!reCaptchaIsValid) { ModelState.TryAddModelError("reCaptchaValidationFailed", "You are a robot!"); } } if (!ModelState.IsValid) { var post = await _postService.Get(comment.PostId); return(View(nameof(Post), new PostViewModel(post, comment))); } await _commentService.Add(new CommentDto { PostId = comment.PostId, Author = comment.Name, Email = comment.Email, Content = comment.Content }); return(RedirectToAction(nameof(Post), new { id = comment.PostId })); }
/// <summary> /// Adapts a CommentFormViewModel to a PageComment. /// </summary> /// <param name="formViewModel">The comment form view model.</param> /// <returns>PageComment</returns> private PageComment AdaptCommentFormViewModelToSocialComment(CommentFormViewModel formViewModel) { return(new PageComment { Target = _pageRepository.GetPageId(formViewModel.CurrentPageLink as PageReference), Body = formViewModel.Body, AuthorId = this._userRepository.GetUserId(this.User) }); }
public async Task <IActionResult> AddComment(CommentFormViewModel model) { if (!_commentsSettings.EnableComments) { return(Json(new { result = false, message = "Comment Not Allowed! " })); //ReturnCommentResult(null, new { result = false }); } if (string.IsNullOrEmpty(model.PostId)) { return(Json(new { result = false, message = "Error" })); //return ReturnCommentResult(null, new { result = false }); } var post = await _postManager.FindByIdAsync(model.PostId); if (post == null) { return(Json(new { result = false, message = "Error" })); } if (ModelState.IsValid) { var comment = new Comment() { Author = model.Author, Content = model.Content, Website = model.Website, Email = model.Email, ParentId = model.ParentId, PostId = post.Id, }; var httpFeatures = HttpContext.Features.Get <IHttpConnectionFeature>(); comment.IP = httpFeatures.LocalIpAddress == null ? null : httpFeatures.LocalIpAddress.ToString(); comment.IsApproved = !_commentsSettings.EnableCommentsModeration; if (_commentsSettings.TrustAuthenticatedUsers) { bool isTrust = await _commentManager.IsTrustUserAsync(comment.Email); comment.IsApproved = isTrust; } await _commentManager.CreateAsync(comment); await _postManager.IncreaseCommentsCountAsync(post.Id); SaveCommentFormUser(new LastCommentFormUserInfo() { Author = model.Author, Email = model.Email, Website = model.Website }); return(Json(new { result = true, commentId = comment.Id, parentId = comment.ParentId, url = Url.RouteUrl("Comment", new { commentId = comment.Id }) })); //return ReturnCommentResult(post, new { result = true, comment = comment.ToModel() }); } return(Json(new { result = false, message = string.Join(" ", ModelState.Values.SelectMany(t => t.Errors.Select(e => e.ErrorMessage))) })); }
// GET: CommentController/Create public ActionResult Create(int id) { var vm = new CommentFormViewModel(); Post post = _postRepository.GetPublishedPostById(id); vm.Post = post; return(View(vm)); }
public ActionResult NewComment(int postId) { var viewModel = new CommentFormViewModel { PostId = postId, Comment = new Comments() }; return(View("NewComment", viewModel)); }
/// <summary> /// Validates the comment form. /// </summary> /// <param name="formViewModel">The comment form view model.</param> /// <returns>Returns a list of validation errors.</returns> private List <string> ValidateCommentForm(CommentFormViewModel formViewModel) { var errors = new List <string>(); // Make sure the comment body has some text if (string.IsNullOrWhiteSpace(formViewModel.Body)) { errors.Add(BodyValidationErrorMessage); } return(errors); }
public ActionResult Edit(int id, CommentFormViewModel vm) { try { vm.Comment.CreateDateTime = DateAndTime.Now; _commentRepository.UpdateComment(vm.Comment); return(RedirectToAction("Index", "Comment", new { Id = vm.Comment.PostId })); } catch (Exception ex) { return(View(vm)); } }
//id is comment id public ActionResult EditComment(int id) { CommentEntity comment = commentService.GetById(id); if (comment != null) { TempData[commentModelKey] = new CommentFormViewModel() { Comment = comment.Message, CommentId = id, ArticleId = comment.ArticleId }; TempData["user"] = userService.GetByLogin(Request.GetCurrentUserLogin()).ToPlUser(); return(Redirect(@"~/Article/Show/" + comment.ArticleId.ToString())); } return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> Edit(int id, int supplementId, string returnUrl) { bool isCommentExistingById = await this.commentService.IsCommentExistingById(id, false); if (!isCommentExistingById) { TempData.AddErrorMessage(string.Format(EntityNotFound, CommentEntity)); return(this.RedirectToHomeIndex()); } bool isSupplementExistingById = await this.supplementService.IsSupplementExistingById(supplementId, false); if (!isSupplementExistingById) { TempData.AddErrorMessage(string.Format(EntityNotFound, SupplementEntity)); return(this.RedirectToHomeIndex()); } string username = User.Identity.Name; bool isUserRestricted = await this.userService.IsUserRestricted(username); if (isUserRestricted) { TempData.AddErrorMessage(UserRestrictedErrorMessage); return(this.RedirectToHomeIndex()); } string userId = this.userManager.GetUserId(User); bool isUserAuthor = await this.commentService.IsUserAuthor(id, userId); bool isUserModerator = User.IsInRole(ModeratorRole); if (!isUserAuthor && !isUserModerator) { return(this.RedirectToHomeIndex()); } CommentBasicServiceModel comment = await this.commentService.GetEditModelAsync(id); CommentFormViewModel model = Mapper.Map <CommentFormViewModel>(comment); return(View(model)); }
// GET: CommentController/Edit/5 public ActionResult Edit(int id) { Comment comment = _commentRepository.GetCommentById(id); CommentFormViewModel vm = new CommentFormViewModel() { Post = comment.Post, Comment = comment }; if (comment == null) { return(NotFound()); } return(View(vm)); }
//[ValidateAntiForgeryToken] //Pass in PostId and Comment object from create form public ActionResult Create(int id, CommentFormViewModel vm) { try { vm.Comment.CreateDateTime = DateAndTime.Now; vm.Comment.PostId = id; vm.Comment.UserProfileId = GetCurrentUserProfileId(); _commentRepository.Add(vm.Comment); return(RedirectToAction("Index", "Comment", new { Id = id })); } catch { return(View(vm)); } }
/// <summary> /// Adds the comment in the CommentFormViewModel to the Episerver Social repository. /// </summary> /// <param name="formViewModel">The submitted comment form view model.</param> /// <returns>The added PageComment</returns> private PageComment AddComment(CommentFormViewModel formViewModel) { var newComment = this.AdaptCommentFormViewModelToSocialComment(formViewModel); PageComment addedComment = null; try { addedComment = this._commentRepository.Add(newComment); AddMessage(MessageKey, new MessageViewModel(SubmitSuccessMessage, SuccessMessage)); } catch (SocialRepositoryException ex) { AddMessage(MessageKey, new MessageViewModel(ex.Message, ErrorMessage)); } return(addedComment); }
public ActionResult PostComment(CommentFormViewModel form) { if (ModelState.IsValid) { var comment = new ArticleComment() { ArticleId = form.ArticleId, ParentId = form.ParentId, Name = form.Name, Email = form.Email, Message = form.Message, AddedDate = DateTime.Now, }; _articlesRepo.AddComment(comment); return(RedirectToAction("ContactUsSummary", "Home")); } return(RedirectToAction("Details", new { id = form.ArticleId })); }
public ActionResult HandleSubmitForm(CommentFormViewModel model) { if (!ModelState.IsValid) return CurrentUmbracoPage(); string url = model.FirstName + " " + model.LastName; var newComment = Services.ContentService.CreateContent(url, CurrentPage.Id, "Comment"); newComment.SetValue("firstName", model.FirstName); newComment.SetValue("lastName", model.LastName); newComment.SetValue("comment", model.Comment); Services.ContentService.SaveAndPublishWithStatus(newComment); TempData["success"] = true; return RedirectToCurrentUmbracoPage(); }
private void LoadLastCommentFormUser(CommentFormViewModel model) { var cookieValue = Request.Cookies["lastCommentForm"]; if (!string.IsNullOrEmpty(cookieValue)) { try { var lastForm = JsonConvert.DeserializeObject <LastCommentFormUserInfo>(cookieValue); model.Author = lastForm.Author; model.Email = lastForm.Email; model.Website = lastForm.Website; } catch (Exception) { } } }
public async Task <IActionResult> Create(string returnUrl, CommentFormViewModel model) { if (!ModelState.IsValid) { return(View(model)); } bool isSupplementExisting = await this.supplementService.IsSupplementExistingById(model.SupplementId, false); if (!isSupplementExisting) { TempData.AddErrorMessage(string.Format(EntityNotFound, SupplementEntity)); return(this.RedirectToHomeIndex()); } string username = User.Identity.Name; bool isUserRestricted = await this.userService.IsUserRestricted(username); if (isUserRestricted) { TempData.AddErrorMessage(UserRestrictedErrorMessage); return(this.RedirectToHomeIndex()); } string userId = this.userManager.GetUserId(User); await this.commentService.CreateAsync(model.Content, userId, model.SupplementId); TempData.AddSuccessMessage(string.Format(EntityCreated, CommentEntity)); bool isUserModerator = User.IsInRole(ModeratorRole); if (isUserModerator) { return(RedirectToAction(nameof(SupplementsController.Details), Supplements, new { area = ModeratorArea, id = model.SupplementId, returnUrl })); } return(this.RedirectToAction(nameof(SupplementsController.Details), Supplements, new { id = model.SupplementId, returnUrl })); }
public ActionResult Submit(CommentFormViewModel formViewModel) { var errors = ValidateCommentForm(formViewModel); if (errors.Count() == 0) { var addedComment = AddComment(formViewModel); if (addedComment != null && formViewModel.SendActivity) { AddCommentActivity(addedComment); } } else { // Flag the CommentBody model state with validation error AddMessage(MessageKey, new MessageViewModel(errors.First(), ErrorMessage)); } return(Redirect(UrlResolver.Current.GetUrl(formViewModel.CurrentPageLink))); }
//id is comment id public ActionResult EditComment(int id, CommentFormViewModel model) { UserEntity currentUser = userService.GetByLogin(Request.GetCurrentUserLogin()); if (currentUser.Ban) { return(RedirectToAction("BanPage", "Account")); } if (ModelState.IsValid) { if (commentService.GetById(id).AuthorId == currentUser.Id) { commentService.Edit(id, model.Comment); return(View()); } } //TODO return page where we now return(RedirectToAction("Index", "Home")); }
public ActionResult HandleFormSubmit(CommentFormViewModel model) { if (!ModelState.IsValid) { return(CurrentUmbracoPage()); } //post comment Udi udi = Udi.Parse(@"umb://document/" + CurrentPage.Key.ToString());//Parse Udi for CreateContent(). var newComment = Services.ContentService.CreateContent(model.Title, udi, "commentPost"); newComment.SetValue("title", model.Title); newComment.SetValue("author", model.Author); newComment.SetValue("email", model.Email); //newComment.SetValue("comment", model.Comment); newComment.SetValue("comment", model.Comment); Services.ContentService.SaveAndPublish(newComment); TempData["success"] = true; return(RedirectToCurrentUmbracoPage()); }
public ActionResult SaveComment(Comments comment) { if (!ModelState.IsValid) { var viewModel = new CommentFormViewModel { Comment = comment, PostId = comment.Post_Id }; return(View("NewComment", viewModel)); } Posts post = _context.Posts.SingleOrDefault(m => m.Id == comment.Post_Id); // Checks if comment is new and adds if so if (comment.Id == 0) { comment.TimeCreated = DateTime.Now; post._Comments.Add(comment); _context.Comments.Add(comment); } // Else edits else { var commentInDb = _context.Comments.Single(m => m.Id == comment.Id); commentInDb.Content = comment.Content; } _context.SaveChanges(); // Redirects to post var postViewModel = new PostViewModel { Title = post.Title, Id = post.Id, Content = post.Content, Comments = post._Comments }; return(View("ViewPostDetails", postViewModel)); }
public ActionResult Create(CommentFormViewModel comment) { if (this.User.Identity.IsAuthenticated) { var newComment = new CommentForm { Title = comment.Title, Description = comment.Description, AuthorId = this.User.Identity.GetUserId() }; this.feedbacks.Add(newComment); this.feedbacks.SaveChanges(); this.TempData["Notification"] = "Thank you for your Feedback"; } else { this.TempData["Notification"] = "You must be logged in to live a comment"; } return(this.Redirect("Create")); }
// GET: Comments/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Comment originalComment = db.Comments.Find(id); if (originalComment == null) { return(HttpNotFound()); } var viewModel = new CommentFormViewModel { comment = originalComment, procedure = (from p in db.Procedures where p.Priority == originalComment.Priority select p).First() }; return(View(viewModel)); }