public async Task <IActionResult> OnPostAsync() { // if model is valid then process if (ModelState.IsValid) { var mode = "edit"; // if this is a revision to an existing comment then add revision if (CommentEditModel.Id.HasValue) { RevisionAddModel model = new RevisionAddModel { CommentHeaderId = CommentEditModel.Id.Value, Text = CommentEditModel.NewText }; await _service.AddRevisionAsync(model); } // Else add comment - need current user id else { mode = "add"; CommentAddModel model = new CommentAddModel { UserId = User.FindFirstValue(ClaimTypes.NameIdentifier), Text = CommentEditModel.NewText }; await _service.AddCommentAsync(model); } StatusMessage = string.Format("Comment {0}ed successfully", mode); return(RedirectToPage("Index")); } // else redisplay page - validation errors will be displayed return(Page()); }
/// <summary> /// Task for adding a revision to a comment header /// </summary> /// <param name="model">the revision to add</param> /// <returns>Task for adding a revision to a comment header</returns> public async Task AddRevisionAsync(RevisionAddModel model) { CommentRevision revision = new CommentRevision { CommentHeaderId = model.CommentHeaderId, CreatedDate = DateTime.UtcNow, Text = model.Text }; _context.CommentRevisions.Add(revision); await _context.SaveChangesAsync(); }