learn-go/snippetbox/cmd/web/handlers.go

112 lines
2.6 KiB
Go
Raw Normal View History

2024-01-22 21:25:34 +00:00
package main
import (
2024-01-25 20:41:08 +00:00
"errors"
"fmt"
2024-01-22 21:25:34 +00:00
"net/http"
"strconv"
2024-01-25 20:41:08 +00:00
2024-01-26 06:01:12 +00:00
"github.com/julienschmidt/httprouter"
2024-01-25 20:41:08 +00:00
"snippetbox.chaosfem.tw/internal/models"
2024-02-03 05:24:04 +00:00
"snippetbox.chaosfem.tw/internal/validator"
2024-01-22 21:25:34 +00:00
)
// home ...
2024-01-26 06:01:12 +00:00
func (app *application) home(w http.ResponseWriter, r *http.Request) {
2024-01-25 20:41:08 +00:00
snippets, err := app.snippets.Latest()
2024-01-23 19:19:53 +00:00
if err != nil {
app.serverError(w, r, err)
2024-01-23 19:19:53 +00:00
return
}
2024-01-25 23:43:07 +00:00
data := app.newTemplateData(r)
data.Snippets = snippets
2024-01-25 22:08:58 +00:00
2024-01-25 23:06:28 +00:00
app.render(w, r, http.StatusOK, "home.tmpl", data)
2024-01-22 21:25:34 +00:00
}
// snippetView ...
2024-01-26 06:01:12 +00:00
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
id, err := strconv.Atoi(params.ByName("id"))
if err != nil || id < 1 {
app.notFound(w)
return
}
2024-01-25 20:41:08 +00:00
snippet, err := app.snippets.Get(id)
if err != nil || id < 1 {
if errors.Is(err, models.ErrNoRecord) {
app.notFound(w)
} else {
app.serverError(w, r, err)
}
return
}
2024-01-25 23:43:07 +00:00
data := app.newTemplateData(r)
data.Snippet = snippet
2024-01-25 21:41:06 +00:00
2024-01-25 23:06:28 +00:00
app.render(w, r, http.StatusOK, "view.tmpl", data)
2024-01-22 21:25:34 +00:00
}
2024-01-26 06:01:12 +00:00
// (app *application) snippetCreate ...
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
2024-01-26 06:31:27 +00:00
data := app.newTemplateData(r)
2024-01-30 23:51:20 +00:00
data.Form = snippetCreateForm{
Expires: 365,
}
2024-01-26 06:31:27 +00:00
app.render(w, r, http.StatusOK, "create.tmpl", data)
2024-01-26 06:01:12 +00:00
}
2024-01-22 21:25:34 +00:00
2024-01-30 23:51:20 +00:00
type snippetCreateForm struct {
Title string
Content string
Expires int
2024-02-03 05:24:04 +00:00
validator.Validator
2024-01-30 23:51:20 +00:00
}
2024-01-26 06:01:12 +00:00
// snippetCreatePost ...
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
2024-01-26 06:31:27 +00:00
err := r.ParseForm()
if err != nil {
app.clientError(w, http.StatusBadRequest)
return
}
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
if err != nil {
app.clientError(w, http.StatusBadRequest)
return
}
2024-01-25 20:41:08 +00:00
2024-01-30 23:51:20 +00:00
form := snippetCreateForm{
Title: r.PostForm.Get("title"),
Content: r.PostForm.Get("content"),
Expires: expires,
}
2024-01-26 06:57:49 +00:00
2024-02-03 05:24:04 +00:00
form.CheckField(validator.NotBlank(form.Title), "title", "This field cannot be blank")
form.CheckField(validator.MaxChars(form.Title, 100), "title", "This field cannot be more than 100 characters long")
form.CheckField(validator.NotBlank(form.Content), "content", "This field cannot be blank")
form.CheckField(validator.PermittedValue(form.Expires, 1, 7, 365), "expires", "This field must equal 1, 7 or 365")
2024-01-26 06:57:49 +00:00
2024-02-03 05:24:04 +00:00
if !form.Valid() {
2024-01-30 23:51:20 +00:00
data := app.newTemplateData(r)
data.Form = form
app.render(w, r, http.StatusUnprocessableEntity, "create.tmpl", data)
2024-01-26 06:57:49 +00:00
return
}
2024-01-30 23:51:20 +00:00
id, err := app.snippets.Insert(form.Title, form.Content, form.Expires)
2024-01-25 20:41:08 +00:00
if err != nil {
app.serverError(w, r, err)
return
}
2024-01-26 06:01:12 +00:00
http.Redirect(w, r, fmt.Sprintf("/snippet/view/%d", id), http.StatusSeeOther)
2024-01-22 21:25:34 +00:00
}