not really working
This commit is contained in:
parent
8081d61f2f
commit
56fff533f2
3 changed files with 53 additions and 14 deletions
|
|
@ -2,13 +2,13 @@
|
||||||
<h1>Kill me please</h1>
|
<h1>Kill me please</h1>
|
||||||
</header>
|
</header>
|
||||||
<nav>
|
<nav>
|
||||||
<a href="form">New</a>
|
<a href="order">New</a>
|
||||||
</nav>
|
</nav>
|
||||||
<main>
|
<main>
|
||||||
<ul>
|
<ul>
|
||||||
{{range .}}
|
{{range .}}
|
||||||
<li id="order[{{.ID}}]">
|
<li id="order[{{.ID}}]">
|
||||||
{{.Name}} {{.Surname}}
|
<a href="/order/{{.ID}}">{{.Name}} {{.Surname}}</a>
|
||||||
</li>
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
21
main.go
21
main.go
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
@ -36,13 +37,23 @@ type handler struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handler) form(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) form(w http.ResponseWriter, r *http.Request) {
|
||||||
data := order{
|
url := strings.Split(r.URL.String(), "/")
|
||||||
|
o := order{
|
||||||
ID: -1,
|
ID: -1,
|
||||||
Date: time.Now(),
|
Date: time.Now(),
|
||||||
Cakes: h.cakes,
|
|
||||||
}
|
}
|
||||||
|
id, err := strconv.Atoi(url[2])
|
||||||
|
if err == nil {
|
||||||
|
o = h.orders[id]
|
||||||
|
}
|
||||||
|
type formData struct {
|
||||||
|
Order order
|
||||||
|
Catalogue []cake
|
||||||
|
}
|
||||||
|
data := formData{o, h.cakes}
|
||||||
|
|
||||||
w.Header().Set("content-type", "text/html")
|
w.Header().Set("content-type", "text/html")
|
||||||
err := h.tmpl.ExecuteTemplate(w, "form.html", data)
|
err = h.tmpl.ExecuteTemplate(w, "order.html", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error executing the template: ", err)
|
fmt.Println("error executing the template: ", err)
|
||||||
}
|
}
|
||||||
|
|
@ -111,7 +122,7 @@ func logger(in http.HandlerFunc) http.HandlerFunc {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var h handler
|
var h handler
|
||||||
templates, err := template.ParseFiles("index.html", "form.html")
|
templates, err := template.ParseFiles("index.html", "order.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("can't parse templates: ", err)
|
log.Fatal("can't parse templates: ", err)
|
||||||
}
|
}
|
||||||
|
|
@ -129,7 +140,7 @@ func main() {
|
||||||
cakes = append(cakes, cake{"Malinowa chmurka", 1, 120, 0})
|
cakes = append(cakes, cake{"Malinowa chmurka", 1, 120, 0})
|
||||||
h.cakes = cakes
|
h.cakes = cakes
|
||||||
|
|
||||||
http.HandleFunc("GET /form", logger(h.form))
|
http.HandleFunc("GET /order/", logger(h.form))
|
||||||
http.HandleFunc("GET /", logger(h.index))
|
http.HandleFunc("GET /", logger(h.index))
|
||||||
http.HandleFunc("POST /", logger(h.addOrder))
|
http.HandleFunc("POST /", logger(h.addOrder))
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
</nav>
|
</nav>
|
||||||
<main>
|
<main>
|
||||||
<form method="post">
|
<form method="post">
|
||||||
|
{{with .Order}}
|
||||||
<div>
|
<div>
|
||||||
<h2>Info</h2>
|
<h2>Info</h2>
|
||||||
<input hidden="true" name="id" value="{{.ID}}">
|
<input hidden="true" name="id" value="{{.ID}}">
|
||||||
|
|
@ -27,11 +28,38 @@
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<h2>Basket</h2>
|
<h2>Basket</h2>
|
||||||
|
<ul>
|
||||||
{{range .Cakes}}
|
{{range .Cakes}}
|
||||||
|
<li>
|
||||||
<label>{{.Name}} {{.Price}}</label>
|
<label>{{.Name}} {{.Price}}</label>
|
||||||
<input type="number" min="0" name="cake[{{.ID}}]" value="{{.Amount}}">
|
<input id="cake[{{.ID}}]" type="number" min="0" name="cake[{{.ID}}]" value="{{.Amount}}">
|
||||||
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
{{end}}
|
||||||
|
{{with .Catalogue}}
|
||||||
|
<div>
|
||||||
|
<h2>Catalogue</h2>
|
||||||
|
<ul>
|
||||||
|
{{range .}}
|
||||||
|
<li>
|
||||||
|
{{.Name}} {{.Price}} <button type=button onClick="addCake({{.ID}})">add</button>
|
||||||
|
</li>
|
||||||
|
{{end}}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
<button type="submit">submit</button>
|
<button type="submit">submit</button>
|
||||||
</form>
|
</form>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function addCake(id){
|
||||||
|
var cake = document.getElementById("cake["+id+"]")
|
||||||
|
if (cake != null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log(cake)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue