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)); } }
protected async override Task <CommentView> HandleInput(CommentCreateParams input) { using (var connection = database.GetConnection()) { IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); // Locate the post to ensure it actually exists. Post?post = await postRepo.FindById(input.PostId); if (post == null) { throw new InvalidOperationException(); } using (var transaction = connection.BeginTransaction()) { Comment comment = new Comment() { User = input.User, PostId = post.Id, Body = input.Body, CreationDate = DateTime.UtcNow }; // Set the parent comment if needed. if (input.ParentId != 0) { comment.Parent = await commentRepo.FindById(input.ParentId); } // Update the comment count cache on the post. post.CommentCount++; comment.Upvotes++; await commentRepo.Add(comment); Vote upvote = new Vote() { User = input.User, ResourceId = comment.Id, ResourceType = VoteResourceType.Comment, Direction = VoteDirection.Up }; await postRepo.Update(post); await voteRepo.Add(upvote); comment.Vote = upvote; transaction.Commit(); return(commentMapper.Map(comment)); } } }
public async Task <IActionResult> Delete([FromBody] int commentId) { if (ModelState.IsValid) { try { if (_commentRepo.HardDelete(_commentRepo.FindById(commentId))) { _commentRepo.Save(); return(Ok()); } return(NoContent()); } catch (Exception ex) { throw new Exception(ex.Message); } } else { return(BadRequest()); } }
public async Task Handle(VoteOnCommentEvent domainEvent) { Comment?p = await repo.FindById(domainEvent.CommentId); if (p == null) { throw new InvalidOperationException(); } if (domainEvent.OldVote != null) { p.Votes.RemoveVote(domainEvent.OldVote.Direction); } p.Votes.AddVote(domainEvent.NewVote.Direction); await repo.Update(p); }
protected override async Task <VoteView> HandleInput(VoteOnCommentParams input) { using (var connection = database.GetConnection()) { IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); IUserRepo userRepo = database.GetRepo <IUserRepo>(connection); using (var transaction = connection.BeginTransaction()) { Comment comment = (await commentRepo.FindById(input.CommentId)) !; Vote? oldVote = await voteRepo.FindByUserAndComment(input.User.Username, input.CommentId); // Wipe out the old one... if (oldVote != null) { comment.RemoveVote(oldVote.Direction); await voteRepo.Delete(oldVote); comment.User.CommentKarma -= (int)oldVote.Direction; } // Create the new vote, and update the comment's karma cache. Vote newVote = new Vote() { User = input.User, ResourceType = VoteResourceType.Comment, ResourceId = input.CommentId, Direction = input.Vote }; comment.AddVote(newVote.Direction); comment.User.CommentKarma += (int)newVote.Direction; await voteRepo.Add(newVote); await commentRepo.Update(comment); await userRepo.Update(comment.User); transaction.Commit(); return(voteViewMapper.Map(newVote)); } } }
protected async override Task <CommentView?> HandleInput(FindByValueParams <int> input) { using (var connection = database.GetConnection()) { ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); Comment?c = await commentRepo.FindById(input.Value); if (c == null) { return(null); } if (input.User != null) { IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); await GetVotes(voteRepo, c, input.User); } return(commentMapper.Map(c)); } }
protected override async Task <CommentView> HandleInput(CommentUpdateParams input) { using (var connection = database.GetConnection()) { ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); Comment? comment = await commentRepo.FindById(input.CommentId); if (comment == null) { throw new NotFoundException(); } if (!(await this.commentPermissionHandler.HasPermission(input.User, PermissionAction.UpdateComment, comment))) { throw new AuthorizationException(); } comment.Body = input.Body; comment.WasUpdated = true; await commentRepo.Update(comment); return(commentMapper.Map(comment)); } }
public async Task <Comment?> FindById(int commentId) => await repo.FindById(commentId);