protected async override Task <CommentView> HandleInput(CommentDeleteParams input) { using (var connection = database.GetConnection()) { ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); Comment?comment = await commentRepo.FindById(input.CommentId); if (comment == null) { throw new InvalidOperationException(); } // Check to see if they have permission first. if (!(await this.permissionHandler.HasPermission(input.User, PermissionAction.DeleteComment, comment))) { throw new AuthorizationException(); } // (Hopefully) it would be impossible for post to be null if a comment exists... Post post = (await postRepo.FindById(comment.PostId)) !; post.CommentCount -= (comment.ChildCount() + 1); using (var transaction = connection.BeginTransaction()) { await commentRepo.Delete(comment); await postRepo.Update(post); transaction.Commit(); } return(commentMapper.Map(comment)); } }
public IActionResult Delete(int id) { var comment = comments.Read(id); if (comment != null) { comments.Delete(id); } return(Ok(comments)); return(BadRequest(ModelState.ToErrorObject())); }
public ActionResult DeleteConfirmed(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Comment comment = _comm.GetCommentById(id); _comm.Delete(comment); _comm.SaveChanges(); return(RedirectToAction("Index", "Post")); }
public async Task <IActionResult> DeleteComment(int id) { var comment = await _repo.GetCommentById(id); if (comment == null) { return(NotFound()); } if (int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value) != comment.UserId) { return(StatusCode(403)); } _repo.Delete(comment); await _repo.SaveAll(); return(NoContent()); }
public void Delete(int id) { _comment.Delete(id); }