public override async Task <ReadBlogResponse> ReadBlog(ReadBlogRequest request, ServerCallContext context) { var blogId = request.BlogId; var filter = new FilterDefinitionBuilder <BsonDocument>().Eq("_id", new ObjectId(blogId)); var doc = (await mongoCollection.FindAsync(filter)).FirstOrDefault(); if (doc == null) { throw new RpcException(new Status(StatusCode.NotFound, $"Blog with id={blogId} was not found")); } return(new ReadBlogResponse { Blog = new Blog.Blog { Id = blogId, AuthorId = doc.GetValue("author_id").AsString, Title = doc.GetValue("title").AsString, Content = doc.GetValue("content").AsString } }); }
/// <summary> /// GetById Blog Method /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns></returns> public override async Task <ReadBlogResponse> ReadBlog(ReadBlogRequest request, ServerCallContext context) { var filter = new FilterDefinitionBuilder <BsonDocument>().Eq("_id", new ObjectId(request.BlogId)); var result = await mongoCollection.Find(filter).FirstOrDefaultAsync(); if (result == null) { throw new RpcException(new Status(StatusCode.NotFound, "The blog id " + request.BlogId + " was not find")); } Blog.Blog blog = new Blog.Blog() { AuthorId = result.GetValue("author_id").AsString, Title = result.GetValue("title").AsString, Content = result.GetValue("content").AsString }; return(new ReadBlogResponse() { Blog = blog }); }