learn-go/snippetbox/cmd/web/handlers.go
2024-01-30 15:51:20 -08:00

123 lines
2.7 KiB
Go

package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"unicode/utf8"
"github.com/julienschmidt/httprouter"
"snippetbox.chaosfem.tw/internal/models"
)
// home ...
func (app *application) home(w http.ResponseWriter, r *http.Request) {
snippets, err := app.snippets.Latest()
if err != nil {
app.serverError(w, r, err)
return
}
data := app.newTemplateData(r)
data.Snippets = snippets
app.render(w, r, http.StatusOK, "home.tmpl", data)
}
// snippetView ...
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
}
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
}
data := app.newTemplateData(r)
data.Snippet = snippet
app.render(w, r, http.StatusOK, "view.tmpl", data)
}
// (app *application) snippetCreate ...
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
data := app.newTemplateData(r)
data.Form = snippetCreateForm{
Expires: 365,
}
app.render(w, r, http.StatusOK, "create.tmpl", data)
}
type snippetCreateForm struct {
Title string
Content string
Expires int
FieldErrors map[string]string
}
// snippetCreatePost ...
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
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
}
form := snippetCreateForm{
Title: r.PostForm.Get("title"),
Content: r.PostForm.Get("content"),
Expires: expires,
FieldErrors: map[string]string{},
}
if strings.TrimSpace(form.Title) == "" {
form.FieldErrors["title"] = "This field cannot be blank"
} else if utf8.RuneCountInString(form.Title) > 100 {
form.FieldErrors["title"] = "This field cannot contain more than 100 characters"
}
if strings.TrimSpace(form.Content) == "" {
form.FieldErrors["content"] = "This field cannot be blank"
}
if form.Expires != 1 && form.Expires != 7 && form.Expires != 365 {
form.FieldErrors["expires"] = "This field must equal 1, 7 or 365"
}
if len(form.FieldErrors) > 0 {
data := app.newTemplateData(r)
data.Form = form
app.render(w, r, http.StatusUnprocessableEntity, "create.tmpl", data)
return
}
id, err := app.snippets.Insert(form.Title, form.Content, form.Expires)
if err != nil {
app.serverError(w, r, err)
return
}
http.Redirect(w, r, fmt.Sprintf("/snippet/view/%d", id), http.StatusSeeOther)
}