Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active September 20, 2015 07:26
Show Gist options
  • Select an option

  • Save IndianGuru/4071034854c4d69b2386 to your computer and use it in GitHub Desktop.

Select an option

Save IndianGuru/4071034854c4d69b2386 to your computer and use it in GitHub Desktop.
The Add service
// quoteKey returns the key used for all quote entries.
func quoteKey(c context.Context) *datastore.Key {
return datastore.NewKey(c, "Quote", "default_quote", 0, nil)
}
// Add creates a new quote given the fields in AddRequest, stores it in the
// datastore, and returns it.
func (QuotesAPI) Add(c context.Context, r *AddRequest) (*Quote, error) {
// We set the same parent key on every Quote entity to ensure each Quote
// is in the same entity group. Queries across the single entity group
// will be consistent.
k := datastore.NewIncompleteKey(c, "Quote", quoteKey(c))
t := &Quote{Author: r.Author, Message: r.Message}
k, err := datastore.Put(c, k, t)
if err != nil {
return nil, err
}
t.UID = k
return t, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment