diff options
| -rw-r--r-- | handler.go | 30 | ||||
| -rw-r--r-- | recipe.go | 13 | 
2 files changed, 21 insertions, 22 deletions
| @@ -15,18 +15,9 @@ const (  	VALID_ID_REGEX = `^[0-9]+$`  ) -type Recipe struct { -	Id    string -	Title string -	Text  string -	Html  string -} -  func indexGet(w http.ResponseWriter, r *http.Request) {  	list := getRecipeList() -  	sort.Sort(list) -  	ServeTemplate(w, "index.html", list)  } @@ -64,16 +55,17 @@ func recipeEditGet(w http.ResponseWriter, r *http.Request) {  	}  	idStr := ids[0] -	textpath := filepath.Join(config.Data, "recipes", idStr, "text") -	data, _ := ioutil.ReadFile(textpath) - -	recipe := Recipe{ -		idStr, -		"", -		string(data), -		"", +	text, err := getRecipeText(idStr) +	if err != nil { +		http.Error(w, "Could not get recipe.", 400) +		return  	} +	recipe := struct{ +		Id   string +		Text string +	}{idStr, string(text)} +  	ServeTemplate(w, "recipe_edit.html", recipe)  } @@ -113,9 +105,7 @@ func recipeConfirmDeletionGet(w http.ResponseWriter, r *http.Request) {  		return  	} -	recipe := Recipe{ids[0], "", "", ""} - -	ServeTemplate(w, "recipe_confirm_deletion.html", recipe) +	ServeTemplate(w, "recipe_confirm_deletion.html", struct{Id string}{ids[0]})  }  func recipeConfirmDeletionPost(w http.ResponseWriter, r *http.Request) { @@ -22,11 +22,20 @@ type recipe struct {  	}  } +func getRecipeText(id string) ([]byte, error) { +	var b []byte +	textpath := filepath.Join(config.Data, "recipes", id, "text") +	b, err := ioutil.ReadFile(textpath) +	if err != nil { +		return b, err +	} +	return b, nil +} +  func getRecipe(id string) (recipe, error) {  	r := recipe{} -	textpath := filepath.Join(config.Data, "recipes", id, "text") -	data, err := ioutil.ReadFile(textpath) +	data, err := getRecipeText(id)  	if err != nil {  		return r, err  	} | 
