ch8.3
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
@@ -83,12 +85,33 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request
|
|||||||
}
|
}
|
||||||
title := r.PostForm.Get("title")
|
title := r.PostForm.Get("title")
|
||||||
content := r.PostForm.Get("content")
|
content := r.PostForm.Get("content")
|
||||||
|
|
||||||
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.clientError(w, http.StatusBadRequest)
|
app.clientError(w, http.StatusBadRequest)
|
||||||
return
|
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)
|
id, err := app.snippets.Insert(title, content, expires)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.serverError(w, err)
|
app.serverError(w, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user