|
|
|
@ -10,6 +10,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"git.runcible.io/learning/ratchet/internal/model"
|
|
|
|
|
"git.runcible.io/learning/ratchet/internal/validator"
|
|
|
|
|
"github.com/go-playground/form/v4"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TODO function should accept and a pointer to an interface allowing for mocking in tests.
|
|
|
|
@ -139,7 +140,7 @@ func handleSnippetCreateGet(tc *TemplateCache) http.Handler {
|
|
|
|
|
// snippetCreate handles display of the form used to create snippets
|
|
|
|
|
//
|
|
|
|
|
// curl -iL -d "" http://localhost:5001/snippet/create
|
|
|
|
|
func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, formDecoder *form.Decoder, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
return http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// example of a custom header. Must be done before calling WriteHeader
|
|
|
|
@ -173,24 +174,36 @@ func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, snippetServ
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var form snippetCreateForm
|
|
|
|
|
|
|
|
|
|
// AUTOMATIC FORM PROCESSING
|
|
|
|
|
|
|
|
|
|
err = formDecoder.Decode(&form, r.PostForm)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
clientError(w, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// OLD WAY
|
|
|
|
|
// The r.PostForm.Get() method always returns the form data as a *string*.
|
|
|
|
|
// However, we're expecting our expires value to be a number, and want to
|
|
|
|
|
// represent it in our Go code as an integer. So we need to manually convert
|
|
|
|
|
// the form data to an integer using strconv.Atoi(), and we send a 400 Bad
|
|
|
|
|
// Request response if the conversion fails.
|
|
|
|
|
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
clientError(w, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// clientError(w, http.StatusBadRequest)
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Create an instance of the snippetCreateForm struct containing the values
|
|
|
|
|
// from the form and an empty map for any validation errors.
|
|
|
|
|
form := snippetCreateForm{
|
|
|
|
|
Title: r.PostForm.Get("title"),
|
|
|
|
|
Content: r.PostForm.Get("content"),
|
|
|
|
|
Expires: expires,
|
|
|
|
|
}
|
|
|
|
|
// form := snippetCreateForm{
|
|
|
|
|
// Title: r.PostForm.Get("title"),
|
|
|
|
|
// Content: r.PostForm.Get("content"),
|
|
|
|
|
// Expires: expires,
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// VALIDATION
|
|
|
|
|
// THE OLD WAY
|
|
|
|
|