From c184591bc98bb5360f1444819c62f5609a5724e1 Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Wed, 18 Mar 2020 11:13:14 +0100 Subject: [PATCH] homeHandler, new page form and handler --- controllers/home.go | 3 +-- controllers/page.go | 6 ++++++ controllers/router.go | 3 ++- data/pageQueries.go | 2 +- views/templates/base.html | 30 ++++++++++++++++-------------- views/templates/home.html | 31 ++++++++++++++++++++++++++++--- 6 files changed, 54 insertions(+), 21 deletions(-) diff --git a/controllers/home.go b/controllers/home.go index 7fbf9f7..f26902a 100644 --- a/controllers/home.go +++ b/controllers/home.go @@ -20,11 +20,10 @@ func (hh HomeHandler) home(w http.ResponseWriter, r *http.Request) { if err != nil { log.Fatal(err) } - views.Template(w, "home", struct{ Wikis []models.Page }{index}) } -func contactHandler(w http.ResponseWriter, r *http.Request) { +func (hh HomeHandler) contact(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { views.Template(w, "contact", nil) return diff --git a/controllers/page.go b/controllers/page.go index 94c9d9c..0199213 100644 --- a/controllers/page.go +++ b/controllers/page.go @@ -52,3 +52,9 @@ func (ph PageHandler) save(w http.ResponseWriter, r *http.Request, title string) // checkError(err, w) http.Redirect(w, r, "/view/"+title, http.StatusFound) } + +func (ph PageHandler) new(w http.ResponseWriter, r *http.Request) { + title := r.FormValue("newPage") + http.Redirect(w, r, "/edit/"+title, http.StatusFound) + return +} diff --git a/controllers/router.go b/controllers/router.go index c15ce81..d55edbf 100644 --- a/controllers/router.go +++ b/controllers/router.go @@ -11,7 +11,8 @@ func Router(ph PageHandler, hh HomeHandler) { http.HandleFunc("/view/", makeHandler(ph.view)) http.HandleFunc("/edit/", makeHandler(ph.edit)) http.HandleFunc("/save/", makeHandler(ph.save)) - http.HandleFunc("/contact/", contactHandler) + http.HandleFunc("/new/", ph.new) + http.HandleFunc("/contact/", hh.contact) http.HandleFunc("/", hh.home) } diff --git a/data/pageQueries.go b/data/pageQueries.go index e3f443c..31e5a9c 100644 --- a/data/pageQueries.go +++ b/data/pageQueries.go @@ -17,7 +17,7 @@ const ( // GetAllPages is the SQL command used to retrieve all pages from the database. GetAllPages = ` - SELECT * FROM pages ORDER BY id; + SELECT * FROM pages ORDER BY title; ` // FindPages is the SQL command used to retrieve all pages from the table diff --git a/views/templates/base.html b/views/templates/base.html index 8b1aabc..8f6734e 100644 --- a/views/templates/base.html +++ b/views/templates/base.html @@ -40,20 +40,22 @@ diff --git a/views/templates/home.html b/views/templates/home.html index 7094bfe..db5eacf 100644 --- a/views/templates/home.html +++ b/views/templates/home.html @@ -14,9 +14,8 @@ >Documentation -
-

Index

-

Some description

+ + + +
+

Go-Wiki Index

+

Some description

+ + + + + + + + + + {{ range $index, $entry := .Wikis }} + + + + + {{ end }} + +
Title
{{ $entry.Title }} + Edit +
{{end}}