Created
May 8, 2019 14:21
-
-
Save kyriediculous/f499c7ebb9d12bafcef0a1f3a702565f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *BlogServiceServer) UpdateBlog(ctx context.Context, req *blogpb.UpdateBlogReq) (*blogpb.UpdateBlogRes, error) { | |
// Get the blog data from the request | |
blog := req.GetBlog() | |
// Convert the Id string to a MongoDB ObjectId | |
oid, err := primitive.ObjectIDFromHex(blog.GetId()) | |
if err != nil { | |
return nil, status.Errorf( | |
codes.InvalidArgument, | |
fmt.Sprintf("Could not convert the supplied blog id to a MongoDB ObjectId: %v", err), | |
) | |
} | |
// Convert the data to be updated into an unordered Bson document | |
update := bson.M{ | |
"authord_id": blog.GetAuthorId(), | |
"title": blog.GetTitle(), | |
"content": blog.GetContent(), | |
} | |
// Convert the oid into an unordered bson document to search by id | |
filter := bson.M{"_id": oid} | |
// Result is the BSON encoded result | |
// To return the updated document instead of original we have to add options. | |
result := blogdb.FindOneAndUpdate(ctx, filter, bson.M{"$set": update}, options.FindOneAndUpdate().SetReturnDocument(1)) | |
// Decode result and write it to 'decoded' | |
decoded := BlogItem{} | |
err = result.Decode(&decoded) | |
if err != nil { | |
return nil, status.Errorf( | |
codes.NotFound, | |
fmt.Sprintf("Could not find blog with supplied ID: %v", err), | |
) | |
} | |
return &blogpb.UpdateBlogRes{ | |
Blog: &blogpb.Blog{ | |
Id: decoded.ID.Hex(), | |
AuthorId: decoded.AuthorID, | |
Title: decoded.Title, | |
Content: decoded.Content, | |
}, | |
}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment