why do I always overcomplicate things
This commit is contained in:
parent
9e2861ecba
commit
40fc76c7fc
3 changed files with 125 additions and 2 deletions
29
form.html
Normal file
29
form.html
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<header>
|
||||
<h1>New Order</h1>
|
||||
</header>
|
||||
<nav>
|
||||
<a href="/">back</a>
|
||||
</nav>
|
||||
<main>
|
||||
<form method="post">
|
||||
<div>
|
||||
<h2>Info</h2>
|
||||
<label>name</label>
|
||||
<input type="text" name="name" value="{{.Name}}">
|
||||
<label>surname</label>
|
||||
<input type="text" name="surname" value="{{.Surname}}">
|
||||
<label>phone</label>
|
||||
<input type="tel" name="phone" value="{{.Phone}}">
|
||||
<label>date</label>
|
||||
<input type="date" name="date" value="{{.Date}}">
|
||||
</div>
|
||||
<div>
|
||||
<h2>Basket</h2>
|
||||
{{range .Cakes}}
|
||||
<label>{{.Name}} {{.Price}}</label>
|
||||
<input type="number" min="0" name="cake[{{.ID}}]" value="{{.Amount}}">
|
||||
{{end}}
|
||||
</div>
|
||||
<button type="submit">submit</button>
|
||||
</form>
|
||||
</main>
|
||||
15
index.html
Normal file
15
index.html
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<header>
|
||||
<h1>Kill me please</h1>
|
||||
</header>
|
||||
<nav>
|
||||
<a href="form">New</a>
|
||||
</nav>
|
||||
<main>
|
||||
<ul>
|
||||
{{range .}}
|
||||
<li id="order[{{.ID}}]">
|
||||
{{.Name}} {{.Surname}}
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
</main>
|
||||
83
main.go
83
main.go
|
|
@ -2,10 +2,89 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("chuj")
|
||||
func form(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received a get request at: ", r.URL.String())
|
||||
tmpl, err := template.ParseFiles("form.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int
|
||||
Amount int
|
||||
}
|
||||
type tmplData struct {
|
||||
Name string
|
||||
Surname string
|
||||
Phone string
|
||||
Date string
|
||||
Cakes []cake
|
||||
}
|
||||
data := tmplData{
|
||||
Name: "",
|
||||
Surname: "",
|
||||
Phone: "",
|
||||
Date: "",
|
||||
Cakes: make([]cake, 0),
|
||||
}
|
||||
data.Cakes = append(data.Cakes, cake{"Hello", 0, 100, 0})
|
||||
data.Cakes = append(data.Cakes, cake{"World", 1, 120, 0})
|
||||
w.Header().Set("content-type", "text/html")
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
fmt.Println("error executing the template: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func addOrder(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received a post request at: ", r.URL.String())
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
fmt.Println("can't parse the form")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println("received form: ", r.Form)
|
||||
w.Header().Set("content-type", "text/html")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
w.Write([]byte("accepted <a href='/'>back</a>"))
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received a get request at: ", r.URL.String())
|
||||
tmpl, err := template.ParseFiles("index.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type tmplData struct {
|
||||
ID int
|
||||
Name string
|
||||
Surname string
|
||||
}
|
||||
data := make([]tmplData, 0)
|
||||
data = append(data, tmplData{0, "Jan", "Kowalski"})
|
||||
data = append(data, tmplData{1, "Jakub", "Bronk"})
|
||||
|
||||
w.Header().Set("content-type", "text/html")
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
fmt.Println("error executing the template: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("GET /form", form)
|
||||
http.HandleFunc("GET /", index)
|
||||
http.HandleFunc("POST /", addOrder)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue