public async Task<ActionResult> Create([Bind(Include = "PostID,ParentCommentID,Content")] CommentView commentView) { string userId = User.Identity.GetUserId(); if (ModelState.IsValid && await GetDb().Posts.AnyAsync(p => p.PostID == commentView.PostID && (p.CreatedOn != null || p.AuthorID == userId)) && (commentView.ParentCommentID == null || await GetDb().Comments.AnyAsync(c => c.CommentID == commentView.ParentCommentID))) { Comment comment = new Comment(); if (!await GetDb().Posts.AnyAsync(p => p.PostID == commentView.PostID)) return new EmptyResult(); comment.PostID = commentView.PostID; if (await GetDb().Comments.AnyAsync(c => c.CommentID == commentView.ParentCommentID)) comment.ParentCommentID = commentView.ParentCommentID; comment.AuthorID = User.Identity.GetUserId(); comment.CreatedOn = new DateTimeOffset(DateTime.Now); comment = GetDb().Comments.Add(comment); await GetDb().SaveChangesAsync(); CommentContent commentContent = new CommentContent(); commentContent.CommentID = comment.CommentID; commentContent.Content = commentView.Content; GetDb().CommentContents.Add(commentContent); await GetDb().SaveChangesAsync(); return PartialView("Comments/_CommentPartial", comment); } return new EmptyResult(); }
public async Task<ActionResult> Edit([Bind(Include = "TempTokens,Content,UpdateReason,IsDeleted")] EditedCommentView commentView) { Comment comment = await GetDb().Comments.FindAsync(this.GetTemp<int>(commentView, "CommentID")); if (comment == null) { this.ClearTemp(commentView); return new EmptyResult(); } if (ModelState.IsValid) { CommentContent commentContent = new CommentContent(); commentContent.CommentID = comment.CommentID; commentContent.EditorID = User.Identity.GetUserId(); commentContent.UpdatedOn = new DateTimeOffset(DateTime.Now); commentContent.UpdateReason = commentView.UpdateReason; commentContent.IsDeleted = commentView.IsDeleted; commentContent.Content = commentView.Content; GetDb().CommentContents.Add(commentContent); this.ClearTemp(commentView); await GetDb().SaveChangesAsync(); } return PartialView("Comments/_CommentPartial", comment); /* if (ModelState.IsValid && (postView.DoPublish || post.CreatedOn == null)) { PostContent postContent = await this.GetDb().PostContents.FirstAsync(pc => pc.PostID == post.PostID); if (!postView.DoPublish) // This is a draft that needs to not be published { postContent.Extract = postView.Extract; postContent.Content = postView.Content; this.GetDb().Entry(postContent).State = EntityState.Modified; } else if (post.CreatedOn == null) // This is an edited draft { if (postView.IsDeleted) // The draft needs to be deleted { this.GetDb().PostContents.Remove(postContent); this.GetDb().Posts.Remove(post); } else // The draft needs to be published { post.CreatedOn = new DateTimeOffset(DateTime.Now); this.GetDb().Entry(post).State = EntityState.Modified; postContent.Extract = postView.Extract; postContent.Content = postView.Content; this.GetDb().Entry(postContent).State = EntityState.Modified; } } else // This is a pre-existing post { postContent = new PostContent(); postContent.PostID = post.PostID; postContent.EditorID = (await this.GetUserManager().FindByNameAsync(User.Identity.Name)).Id; postContent.UpdatedOn = new DateTimeOffset(DateTime.Now); postContent.UpdateReason = postView.UpdateReason; postContent.Extract = postView.Extract; postContent.Content = postView.Content; postContent.IsDeleted = postView.IsDeleted; this.GetDb().PostContents.Add(postContent); } this.ClearTemp(postView); await this.GetDb().SaveChangesAsync(); return RedirectToAction("Index"); } if (postView.DoPublish && post.CreatedOn == null) postView.DoPublish = false; postView.Title = post.Title; */ //return View(); }