public async Task Update(int postId, PostUpdate update, User user) { Post?p = await repo.FindById(postId); if (p == null) { throw new NotFoundException($"No post with Id {postId} found."); } p.Update(update); await repo.Update(p); await bus.Dispatch(new PostUpdateEvent(p)); }
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 override async Task <PostView> HandleInput(PostUpdateParams input) { using (var connection = database.GetConnection()) { IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); Post? post = await postRepo.FindById(input.PostId); if (post == null) { throw new InvalidOperationException(); } if (!(await this.postPermissionHandler.HasPermission(input.User, PermissionAction.UpdatePost, post))) { throw new AuthorizationException(); } if (post.Type == PostType.Link) { throw new InvalidOperationException("Link posts can't be updated."); } if (post.WasDeleted) { throw new InvalidOperationException("Post has already been deleted."); } post.Body = input.Body; await postRepo.Update(post); return(postMapper.Map(post)); } }
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 override async Task <VoteView> HandleInput(VoteOnPostParams input) { using (var connection = database.GetConnection()) { IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); IUserRepo userRepo = database.GetRepo <IUserRepo>(connection); using (var transaction = connection.BeginTransaction()) { Post post = (await postRepo.FindById(input.PostId)) !; Vote?oldVote = await voteRepo.FindByUserAndPost(input.User.Username, input.PostId); // Wipe out the old one... if (oldVote != null) { post.RemoveVote(oldVote.Direction); await voteRepo.Delete(oldVote); if (post.Type != PostType.Text) { post.User.PostKarma -= (int)oldVote.Direction; } } // Create the new vote, and update the comment's karma cache. Vote newVote = new Vote() { User = input.User, ResourceType = VoteResourceType.Post, ResourceId = input.PostId, Direction = input.Vote }; post.AddVote(newVote.Direction); if (post.Type != PostType.Text) { post.User.PostKarma += (int)newVote.Direction; } await voteRepo.Add(newVote); await postRepo.Update(post); await userRepo.Update(post.User); transaction.Commit(); return(voteViewMapper.Map(newVote)); } } }
public async Task Handle(VoteOnPostEvent domainEvent) { Post?p = await repo.FindById(domainEvent.PostId); 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 async override Task <PostView?> HandleInput(FindByValueParams <int> input) { using (var connection = database.GetConnection()) { IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); Post?post = await postRepo.FindById(input.Value); if (post == null) { return(null); } //Pull in the vote if needed. if (input.User != null) { post.Vote = await voteRepo.FindByUserAndPost(input.User.Username, input.Value); } return(postMapper.Map(post)); } }
protected override async Task <PostView?> HandleInput(PostDeleteParams input) { using (var connection = database.GetConnection()) { IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); Post?p = await postRepo.FindById(input.PostId); if (p == null) { throw new InvalidOperationException(); } if (!(await this.permissionHandler.HasPermission(input.User, PermissionAction.DeletePost, p))) { throw new AuthorizationException(); } await postRepo.Delete(p); return(postMapper.Map(p)); } }