diff --git a/main.go b/main.go index 0f95903..0b64c42 100644 --- a/main.go +++ b/main.go @@ -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)) } diff --git a/views/_task.html b/views/_task.html index d778487..8827738 100644 --- a/views/_task.html +++ b/views/_task.html @@ -1,8 +1,7 @@
  • - +
    -
  • diff --git a/views/list.html b/views/list.html index 44c4f88..8d072ca 100644 --- a/views/list.html +++ b/views/list.html @@ -2,7 +2,10 @@

    todos

    - +
    + + +