From 1931b047fce7b7c55697ea4e1cd603350b931083 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Thu, 25 Jan 2024 22:31:27 -0800 Subject: [PATCH] lets-go:8.2 create form --- snippetbox/cmd/web/handlers.go | 21 +++++++++++++++++---- snippetbox/cmd/web/routes.go | 8 ++++++++ snippetbox/ui/html/pages/create.tmpl | 23 +++++++++++++++++++++++ snippetbox/ui/html/partials/nav.tmpl | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 snippetbox/ui/html/pages/create.tmpl diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index 5dcbeeb..6ce05bb 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -52,14 +52,27 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) { // (app *application) snippetCreate ... func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Form for new snippet, plz?")) + data := app.newTemplateData(r) + + app.render(w, r, http.StatusOK, "create.tmpl", data) } // snippetCreatePost ... func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) { - title := "0 snail" - content := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n - Kobayashi Issa" - expires := 7 + err := r.ParseForm() + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } + + title := r.PostForm.Get("title") + content := r.PostForm.Get("content") + + expires, err := strconv.Atoi(r.PostForm.Get("expires")) + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } id, err := app.snippets.Insert(title, content, expires) if err != nil { diff --git a/snippetbox/cmd/web/routes.go b/snippetbox/cmd/web/routes.go index 961b99b..16c0b3b 100644 --- a/snippetbox/cmd/web/routes.go +++ b/snippetbox/cmd/web/routes.go @@ -11,6 +11,14 @@ import ( func (app *application) routes() http.Handler { router := httprouter.New() + router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + app.notFound(w) + }) + + router.MethodNotAllowed = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + app.clientError(w, http.StatusMethodNotAllowed) + }) + // setup server for static files fileServer := http.FileServer(http.Dir("./ui/static")) router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer)) diff --git a/snippetbox/ui/html/pages/create.tmpl b/snippetbox/ui/html/pages/create.tmpl new file mode 100644 index 0000000..3e19bc3 --- /dev/null +++ b/snippetbox/ui/html/pages/create.tmpl @@ -0,0 +1,23 @@ +{{define "title"}}Create a New Snippet{{end}} + +{{define "main"}} +
+
+ + +
+
+ + +
+
+ + One Year + One Week + One Day +
+
+ +
+
+{{end}} diff --git a/snippetbox/ui/html/partials/nav.tmpl b/snippetbox/ui/html/partials/nav.tmpl index f815ef4..d9fbcd8 100644 --- a/snippetbox/ui/html/partials/nav.tmpl +++ b/snippetbox/ui/html/partials/nav.tmpl @@ -1,5 +1,6 @@ {{define "nav"}} {{end}} \ No newline at end of file