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

241 lines
6.4 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
)
2024-02-13 20:54:07 +00:00
// ping ...
func ping(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
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-08 00:27:03 +00:00
Title string `form:"title"`
Content string `form:"content"`
Expires int `form:"expires"`
2024-02-07 00:46:46 +00:00
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 {
2024-02-08 00:27:03 +00:00
Username string `form:"username"`
Email string `form:"email"`
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)
}
// userSignupPost ...
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")
2024-02-07 23:15:54 +00:00
form.CheckField(validator.NotBlank(form.Email), "email", "This field cannot be blank")
form.CheckField(validator.Matches(form.Email, validator.EmailRX), "email", "This field must be a valid email address")
form.CheckField(validator.NotBlank(form.Password), "password", "This field cannot be blank")
2024-02-07 23:15:54 +00:00
form.CheckField(validator.MinChars(form.Password, 8), "password", "This field must be at least 8 characters long")
if !form.Valid() {
data := app.newTemplateData(r)
data.Form = form
app.render(w, r, http.StatusUnprocessableEntity, "signup.tmpl", data)
return
}
2024-02-07 23:54:12 +00:00
err = app.users.Insert(form.Username, form.Email, form.Password)
if err != nil {
if errors.Is(err, models.ErrDuplicateEmail) {
form.AddFieldError("email", "Email address is already in use")
data := app.newTemplateData(r)
data.Form = form
app.render(w, r, http.StatusUnprocessableEntity, "signup.tmpl", data)
} else {
app.serverError(w, r, err)
}
return
}
2024-02-07 23:54:12 +00:00
app.sessionManager.Put(r.Context(), "flash", fmt.Sprintf("Signup was successful (%s). Please log in.", form.Username))
http.Redirect(w, r, "/", http.StatusSeeOther)
}
type userLoginForm struct {
2024-02-08 00:27:03 +00:00
Email string `form:"email"`
Password string `form:"password"`
validator.Validator `form:"-"`
}
// userLogin ...
func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
data := app.newTemplateData(r)
data.Form = userLoginForm{}
app.render(w, r, http.StatusOK, "login.tmpl", data)
}
// userLoginPost ...
func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) {
var form userSignupForm
err := app.decodePostForm(r, &form)
if err != nil {
app.clientError(w, http.StatusBadRequest)
return
}
2024-02-08 00:27:03 +00:00
form.CheckField(validator.NotBlank(form.Email), "email", "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, "login.tmpl", data)
return
}
2024-02-08 00:27:03 +00:00
userID, err := app.users.Authenticate(form.Email, form.Password)
if err != nil {
if errors.Is(err, models.ErrInvalidCredentials) {
form.AddNonFieldError("Email or password is incorrect")
2024-02-07 20:40:54 +00:00
2024-02-08 00:27:03 +00:00
data := app.newTemplateData(r)
data.Form = form
app.render(w, r, http.StatusUnprocessableEntity, "login.tmpl", data)
} else {
app.serverError(w, r, err)
}
return
}
err = app.sessionManager.RenewToken(r.Context())
if err != nil {
app.serverError(w, r, err)
return
}
2024-02-07 20:40:54 +00:00
2024-02-08 00:27:03 +00:00
app.sessionManager.Put(r.Context(), "authenticatedUserID", userID)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// userLoginPost ...
func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
2024-02-08 00:29:09 +00:00
err := app.sessionManager.RenewToken(r.Context())
if err != nil {
app.serverError(w, r, err)
return
}
app.sessionManager.Remove(r.Context(), "authenticatedUserID")
app.sessionManager.Put(r.Context(), "flash", fmt.Sprintf("You have been logged out."))
http.Redirect(w, r, "/", http.StatusSeeOther)
}