From 3015b7318ece38eb08a86d8b9b90f137a282ea2b Mon Sep 17 00:00:00 2001 From: lbenedar Date: Thu, 5 Mar 2026 15:17:19 +0300 Subject: [PATCH] ch8.3 --- cmd/web/handlers.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 61a0b66..7cfd6c1 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -6,6 +6,8 @@ import ( "fmt" "net/http" "strconv" + "strings" + "unicode/utf8" "gitea.local.lab/Lbenedar/snippetbox/internal/models" "github.com/julienschmidt/httprouter" @@ -83,12 +85,33 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request } title := r.PostForm.Get("title") content := r.PostForm.Get("content") + expires, err := strconv.Atoi(r.PostForm.Get("expires")) if err != nil { app.clientError(w, http.StatusBadRequest) return } + fieldErrors := make(map[string]string) + if strings.TrimSpace(title) == "" { + fieldErrors["title"] = "This field cannot be blank" + } else if utf8.RuneCountInString(title) > 100 { + fieldErrors["title"] = "This field cannot be more than 100 character long" + } + + if strings.TrimSpace(content) == "" { + fieldErrors["content"] = "This field cannot be blank" + } + + if expires != 1 && expires != 7 && expires != 365 { + fieldErrors["expires"] = "This field must equal 1, 7 and 365" + } + + if len(fieldErrors) > 0 { + fmt.Fprint(w, fieldErrors) + return + } + id, err := app.snippets.Insert(title, content, expires) if err != nil { app.serverError(w, err)