diff options
author | xengineering <me@xengineering.eu> | 2024-01-08 19:27:47 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-01-08 19:33:24 +0100 |
commit | 43e7bb10eaade56a9444ec089aa3102218231868 (patch) | |
tree | 259e52b1978cfdf7ff979b0614c545d24d957c88 | |
parent | c6d2e9dadcd08cdf82aab507d9d4d8d58574df20 (diff) | |
download | ceres-43e7bb10eaade56a9444ec089aa3102218231868.tar ceres-43e7bb10eaade56a9444ec089aa3102218231868.tar.zst ceres-43e7bb10eaade56a9444ec089aa3102218231868.zip |
Implement recipe page
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | model/recipe.go | 35 | ||||
-rw-r--r-- | model/sql/recipe-select.sql | 2 | ||||
-rw-r--r-- | view/html/recipe.html | 20 |
4 files changed, 59 insertions, 0 deletions
@@ -44,6 +44,8 @@ func startServer(addr string) *http.Server { r.PathPrefix("/static/"). Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static)))) + r.HandleFunc("/recipes/{id}", view.HandlerHTML(&model.Recipe{})).Methods(`GET`) + r.HandleFunc("/", view.HandlerHTML(&model.Index{})).Methods(`GET`) muxer := http.NewServeMux() diff --git a/model/recipe.go b/model/recipe.go new file mode 100644 index 0000000..2b3953f --- /dev/null +++ b/model/recipe.go @@ -0,0 +1,35 @@ +package model + +type Recipe struct { + Id string // FIXME has to be uint + Title string + Portions string // FIXME has to be uint + URL string + Notes string +} + +func (d *Recipe) FromDB() error { + query, err := GetSql(`recipe-select`) + if err != nil { + return err + } + + return db.QueryRow(query, d.Id).Scan( + &d.Id, + &d.Title, + &d.Portions, + &d.URL, + &d.Notes, + ) +} + +func (d *Recipe) ToDB() error { + query, err := GetSql(`recipe-update`) + if err != nil { + return err + } + + _, err = db.Exec(query, d.Title, d.Portions, d.URL, d.Notes, d.Id) + + return err +} diff --git a/model/sql/recipe-select.sql b/model/sql/recipe-select.sql new file mode 100644 index 0000000..2d2bc60 --- /dev/null +++ b/model/sql/recipe-select.sql @@ -0,0 +1,2 @@ +SELECT id,title,portions,url,notes FROM recipes +WHERE id=?; diff --git a/view/html/recipe.html b/view/html/recipe.html new file mode 100644 index 0000000..acf346f --- /dev/null +++ b/view/html/recipe.html @@ -0,0 +1,20 @@ +{{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> + <a href="/recipes/{{.Id}}/edit"><button>edit</button></a> + </main> + {{ template "footer" }} + </body> +</html> +{{end}} |