diff options
| author | xengineering <me@xengineering.eu> | 2024-02-12 20:33:24 +0100 | 
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2024-02-13 20:26:47 +0100 | 
| commit | bf0ad86df2e64191f6c430c328fee211ac3affa7 (patch) | |
| tree | 6bde15b19d89cc31c9ed6e42a4cbccfb6dd6c4c8 | |
| parent | 40868be7d4b9e1d9909a19dccc25ec49c1b5924b (diff) | |
| download | ceres-bf0ad86df2e64191f6c430c328fee211ac3affa7.tar ceres-bf0ad86df2e64191f6c430c328fee211ac3affa7.tar.zst ceres-bf0ad86df2e64191f6c430c328fee211ac3affa7.zip | |
view: Implement GET handler for model.Recipe
| -rw-r--r-- | main.go | 1 | ||||
| -rw-r--r-- | view/html/recipe.html | 19 | ||||
| -rw-r--r-- | view/recipe.go | 30 | 
3 files changed, 50 insertions, 0 deletions
| @@ -44,6 +44,7 @@ func startServer(addr string) *http.Server {  	r.PathPrefix("/static/").  		Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static))))  	r.HandleFunc("/recipes", view.Recipes).Methods(`GET`) +	r.HandleFunc("/recipe/{id:[0-9]+}", view.Recipe).Methods(`GET`)  	muxer := http.NewServeMux()  	muxer.Handle("/", r) diff --git a/view/html/recipe.html b/view/html/recipe.html new file mode 100644 index 0000000..30aecb5 --- /dev/null +++ b/view/html/recipe.html @@ -0,0 +1,19 @@ +{{define "recipe"}} +<html> +	{{ template "head" }} +	<header> +		<nav> +			<a href="/">HOME</a> +		</nav> +		<h1>{{.Title}}</h1> +	</header> +	<body> +		<main> +			<p>Portions: {{.Portions}}</p> +			<p><a href="{{.Url}}">original recipe</a></p> +			<p>{{.Notes}}</p> +		</main> +		{{ template "footer" }} +	</body> +</html> +{{end}} diff --git a/view/recipe.go b/view/recipe.go new file mode 100644 index 0000000..3739143 --- /dev/null +++ b/view/recipe.go @@ -0,0 +1,30 @@ +package view + +import ( +	"net/http" +	"strconv" + +	"xengineering.eu/ceres/model" + +	"github.com/gorilla/mux" +) + +func Recipe(w http.ResponseWriter, r *http.Request) { +	id_str := mux.Vars(r)[`id`] +	id, _ := strconv.Atoi(id_str) + +	recipe := model.Recipe{} +	recipe.Id = int64(id) + +	err := recipe.Read() +	if err != nil { +		http.Error(w, err.Error(), http.StatusInternalServerError) +		return +	} + +	err = html.ExecuteTemplate(w, "recipe", recipe) +	if err != nil { +		http.Error(w, err.Error(), http.StatusInternalServerError) +		return +	} +} | 
