Example #1
0
File: get.go Project: liyu4/home
// GetList fetches the list for the given tag.
func GetList(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

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

	// Handle Not-modified
	ut := r.FormValue("date")
	if ut != "" {
		notmod(c, w, r, key, ut)
		return
	}

	l, ok := GetListHelper(c, w, r, key)
	if !ok {
		return
	}

	// Save the results to memcache.
	item := &memcache.Item{
		Key:   key,
		Value: []byte(fmt.Sprintf("%d", l.LastModified.Unix())),
	}
	if err := memcache.Set(c, item); err != nil {
		gorca.Log(c, r, "error", "failed to set memcache: %v", err)
	}

	// Write the lists as JSON.
	gorca.WriteJSON(c, w, r, l)
}
Example #2
0
File: mod.go Project: 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)

}
Example #3
0
File: get.go Project: icub3d/home
// GetLink fetches the link for the given tag.
func GetLink(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

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

	l, ok := GetLinkHelper(c, w, r, key)
	if !ok {
		return
	}

	// Write the links as JSON.
	gorca.WriteJSON(c, w, r, l)
}
Example #4
0
File: get.go Project: icub3d/home
// GetRecipe fetches the recipe for the given tag.
func GetRecipe(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

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

	recipe, ok := GetRecipeHelper(c, w, r, key)
	if !ok {
		return
	}

	// Write the recipes as JSON.
	gorca.WriteJSON(c, w, r, recipe)
}
Example #5
0
File: get.go Project: liyu4/home
// GetAllLists fetches all of the lists.
func GetAllLists(w http.ResponseWriter, r *http.Request) {
	// Create the query.
	c := appengine.NewContext(r)
	q := datastore.NewQuery("List").Order("-LastModified")

	// Fetch the lists.
	lists := []List{}
	if _, err := q.GetAll(c, &lists); err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return
	}

	// Write the lists as JSON.
	gorca.WriteJSON(c, w, r, lists)
}
Example #6
0
File: get.go Project: liyu4/home
// GetAllRecipes fetches all of the recipes.
func GetAllRecipes(w http.ResponseWriter, r *http.Request) {
	// Create the query.
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Recipe").Order("-LastModified")

	// Fetch the recipes.
	recipes := []Recipe{}
	if _, err := q.GetAll(c, &recipes); err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return
	}

	// We don't need the ingredients or directions
	for _, recipe := range recipes {
		recipe.Ingredients = []string{}
		recipe.Directions = []string{}
	}

	// Write the recipes as JSON.
	gorca.WriteJSON(c, w, r, recipes)
}
Example #7
0
File: mod.go Project: 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)

}
Example #8
0
File: get.go Project: icub3d/home
// GetAllLinks fetches all of the links.
func GetAllLinks(w http.ResponseWriter, r *http.Request) {
	// Create the query.
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Link")

	search := r.FormValue("search")
	if search != "" {
		q = q.Filter("Tags =", search)
	}

	q = q.Order("Name")

	// Fetch the links.
	links := []Link{}
	if _, err := q.GetAll(c, &links); err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return
	}

	// Write the links as JSON.
	gorca.WriteJSON(c, w, r, links)
}
Example #9
0
File: user.go Project: liyu4/home
// GetUser sends a JSON response with the current user information.
func GetUser(w http.ResponseWriter, r *http.Request) {
	// Get the context.
	c := appengine.NewContext(r)

	// Get the current user.
	u, ok := gorca.GetUserOrUnexpected(c, w, r)
	if !ok {
		return
	}

	// Get their logout URL.
	logout, ok := gorca.GetUserLogoutURL(c, w, r, "/")
	if !ok {
		return
	}

	// Write the user information out.
	gorca.WriteJSON(c, w, r, UserInfo{
		Email:     u.Email,
		LogoutURL: logout,
	})
}
Example #10
0
File: mod.go Project: 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)

}
Example #11
0
File: mod.go Project: 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)

}
Example #12
0
File: mod.go Project: 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)

}