parsing metadata

This commit is contained in:
bronku 2025-02-06 20:26:34 +01:00
parent bc4c9d62f0
commit 8081d61f2f
2 changed files with 38 additions and 7 deletions

View file

@ -8,6 +8,7 @@
<form method="post">
<div>
<h2>Info</h2>
<input hidden="true" name="id" value="{{.ID}}">
<label>name</label>
<input type="text" name="name" value="{{.Name}}">
<label>surname</label>
@ -20,7 +21,9 @@
<option value="Somonino">Somonino</option>
</select>
<label>date</label>
<input type="date" name="date" value="{{.Date}}">
<input type="date" name="date" value="{{.Date.Format "2006-01-02"}}">
<label>paid</label>
<input type="number" name="paid" min="0" value="{{.Paid}}">
</div>
<div>
<h2>Basket</h2>

36
main.go
View file

@ -5,6 +5,7 @@ import (
"html/template"
"log"
"net/http"
"strconv"
"time"
_ "github.com/mattn/go-sqlite3"
@ -36,10 +37,7 @@ type handler struct {
func (h *handler) form(w http.ResponseWriter, r *http.Request) {
data := order{
Name: "",
Surname: "",
Phone: "",
Location: "",
ID: -1,
Date: time.Now(),
Cakes: h.cakes,
}
@ -58,6 +56,36 @@ func (h *handler) addOrder(w http.ResponseWriter, r *http.Request) {
return
}
fmt.Println("received form: ", r.Form)
var n order
n.ID, err = strconv.Atoi(r.FormValue("id"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order id: ", err)
return
}
n.Name = r.FormValue("name")
n.Surname = r.FormValue("surname")
n.Phone = r.FormValue("phone")
n.Location = r.FormValue("location")
n.Date, err = time.Parse("2006-01-02", r.FormValue("date"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order date: ", err)
return
}
n.Paid, err = strconv.Atoi(r.FormValue("paid"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println("can't parse order paid: ", err)
return
}
n.Cakes = make([]cake, 0)
h.orders = append(h.orders, n)
fmt.Println("parsed order: ", n)
w.Header().Set("content-type", "text/html")
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("accepted <a href='/'>back</a>"))