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 override async Task <PostView?> HandleInput(PostCreateParams input) { using (var connection = database.GetConnection()) { ISpaceRepo spaceRepo = database.GetRepo <ISpaceRepo>(connection); IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); Space?space = await spaceRepo.FindByName(input.Space); if (space == null) { throw new InvalidOperationException($"No space with name ${input.Space} found."); } using (var transaction = connection.BeginTransaction()) { Post post = new Post() { Type = input.Type, Title = input.Title, Body = input.Body, User = input.User, CreationDate = DateTime.UtcNow, Space = space }; if (post.Type == PostType.Link && !System.Text.RegularExpressions.Regex.IsMatch(post.Body, Regex.UrlProtocol)) { post.Body = $"http://{post.Body}"; } // Not liking these count caches. Makes no sense? post.Upvotes++; await postRepo.Add(post); Vote upvote = new Vote() { User = input.User, ResourceId = post.Id, ResourceType = VoteResourceType.Post, Direction = VoteDirection.Up }; await voteRepo.Add(upvote); post.Vote = upvote; transaction.Commit(); return(postMapper.Map(post)); } } }
protected override async Task <PagedResultSet <PostView> > HandleInput(FindByValueParams <string> input) { using (var connection = database.GetConnection()) { IPostRepo postRepo = database.GetRepo <IPostRepo>(connection); IVoteRepo voteRepo = database.GetRepo <IVoteRepo>(connection); PagedResultSet <Post> posts = await postRepo.FindByUser(input.Value, input.Pagination?.PageNumber ?? 0, input.Pagination?.PageSize ?? Post.PageSize); if (input.User != null) { foreach (Post p in posts) { p.Vote = await voteRepo.FindByUserAndPost(input.User.Username, p.Id); } } return(new PagedResultSet <PostView>(posts.Items.Select(p => postMapper.Map(p)), posts.Pagination)); } }
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)); } }