create todos

master
Jason Staten 2 years ago
parent dd9493f28b
commit ad34ae002a

@ -43,20 +43,38 @@ type ViewModel struct {
}
func main() {
todos := make([]Todo, 0, 10)
t := New()
http.Handle("/public/", http.FileServer(http.FS(publicFS)))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
vm := ViewModel{
Todos: []Todo{
{Title: "One"},
{Title: "Two", Completed: true},
{Title: "Three"},
},
Todos: todos,
}
err := t.Render(w, "list.html", vm)
if err != nil {
log.Println(err)
}
})
http.HandleFunc("/todo", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
title := r.Form.Get("title")
todos = append(todos, Todo{
Title: title,
})
w.Header().Set("Location", "/")
w.WriteHeader(http.StatusSeeOther)
})
log.Print("Running on 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

@ -1,8 +1,7 @@
<li class="{{if .Completed}}completed{{end}}">
<div class="view">
<input class="toggle" type="checkbox" checked>
<input class="toggle" type="checkbox" {{if .Completed}}checked{{end}}>
<label>{{.Title}}</label>
<button class="destroy"></button>
</div>
<input class="edit" value="Create a TodoMVC template">
</li>

@ -2,7 +2,10 @@
<section class="todoapp">
<header class="header">
<h1>todos</h1>
<input class="new-todo" placeholder="What needs to be done?" autofocus>
<form action="/todo" method="POST">
<input name="title" class="new-todo" placeholder="What needs to be done?" autofocus>
<button style="display: none;">Submit</button>
</form>
</header>
<!-- This section should be hidden by default and shown when there are todos -->
<section class="main">

Loading…
Cancel
Save