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)); } } }
protected async override Task <IEnumerable <CommentView> > HandleInput(FindByValueParams <int> input) { using (var connection = database.GetConnection()) { ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); IEnumerable <Comment> comments = await commentRepo.FindByPost(input.Value); if (input.User != null) { IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); foreach (Comment c in comments) { await GetVotes(voteRepo, c, input.User); } } return(comments.Select(c => commentMapper.Map(c))); } }
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 async override Task <PagedResultSet <CommentView> > HandleInput(FindByValueParams <string> input) { using (var connection = database.GetConnection()) { ICommentRepo commentRepo = database.GetRepo <ICommentRepo>(connection); PagedResultSet <Comment> comments = await commentRepo.FindByUser(input.Value, input.Pagination?.PageNumber ?? 0, input.Pagination?.PageSize ?? Comment.PageSize); if (input.User != null) { foreach (Comment c in comments) { IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); await GetVotes(voteRepo, c, input.User); } } return(new PagedResultSet <CommentView>( comments.Select(c => commentMapper.Map(c)), comments.Pagination )); } }
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)); } }