示例#1
0
文件: datastore.go 项目: liyu4/home
// PutListHelepr saves the list and it's items to the datastore. If
// the list or any of it's items don't have a key, a key will be made
// for it.
func PutListHelper(c appengine.Context, w http.ResponseWriter,
	r *http.Request, l *List) bool {

	// This is the list of keys we are going to PutMulti.
	keys := make([]string, 0, len(l.Items)+1)

	// This is the list of things we are going to put.
	values := make([]interface{}, 0, len(l.Items)+1)

	// We need the key to generate new keys and we might not even have
	// one.
	var lkey *datastore.Key
	var skey string
	var ok bool
	if l.Key != "" {
		// Just convert the key we already have.
		lkey, ok = gorca.StringToKey(c, w, r, l.Key)
		if !ok {
			return false
		}
	} else {
		// Make a key and save it's value to the list.
		skey, lkey, ok = gorca.NewKey(c, w, r, "List", nil)
		if !ok {
			return false
		}

		l.Key = skey
	}

	// Add the list itself.
	keys = append(keys, l.Key)
	values = append(values, l)

	// Add the items in the list.
	for _, item := range l.Items {
		if item.Key != "" {
			keys = append(keys, item.Key)
		} else {
			skey, _, ok := gorca.NewKey(c, w, r, "Item", lkey)
			if !ok {
				return false
			}

			item.Key = skey
			keys = append(keys, skey)
		}

		values = append(values, item)
	}

	// Save them all.
	return gorca.PutStringKeys(c, w, r, keys, values)
}
示例#2
0
文件: datastore.go 项目: icub3d/home
// PutLinkHelepr saves the link. If the link or any of it's items
// don't have a key, a key will be made for it.
func PutLinkHelper(c appengine.Context, w http.ResponseWriter,
	r *http.Request, l *Link) bool {

	// This is the link of keys we are going to PutMulti.
	keys := make([]string, 0, 1)

	// This is the link of things we are going to put.
	values := make([]interface{}, 0, 1)

	// TODO The words in the name should be part in the tags.

	// We need the key to generate new keys and we might not even have
	// one.
	var skey string
	var ok bool
	if l.Key == "" {
		// Make a key and save it's value to the link.
		skey, _, ok = gorca.NewKey(c, w, r, "Link", nil)
		if !ok {
			return false
		}

		l.Key = skey
	}

	// Add the link itself.
	keys = append(keys, l.Key)
	values = append(values, l)

	// Save them all.
	return gorca.PutStringKeys(c, w, r, keys, values)
}
示例#3
0
文件: datastore.go 项目: liyu4/home
// PutRecipeHelepr saves the recipe to the datastore. If the recipe
// doesn't have a key, a key will be made for it.
func PutRecipeHelper(c appengine.Context, w http.ResponseWriter,
	r *http.Request, l *Recipe) bool {

	// We may need to make a key.
	if l.Key == "" {
		// Make a key and save it's value to the recipe.
		skey, _, ok := gorca.NewKey(c, w, r, "Recipe", nil)
		if !ok {
			return false
		}

		l.Key = skey
	}

	// Save them all.
	return gorca.PutStringKeys(c, w, r, []string{l.Key}, []interface{}{l})
}