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

151 lines
3.7 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 {
2024-02-07 00:46:46 +00:00
Title string `form:"title"`
Content string `form:"content"`
Expires int `form:"expires"`
validator.Validator `form:"-"`
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-02-07 00:46:46 +00:00
var form snippetCreateForm
2024-01-26 06:31:27 +00:00
2024-02-07 00:46:46 +00:00
err := app.decodePostForm(r, &form)
2024-01-26 06:31:27 +00:00
if err != nil {
app.clientError(w, http.StatusBadRequest)
return
}
2024-01-25 20:41:08 +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-02-07 05:37:06 +00:00
app.sessionManager.Put(r.Context(), "flash", "Snippet successfully created!")
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
}
type userSignupForm struct {
Username string `form:"username"`
Password string `form:"password"`
validator.Validator `form:"-"`
}
// userSignup ...
func (app *application) userSignup(w http.ResponseWriter, r *http.Request) {
data := app.newTemplateData(r)
data.Form = userSignupForm{}
app.render(w, r, http.StatusOK, "signup.tmpl", data)
}
// userSignup ...
func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) {
var form userSignupForm
err := app.decodePostForm(r, &form)
if err != nil {
app.clientError(w, http.StatusBadRequest)
return
}
form.CheckField(validator.NotBlank(form.Username), "username", "This field cannot be blank")
form.CheckField(validator.NotBlank(form.Password), "password", "This field cannot be blank")
if !form.Valid() {
data := app.newTemplateData(r)
data.Form = form
app.render(w, r, http.StatusUnprocessableEntity, "signup.tmpl", data)
return
}
id, err := app.users.Insert(form.Username, form.Password)
if err != nil {
app.serverError(w, r, err)
return
}
app.sessionManager.Put(r.Context(), "flash", fmt.Sprintf("CREATED A USER! (%d)", id))
http.Redirect(w, r, "/", http.StatusSeeOther)
}