organization

This commit is contained in:
Bronku 2025-01-29 11:13:59 +01:00
parent 8c3611e859
commit 1517b2eaf4
5 changed files with 38 additions and 30 deletions

View file

@ -1,7 +1,6 @@
package controller
import (
"fmt"
"html/template"
"io/fs"
"net/http"
@ -15,32 +14,16 @@ type Controller struct {
// #todo: load all automatically
func (c *Controller) LoadTemplates(fs fs.FS) {
c.tmpl = make(map[string]*template.Template)
c.tmpl["order/confirmation.html"], _ = template.ParseFS(fs, "templates/order/confirmation.html")
c.tmpl["new_order.html"], _ = template.ParseFS(fs, "templates/new_order.html")
c.tmpl["order/post_new"], _ = template.ParseFS(fs, "templates/order/post_new.html")
c.tmpl["order/get_new"], _ = template.ParseFS(fs, "templates/order/get_new.html")
}
func (c *Controller) LoadRouter(pub fs.FS) {
serveMux := http.NewServeMux()
serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public"))))
serveMux.HandleFunc("POST /", c.HandlePost)
serveMux.HandleFunc("GET /new_order.html", c.HandleGet)
serveMux.HandleFunc("POST /order/new", c.postNewOrder)
serveMux.HandleFunc("GET /order/new", c.getNewOrder)
c.Handler = serveMux
}
func (c *Controller) HandlePost(w http.ResponseWriter, r *http.Request) {
fmt.Println("received Post request: ", r)
err := r.ParseForm()
if err != nil {
_ = c.tmpl["order/confirmation.html"].Execute(w, struct{ Status any }{Status: err})
}
w.WriteHeader(http.StatusAccepted)
fmt.Println(r.Form)
_ = c.tmpl["order/confirmation.html"].Execute(w, struct{ Status any }{Status: r.Form})
}
func (c *Controller) HandleGet(w http.ResponseWriter, r *http.Request) {
fmt.Println("received Get request: ", r)
_ = c.tmpl["new_order.html"].Execute(w, nil)
}

22
controller/new_order.go Normal file
View file

@ -0,0 +1,22 @@
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/post_new"].Execute(w, struct{ Status any }{Status: err})
}
w.WriteHeader(http.StatusAccepted)
fmt.Println(r.Form)
_ = c.tmpl["order/post_new"].Execute(w, struct{ Status any }{Status: r.Form})
}
func (c *Controller) getNewOrder(w http.ResponseWriter, r *http.Request) {
fmt.Println("received Get request: ", r)
_ = c.tmpl["order/get_new"].Execute(w, nil)
}