Skip to content

Instantly share code, notes, and snippets.

@kyriediculous
Last active May 8, 2019 14:14
Show Gist options
  • Save kyriediculous/66eddc9c9e4a3841ec97235f4b3faca0 to your computer and use it in GitHub Desktop.
Save kyriediculous/66eddc9c9e4a3841ec97235f4b3faca0 to your computer and use it in GitHub Desktop.
func (s *BlogServiceServer) DeleteBlog(ctx context.Context, req *blogpb.DeleteBlogReq) (*blogpb.DeleteBlogRes, error) {
// Get the ID (string) from the request message and convert it to an Object ID
oid, err := primitive.ObjectIDFromHex(req.GetId())
// Check for errors
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("Could not convert to ObjectId: %v", err))
}
// DeleteOne returns DeleteResult which is a struct containing the amount of deleted docs (in this case only 1 always)
// So we return a boolean instead
_, err = blogdb.DeleteOne(ctx, bson.M{"_id": oid})
// Check for errors
if err != nil {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Could not find/delete blog with id %s: %v", req.GetId(), err))
}
// Return response with success: true if no error is thrown (and thus document is removed)
return &blogpb.DeleteBlogRes{
Success: true,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment