public PostResponse AddPost(Post post) { return(singltonDataContextService.Execute(c => { var addedPost = c.Posts.Add(post); c.SaveChanges(); RaiseEvent(new Observable <Post>(EventType.Added, addedPost.Entity)); return PostResponse.Success(addedPost.Entity); })); }
public PostResponse DeletePost(int postId) { return(singltonDataContextService.Execute(c => { var existingPost = c.Posts.FirstOrDefault(p => p.PostId == postId); if (existingPost == null) { return PostResponse.Failure(PostStatus.PostDoesNotExist); } c.Posts.Remove(existingPost); c.SaveChanges(); RaiseEvent(new Observable <Post>(EventType.Deleted, existingPost)); return PostResponse.Success(); })); }
public async Task <PostResponse> UpdatePost(int postId, Post updatedPost) { return(await singltonDataContextService.Execute(async c => { var existingPost = c.Posts.FirstOrDefault(p => p.PostId == postId); if (existingPost == null) { return await Task.FromResult(PostResponse.Failure(PostStatus.PostDoesNotExist)); } var update = new Update <Post> { PropName = nameof(Post.Text), OldValue = existingPost.Clone() as Post }; existingPost.Text = updatedPost.Text; await c.SaveChangesAsync(); RaiseEvent(new Observable <Post>(EventType.Added, existingPost, update)); return PostResponse.Success(); })); }