コード例 #1
0
ファイル: mod.go プロジェクト: liyu4/home
// PutList saves the list for the given tag.
func PutList(w http.ResponseWriter, r *http.Request) {
	// Get the context.
	c := appengine.NewContext(r)

	// Get the Key.
	vars := mux.Vars(r)
	key := vars["key"]

	datastore.RunInTransaction(c, func(c appengine.Context) error {

		// Get the original list.
		ol, ok := GetListHelper(c, w, r, key)
		if !ok {
			return fmt.Errorf("getting original list")
		}

		// Get the new list from the body.
		var nl List
		if !gorca.UnmarshalFromBodyOrFail(c, w, r, &nl) {
			return fmt.Errorf("unmarshalling")
		}

		// Merge the new list into the old list and remove deleted keys.
		delskeys := ol.Merge(&nl)
		if !gorca.DeleteStringKeys(c, w, r, delskeys) {
			return fmt.Errorf("deleting keys")
		}

		// Update the values.
		ol.LastModified = time.Now()
		ol.Name = nl.Name

		if !PutListHelper(c, w, r, ol) {
			return fmt.Errorf("putting list")
		}

		// Remove it from memcache
		memcache.Set(c, &memcache.Item{
			Key:   key,
			Value: []byte(fmt.Sprintf("%d", ol.LastModified.Unix())),
		})

		// Return the updated list back.
		gorca.WriteJSON(c, w, r, ol)

		return nil
	}, nil)

}
コード例 #2
0
ファイル: mod.go プロジェクト: icub3d/home
// PutLink saves the link for the given tag.
func PutLink(w http.ResponseWriter, r *http.Request) {
	// Get the context.
	c := appengine.NewContext(r)

	// Get the Key.
	vars := mux.Vars(r)
	key := vars["key"]

	datastore.RunInTransaction(c, func(c appengine.Context) error {

		// Get the original link.
		ol, ok := GetLinkHelper(c, w, r, key)
		if !ok {
			return fmt.Errorf("getting original link")
		}

		// Get the new link from the body.
		var nl Link
		if !gorca.UnmarshalFromBodyOrFail(c, w, r, &nl) {
			return fmt.Errorf("unmarshalling")
		}

		// Update the values.
		ol.Name = nl.Name
		ol.Icon = nl.Icon
		ol.Url = nl.Url
		ol.Description = nl.Description
		ol.Tags = nl.Tags

		if !PutLinkHelper(c, w, r, ol) {
			return fmt.Errorf("putting link")
		}

		// Return the updated link back.
		gorca.WriteJSON(c, w, r, ol)

		return nil
	}, nil)

}
コード例 #3
0
ファイル: mod.go プロジェクト: liyu4/home
// PutRecipe saves the recipe for the given tag.
func PutRecipe(w http.ResponseWriter, r *http.Request) {
	// Get the context.
	c := appengine.NewContext(r)

	// Get the Key.
	vars := mux.Vars(r)
	key := vars["key"]

	datastore.RunInTransaction(c, func(c appengine.Context) error {

		// Get the original recipe.
		or, ok := GetRecipeHelper(c, w, r, key)
		if !ok {
			return fmt.Errorf("getting original recipe")
		}

		// Get the new recipe from the body.
		nr := &Recipe{}
		if !gorca.UnmarshalFromBodyOrFail(c, w, r, nr) {
			return fmt.Errorf("unmarshalling")
		}

		// Update the values.
		nr.Key = or.Key
		nr.URL = or.URL
		nr.LastModified = time.Now()

		if !PutRecipeHelper(c, w, r, nr) {
			return fmt.Errorf("putting recipe")
		}

		// Return the updated recipe back.
		gorca.WriteJSON(c, w, r, nr)

		return nil
	}, nil)

}
コード例 #4
0
ファイル: mod.go プロジェクト: liyu4/home
// PostList creates a new list from the POSTed data.
func PostList(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	datastore.RunInTransaction(c, func(c appengine.Context) error {

		// Get the list from the body.
		var l List
		if !gorca.UnmarshalFromBodyOrFail(c, w, r, &l) {
			return fmt.Errorf("unmarshalling")
		}

		// Create a new list in the datastore.
		if !NewListHelper(c, w, r, &l) {
			return fmt.Errorf("generating new list")
		}

		// Return the updated list back.
		gorca.WriteJSON(c, w, r, l)

		return nil
	}, nil)

}
コード例 #5
0
ファイル: mod.go プロジェクト: liyu4/home
// PostRecipe creates a new recipe from the POSTed data. If a URL was
// given and the ingredients and diretions are blank, the information
// will be pulled from a URL if possible.
func PostRecipe(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	datastore.RunInTransaction(c, func(c appengine.Context) error {

		// Get the recipe from the body.
		var recipe Recipe
		if !gorca.UnmarshalFromBodyOrFail(c, w, r, &recipe) {
			return fmt.Errorf("unmarshalling")
		}

		// Create a new recipe in the datastore.
		if !NewRecipeHelper(c, w, r, &recipe) {
			return fmt.Errorf("generating new recipe")
		}

		// Return the updated recipe back.
		gorca.WriteJSON(c, w, r, recipe)

		return nil
	}, nil)

}