lets-go:8.2 create form
This commit is contained in:
parent
d1b9b2b269
commit
1931b047fc
@ -52,14 +52,27 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// (app *application) snippetCreate ...
|
// (app *application) snippetCreate ...
|
||||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
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 ...
|
// snippetCreatePost ...
|
||||||
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
|
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
|
||||||
title := "0 snail"
|
err := r.ParseForm()
|
||||||
content := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n - Kobayashi Issa"
|
if err != nil {
|
||||||
expires := 7
|
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)
|
id, err := app.snippets.Insert(title, content, expires)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,6 +11,14 @@ import (
|
|||||||
func (app *application) routes() http.Handler {
|
func (app *application) routes() http.Handler {
|
||||||
router := httprouter.New()
|
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
|
// setup server for static files
|
||||||
fileServer := http.FileServer(http.Dir("./ui/static"))
|
fileServer := http.FileServer(http.Dir("./ui/static"))
|
||||||
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
|
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
|
||||||
|
23
snippetbox/ui/html/pages/create.tmpl
Normal file
23
snippetbox/ui/html/pages/create.tmpl
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{{define "title"}}Create a New Snippet{{end}}
|
||||||
|
|
||||||
|
{{define "main"}}
|
||||||
|
<form action='/snippet/create' method='POST'>
|
||||||
|
<div>
|
||||||
|
<label>Title:</label>
|
||||||
|
<input type='text' name='title'>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Content:</label>
|
||||||
|
<textarea name='content'></textarea>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Delete in:</label>
|
||||||
|
<input type='radio' name='expires' value='365' checked> One Year
|
||||||
|
<input type='radio' name='expires' value='7'> One Week
|
||||||
|
<input type='radio' name='expires' value='1'> One Day
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type='submit' value='Publish Snippet'>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{{end}}
|
@ -1,5 +1,6 @@
|
|||||||
{{define "nav"}}
|
{{define "nav"}}
|
||||||
<nav>
|
<nav>
|
||||||
<a href="/">Home</a>
|
<a href="/">Home</a>
|
||||||
|
<a href="/snippet/create">Create Snippet</a>
|
||||||
</nav>
|
</nav>
|
||||||
{{end}}
|
{{end}}
|
Loading…
Reference in New Issue
Block a user