Exemple #1
0
// updateRecipeFromURL is a helper function that attempts to parse the
// recipe URL to get the recipe data. If an error occurs, false is
// returned and a proper message will have been sent as a
// response. This case should be terminal. If a parser isn't available
// for the URL, no error is returned, but nothing is changed in the
// recipe.
func updateRecipeFromURL(c appengine.Context, w http.ResponseWriter,
	r *http.Request, recipe *Recipe) bool {

	p, err := parsers.GetParserForURL(recipe.URL)
	if err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return false
	}

	if p == nil {
		gorca.Log(c, r, "warn", "no parser found for: %s", recipe.URL)
		return true
	}

	client := urlfetch.Client(c)
	resp, err := client.Get(recipe.URL)
	if err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return false
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return false
	}

	recipe.Name = p.GetName(body)
	recipe.Ingredients = p.GetIngredients(body)
	recipe.Directions = p.GetDirections(body)

	return true
}
Exemple #2
0
// GetListHelper is a helper function that retrieves a list and it's
// items from the datastore. If a failure occured, false is returned
// and a response was returned to the request. This case should be
// terminal.
func GetListHelper(c appengine.Context, w http.ResponseWriter,
	r *http.Request, key string) (*List, bool) {

	// Decode the string version of the key.
	k, err := datastore.DecodeKey(key)
	if err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return nil, false
	}

	// Get the list by key.
	var l List
	if err := datastore.Get(c, k, &l); err != nil {
		gorca.LogAndNotFound(c, w, r, err)
		return nil, false
	}

	// Get all of the items for the list.
	var li ItemsList
	q := datastore.NewQuery("Item").Ancestor(k).Order("Order")
	if _, err := q.GetAll(c, &li); err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return nil, false
	}

	l.Items = li

	return &l, true
}
Exemple #3
0
// 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)
}
Exemple #4
0
// GetRecipeHelper is a helper function that retrieves a recipe and it's
// items from the datastore. If a failure occured, false is returned
// and a response was returned to the request. This case should be
// terminal.
func GetRecipeHelper(c appengine.Context, w http.ResponseWriter,
	r *http.Request, key string) (*Recipe, bool) {

	// Decode the string version of the key.
	k, err := datastore.DecodeKey(key)
	if err != nil {
		gorca.LogAndUnexpected(c, w, r, err)
		return nil, false
	}

	// Get the recipe by key.
	var recipe Recipe
	if err := datastore.Get(c, k, &recipe); err != nil {
		gorca.LogAndNotFound(c, w, r, err)
		return nil, false
	}

	return &recipe, true
}
Exemple #5
0
// 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)
}
Exemple #6
0
// 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)
}