xd
This commit is contained in:
parent
9208e62f1a
commit
9e2861ecba
14 changed files with 1 additions and 311 deletions
|
|
@ -1,35 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Cake struct {
|
|
||||||
Name string
|
|
||||||
Price int
|
|
||||||
ID int
|
|
||||||
}
|
|
||||||
|
|
||||||
type controller struct {
|
|
||||||
tmpl map[string]*template.Template
|
|
||||||
cakes map[int]Cake
|
|
||||||
http.Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controller) LoadRouter(pub fs.FS) {
|
|
||||||
serveMux := http.NewServeMux()
|
|
||||||
|
|
||||||
//temporary, change in dev
|
|
||||||
//serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public"))))
|
|
||||||
serveMux.Handle("GET /", http.FileServerFS(pub))
|
|
||||||
serveMux.HandleFunc("POST /order/new", c.postNewOrder)
|
|
||||||
serveMux.HandleFunc("GET /available_cakes", c.cakeOptions)
|
|
||||||
serveMux.HandleFunc("GET /edit_cakes", c.cakeOptions)
|
|
||||||
serveMux.HandleFunc("GET /cake_editor", c.cakeEditor)
|
|
||||||
serveMux.HandleFunc("GET /cake_editor/{ID}", c.cakeEditor)
|
|
||||||
serveMux.HandleFunc("POST /new_cake", c.newCake)
|
|
||||||
|
|
||||||
c.Handler = serveMux
|
|
||||||
}
|
|
||||||
74
handlers.go
74
handlers.go
|
|
@ -1,74 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
|
||||||
template := strings.Split(r.URL.String(), "/")[1]
|
|
||||||
c.tmpl[template].Execute(w, c.cakes)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controller) cakeEditor(w http.ResponseWriter, r *http.Request) {
|
|
||||||
url := strings.Split(r.URL.String(), "/")
|
|
||||||
if len(url) < 3 {
|
|
||||||
c.tmpl["cake_editor"].Execute(w, Cake{"", 0, -1})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(url[2])
|
|
||||||
if err != nil {
|
|
||||||
c.tmpl["cake_editor"].Execute(w, Cake{"", 0, -1})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cake, exists := c.cakes[id]
|
|
||||||
if !exists {
|
|
||||||
c.tmpl["cake_editor"].Execute(w, Cake{"", 0, -1})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.tmpl["cake_editor"].Execute(w, cake)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
|
|
||||||
err := r.ParseForm()
|
|
||||||
if err != nil {
|
|
||||||
c.tmpl["alert"].Execute(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var in Cake
|
|
||||||
in.Name = r.FormValue("name")
|
|
||||||
in.Price, err = strconv.Atoi(r.FormValue("price"))
|
|
||||||
if err != nil {
|
|
||||||
c.tmpl["alert"].Execute(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
in.ID, err = strconv.Atoi(r.FormValue("id"))
|
|
||||||
if err != nil {
|
|
||||||
c.tmpl["alert"].Execute(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if in.ID == -1 {
|
|
||||||
in.ID = len(c.cakes)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.cakes[in.ID] = in
|
|
||||||
_ = c.tmpl["new_cake"].Execute(w, in)
|
|
||||||
}
|
|
||||||
|
|
||||||
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})
|
|
||||||
}
|
|
||||||
23
main.go
23
main.go
|
|
@ -1,32 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed public
|
|
||||||
//var public embed.FS
|
|
||||||
|
|
||||||
//go:embed templates
|
|
||||||
var templates embed.FS
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var c controller
|
fmt.Println("chuj")
|
||||||
c.tmpl = LoadTemplates(templates)
|
|
||||||
publicfs := os.DirFS("public")
|
|
||||||
c.LoadRouter(publicfs)
|
|
||||||
c.cakes = make(map[int]Cake)
|
|
||||||
c.cakes[0] = Cake{"Sernik", 120, 0}
|
|
||||||
c.cakes[1] = Cake{"Malinowa chmurka", 120, 1}
|
|
||||||
c.cakes[2] = Cake{"Beza Pavlova", 8, 2}
|
|
||||||
|
|
||||||
err := http.ListenAndServe(":8080", c)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Can't start the server: ", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -1,21 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<title>IROON</title>
|
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
||||||
<script src="htmx-2.04.js"></script>
|
|
||||||
<link rel="stylesheet" href="style.css">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<header>
|
|
||||||
<h1>I ran out of names</h1>
|
|
||||||
</header>
|
|
||||||
<main>
|
|
||||||
<div hx-get="new_order_form.html" hx-swap="outerHTML" hx-trigger="load"></div>
|
|
||||||
</main>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
<form x-show="open" hx-post="order/new" hx-swap="outerHTML" id="new_order_form">
|
|
||||||
<div id="main_form">
|
|
||||||
<h3>New Order</h3>
|
|
||||||
<div>
|
|
||||||
<label for="firstname">First Name</label>
|
|
||||||
<input type="text" id="firstname" name="firstname" required value="">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label for="lastname">Last Name</label>
|
|
||||||
<input type="text" id="lastname" name="lastname" value="">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label for="phone">Phone</label>
|
|
||||||
<input type="tel" id="phone" name="phone" value="">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label for="location">Location</label>
|
|
||||||
<input type="text" id="location" name="location" value="">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label for="paid">Paid</label>
|
|
||||||
<input type="number" id="paid" name="paid" step="0.01" min="0" value="">
|
|
||||||
</div>
|
|
||||||
<button id="submit_button" type="submit">Submit</button>
|
|
||||||
<button onclick="document.getElementById('basket').innerHTML=''" type="reset">Reset</button>
|
|
||||||
</div>
|
|
||||||
<div id="basket">
|
|
||||||
<h3>Baset</h3>
|
|
||||||
</div>
|
|
||||||
<div id="cake_selector" hx-get="available_cakes" hx-swap="innerHTML" hx-trigger="load"></div>
|
|
||||||
</form>
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
#new_order_form {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: row;
|
|
||||||
padding: 10pt
|
|
||||||
}
|
|
||||||
|
|
||||||
#new_order_form>div {
|
|
||||||
padding: 10pt;
|
|
||||||
width: 400px;
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
<div id="alert">
|
|
||||||
<h3>{{.}}</h3>
|
|
||||||
<button type="button" onClick="document.getElementById('alert').remove()">close</button>
|
|
||||||
</div>
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
<h3>Add cakes to order</h3>
|
|
||||||
<button hx-get="edit_cakes" hx-swap="innerHTML" hx-target="closest div">edit</button>
|
|
||||||
<div id="cakes">
|
|
||||||
{{range $key, $value := .}}
|
|
||||||
<button onclick="addCake('{{$value.ID}}', '{{$value.Name}} ({{$value.Price}})')" type="button">{{$value.Name}}
|
|
||||||
{{$value.Price}}</button>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
<script>
|
|
||||||
function addCake(id, name) {
|
|
||||||
const existing = document.getElementById("cake[" + id + "]")
|
|
||||||
if (existing != null) {
|
|
||||||
existing.value++
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const label = document.createElement('label')
|
|
||||||
label.innerText = name
|
|
||||||
label.setAttribute("for", "cake[" + id + "]")
|
|
||||||
|
|
||||||
const input = document.createElement('input')
|
|
||||||
input.setAttribute("type", "number")
|
|
||||||
input.setAttribute("id", "cake[" + id + "]")
|
|
||||||
input.setAttribute("name", "cake[" + id + "]")
|
|
||||||
input.setAttribute("value", 1)
|
|
||||||
|
|
||||||
const selected = document.createElement('div')
|
|
||||||
selected.appendChild(label)
|
|
||||||
selected.appendChild(input)
|
|
||||||
|
|
||||||
const deleteButton = document.createElement('button')
|
|
||||||
deleteButton.setAttribute("type", "button")
|
|
||||||
deleteButton.innerText = "delete"
|
|
||||||
deleteButton.addEventListener("click", function () {
|
|
||||||
selected.remove()
|
|
||||||
})
|
|
||||||
|
|
||||||
selected.appendChild(deleteButton)
|
|
||||||
|
|
||||||
const parent = document.getElementById("basket")
|
|
||||||
parent.appendChild(selected)
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
<h3>Edit cakes</h3>
|
|
||||||
<form hx-post="new_cake" hx-swap="outerHTML">
|
|
||||||
<div>
|
|
||||||
<label for="name">Name</label>
|
|
||||||
<input type="text" id="name" name="name" required value="{{.Name}}">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label for="price">Price</label>
|
|
||||||
<input type="number" id="price" name="price" step="0.01" min="0" required value="{{.Price}}">
|
|
||||||
</div>
|
|
||||||
<input value="{{.ID}}" name="id" hidden></input>
|
|
||||||
<button type="submit">Submit</button>
|
|
||||||
<button hx-get="edit_cakes" hx-swap="innerHTML" hx-target="closest div">back</button>
|
|
||||||
</form>
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
<div>
|
|
||||||
<h3>{{.Status}}</h3>
|
|
||||||
<button hx-get="new_order_form.html" hx-swap="outerHTML" hx-target="closest div">Close</button>
|
|
||||||
</div>
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
<h3>Edit cakes</h3>
|
|
||||||
<button hx-get="available_cakes" hx-swap="innerHTML" hx-target="closest div">back</button>
|
|
||||||
<button hx-get="cake_editor" hx-swap="innerHTML" hx-target="closest #cake_selector">New</button>
|
|
||||||
<div id="cakes">
|
|
||||||
{{range $key, $value := .}}
|
|
||||||
<button hx-get="cake_editor/{{$value.ID}}" hx-swap="innerHTML" hx-target="closest #cake_selector">{{$value.Name}}
|
|
||||||
{{$value.Price}}</button>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
<h3>Added cake</h3>
|
|
||||||
<ul>
|
|
||||||
<li>ID: {{.ID}}</li>
|
|
||||||
<li>Name: {{.Name}}</li>
|
|
||||||
<li>Price: {{.Price}}</li>
|
|
||||||
</ul>
|
|
||||||
<button hx-get="edit_cakes" hx-swap="innerHTML" hx-target="closest div">back</button>
|
|
||||||
36
util.go
36
util.go
|
|
@ -1,36 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"io/fs"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func _[T any](output T, err error) T {
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue