homeHandler, new page form and handler

This commit is contained in:
Ruidy Nemausat 2020-03-18 11:13:14 +01:00
parent 1082d81963
commit c184591bc9
6 changed files with 54 additions and 21 deletions

View file

@ -20,11 +20,10 @@ func (hh HomeHandler) home(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
views.Template(w, "home", struct{ Wikis []models.Page }{index}) 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 { if r.Method != http.MethodPost {
views.Template(w, "contact", nil) views.Template(w, "contact", nil)
return return

View file

@ -52,3 +52,9 @@ func (ph PageHandler) save(w http.ResponseWriter, r *http.Request, title string)
// checkError(err, w) // checkError(err, w)
http.Redirect(w, r, "/view/"+title, http.StatusFound) 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
}

View file

@ -11,7 +11,8 @@ func Router(ph PageHandler, hh HomeHandler) {
http.HandleFunc("/view/", makeHandler(ph.view)) http.HandleFunc("/view/", makeHandler(ph.view))
http.HandleFunc("/edit/", makeHandler(ph.edit)) http.HandleFunc("/edit/", makeHandler(ph.edit))
http.HandleFunc("/save/", makeHandler(ph.save)) http.HandleFunc("/save/", makeHandler(ph.save))
http.HandleFunc("/contact/", contactHandler) http.HandleFunc("/new/", ph.new)
http.HandleFunc("/contact/", hh.contact)
http.HandleFunc("/", hh.home) http.HandleFunc("/", hh.home)
} }

View file

@ -17,7 +17,7 @@ const (
// GetAllPages is the SQL command used to retrieve all pages from the database. // GetAllPages is the SQL command used to retrieve all pages from the database.
GetAllPages = ` 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 // FindPages is the SQL command used to retrieve all pages from the table

View file

@ -40,20 +40,22 @@
</div> </div>
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<form class="form-inline my-2 my-lg-0"> <form class="form-inline my-2 my-lg-0" action="/new">
<input <div class="form-group">
class="form-control mr-sm-2" <input
type="search" class="form-control mr-sm-2"
placeholder="Search" type="text"
aria-label="Search" name="newPage"
/> placeholder="New Wiki Entry"
<button />
class="btn btn-outline-success my-2 my-sm-0" </div>
type="submit" <div class="form-group">
action="/view/" <input
> type="submit"
Search value="New"
</button> class="btn btn-outline-primary my-2 my-sm-0"
/>
</div>
</form> </form>
</div> </div>
</div> </div>

View file

@ -14,9 +14,8 @@
>Documentation</a >Documentation</a
> >
</div> </div>
<div>
<h2>Index</h2> <!-- <div>
<p>Some description</p>
<ul> <ul>
{{ range $index, $entry := .Wikis }} {{ range $index, $entry := .Wikis }}
<li> <li>
@ -24,6 +23,32 @@
</li> </li>
{{ end }} {{ end }}
</ul> </ul>
</div> -->
<div>
<h2>Go-Wiki Index</h2>
<p>Some description</p>
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Title</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{{ range $index, $entry := .Wikis }}
<tr>
<td><a href="/view/{{ $entry.Title }}">{{ $entry.Title }}</a></td>
<th scope="row">
<a class="badge-pill badge-primary" href="/edit/{{ $entry.Title }}"
>Edit</a
>
</th>
</tr>
{{ end }}
</tbody>
</table>
</div> </div>
</div> </div>
{{end}} {{end}}