From 331f3080f9d785b95f5135646826644d41c989d6 Mon Sep 17 00:00:00 2001 From: Bronku Date: Fri, 31 Jan 2025 19:39:17 +0100 Subject: [PATCH] new cake form --- controller.go | 5 ++- handlers.go | 45 ++++++++++++++++++- public/index.html | 2 +- templates/alert.html | 4 ++ templates/available_cakes.html | 82 ++++++++++++++++++---------------- templates/cake.html | 8 ++++ templates/cake_editor.html | 13 ++++++ templates/edit_cakes.html | 11 +++++ 8 files changed, 127 insertions(+), 43 deletions(-) create mode 100644 templates/alert.html create mode 100644 templates/cake.html create mode 100644 templates/cake_editor.html create mode 100644 templates/edit_cakes.html diff --git a/controller.go b/controller.go index 10f2954..651eb0d 100644 --- a/controller.go +++ b/controller.go @@ -16,7 +16,10 @@ func (c *controller) LoadRouter(pub fs.FS) { serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) serveMux.HandleFunc("POST /order/new", c.postNewOrder) - serveMux.HandleFunc("GET /cake/options", c.cakeOptions) + serveMux.HandleFunc("GET /available_cakes", c.cakeOptions) + serveMux.HandleFunc("GET /edit_cakes", c.cakeOptions) + serveMux.HandleFunc("GET /cake_editor", c.editCakes) + serveMux.HandleFunc("POST /new_cake", c.newCake) c.Handler = serveMux } diff --git a/handlers.go b/handlers.go index 89308d2..6040973 100644 --- a/handlers.go +++ b/handlers.go @@ -3,14 +3,55 @@ package main import ( "fmt" "net/http" + "strconv" + "strings" ) -func (c *controller) cakeOptions(w http.ResponseWriter, _ *http.Request) { +func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) { type cake struct { Name string ID int } - c.tmpl["available_cakes"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) + w.Header().Set("Content-Type", "text/html") + template := strings.Split(r.URL.String(), "/")[1] + c.tmpl[template].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) +} + +func (c *controller) editCakes(w http.ResponseWriter, r *http.Request) { + type cake struct { + Name string + ID int + Price int + } + w.Header().Set("Content-Type", "text/html") + c.tmpl["cake_editor"].Execute(w, cake{"Hello", 1, 120}) +} + +func (c *controller) newCake(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + c.tmpl["alert"].Execute(w, err) + return + } + + type cake struct { + Name string + ID int + Price int + } + var out cake + out.Name = r.FormValue("name") + out.Price, err = strconv.Atoi(r.FormValue("price")) + if err != nil { + c.tmpl["alert"].Execute(w, err) + return + } + out.ID, err = strconv.Atoi(r.FormValue("id")) + if err != nil { + c.tmpl["alert"].Execute(w, err) + return + } + _ = c.tmpl["cake"].Execute(w, out) } func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) { diff --git a/public/index.html b/public/index.html index 4e28837..617b667 100644 --- a/public/index.html +++ b/public/index.html @@ -46,7 +46,7 @@ -
+
diff --git a/templates/alert.html b/templates/alert.html new file mode 100644 index 0000000..82ac7a9 --- /dev/null +++ b/templates/alert.html @@ -0,0 +1,4 @@ +
+

{{.}}

+ +
\ No newline at end of file diff --git a/templates/available_cakes.html b/templates/available_cakes.html index e431830..e4c5c6d 100644 --- a/templates/available_cakes.html +++ b/templates/available_cakes.html @@ -1,40 +1,44 @@ -
- {{range .Cakes}} - - {{end}} -
- \ No newline at end of file + +
\ No newline at end of file diff --git a/templates/cake.html b/templates/cake.html new file mode 100644 index 0000000..5ae36be --- /dev/null +++ b/templates/cake.html @@ -0,0 +1,8 @@ +
+ + +
\ No newline at end of file diff --git a/templates/cake_editor.html b/templates/cake_editor.html new file mode 100644 index 0000000..f3070dc --- /dev/null +++ b/templates/cake_editor.html @@ -0,0 +1,13 @@ +
+ +
+ + +
+
+ + +
+ + +
\ No newline at end of file diff --git a/templates/edit_cakes.html b/templates/edit_cakes.html new file mode 100644 index 0000000..668c5b0 --- /dev/null +++ b/templates/edit_cakes.html @@ -0,0 +1,11 @@ +
+

Cakes

+ + +
+ {{range .Cakes}} + + {{end}} +
+
+
\ No newline at end of file