diff --git a/README.md b/README.md index b2ba6b7..89ff00b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ # iroon +## todo: +- extract order creation form into it's own element +- option to add additional cakes +- saving orders on server +- displaying orders ## ux design ### adding an order: 1. user selects the _add order_ button diff --git a/controller.go b/controller.go new file mode 100644 index 0000000..bea2340 --- /dev/null +++ b/controller.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "html/template" + "io/fs" + "net/http" + "strings" +) + +type controller struct { + tmpl map[string]*template.Template + http.Handler +} + +func LoadTemplates(fs fs.ReadDirFS) map[string]*template.Template { + files, err := fs.ReadDir("templates") + if err != nil { + fmt.Println("can't read the templates directory") + return nil + } + tmpl := make(map[string]*template.Template) + for _, e := range files { + if e.IsDir() { + continue + } + name := e.Name() + name = strings.Split(name, ".")[0] + tmpl[name], err = template.ParseFS(fs, "templates/"+name+".html") + if err != nil { + fmt.Println("can't open template: " + name) + } + } + return tmpl +} + +func (c *controller) LoadRouter(pub fs.FS) { + serveMux := http.NewServeMux() + + serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) + serveMux.HandleFunc("POST /order/new", c.postNewOrder) + serveMux.HandleFunc("GET /cake/options", c.cakeOptions) + + c.Handler = serveMux +} diff --git a/controller/cake.go b/controller/cake.go deleted file mode 100644 index 96832f9..0000000 --- a/controller/cake.go +++ /dev/null @@ -1,14 +0,0 @@ -package controller - -import ( - "net/http" -) - -func (c *Controller) cakeOptions(w http.ResponseWriter, _ *http.Request) { - type cake struct { - Name string - ID int - } - //w.Header().Set("Content-Type", "text/html") - c.tmpl["cake/options"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) -} diff --git a/controller/controller.go b/controller/controller.go deleted file mode 100644 index 383ba42..0000000 --- a/controller/controller.go +++ /dev/null @@ -1,29 +0,0 @@ -package controller - -import ( - "html/template" - "io/fs" - "net/http" -) - -type Controller struct { - tmpl map[string]*template.Template - http.Handler -} - -// #todo: load all automatically -func (c *Controller) LoadTemplates(fs fs.FS) { - c.tmpl = make(map[string]*template.Template) - c.tmpl["order/confirmation"], _ = template.ParseFS(fs, "templates/order/confirmation.html") - c.tmpl["cake/options"], _ = template.ParseFS(fs, "templates/cake/options.html") -} - -func (c *Controller) LoadRouter(pub fs.FS) { - serveMux := http.NewServeMux() - - serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) - serveMux.HandleFunc("POST /order/new", c.postNewOrder) - serveMux.HandleFunc("GET /cake/options", c.cakeOptions) - - c.Handler = serveMux -} diff --git a/controller/new_order.go b/controller/new_order.go deleted file mode 100644 index dc73107..0000000 --- a/controller/new_order.go +++ /dev/null @@ -1,17 +0,0 @@ -package controller - -import ( - "fmt" - "net/http" -) - -func (c *Controller) postNewOrder(w http.ResponseWriter, r *http.Request) { - fmt.Println("received Post request: ", r) - err := r.ParseForm() - if err != nil { - _ = c.tmpl["order/confirmation"].Execute(w, struct{ Status any }{Status: err}) - } - w.WriteHeader(http.StatusAccepted) - fmt.Println(r.Form) - _ = c.tmpl["order/confirmation"].Execute(w, struct{ Status any }{Status: r.Form}) -} diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..89308d2 --- /dev/null +++ b/handlers.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "net/http" +) + +func (c *controller) cakeOptions(w http.ResponseWriter, _ *http.Request) { + type cake struct { + Name string + ID int + } + c.tmpl["available_cakes"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) +} + +func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + _ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: err}) + } + w.WriteHeader(http.StatusAccepted) + fmt.Println(r.Form) + _ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: r.Form}) +} diff --git a/main.go b/main.go index eb2e3d1..e5d122d 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "net/http" - "github.com/Bronku/iroon/controller" _ "github.com/mattn/go-sqlite3" ) @@ -17,8 +16,8 @@ var public embed.FS var templates embed.FS func main() { - var c controller.Controller - c.LoadTemplates(templates) + var c controller + c.tmpl = LoadTemplates(templates) c.LoadRouter(public) err := http.ListenAndServe(":8080", c) diff --git a/public/index.html b/public/index.html index 0d8fa4d..6d0b8a6 100644 --- a/public/index.html +++ b/public/index.html @@ -14,39 +14,44 @@