func getStories(ctx context.Context) (events.APIGatewayProxyResponse, error) {
err := client.Ping(ctx, readpref.Primary())
if err != nil {
return handleError(err)
}
cursor, err := collection.Find(ctx, bson.D{})
defer cursor.Close(ctx)
if err != nil {
return handleError(err)
}
// stories is the array of stories that will get fetched
var stories []Story
for cursor.Next(ctx) {
// create a value into which the single document can be decoded
var story Story
err := cursor.Decode(&story)
if err != nil {
return handleError(err)
}
stories = append(stories, story)
}
return marshalJSONAndSend(stories)
}
view raw pt-6.main.go hosted with ❤ by GitHub