diff --git a/templates/post.html b/templates/post.html
index ef37627..5024318 100644
--- a/templates/post.html
+++ b/templates/post.html
@@ -20,8 +20,10 @@
diff --git a/templates/thread_create.html b/templates/thread_create.html
index 79d2c04..7a4f230 100644
--- a/templates/thread_create.html
+++ b/templates/thread_create.html
@@ -7,12 +7,20 @@
{{.CSRF}}
diff --git a/web/comment_handler.go b/web/comment_handler.go
index 8ce42bb..d96a95a 100644
--- a/web/comment_handler.go
+++ b/web/comment_handler.go
@@ -16,7 +16,15 @@ type CommentHandler struct {
func (h *CommentHandler) Store() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- content := r.FormValue("content")
+ form := CreateCommentForm{
+ Content: r.FormValue("content"),
+ }
+ if !form.Validate() {
+ h.sessions.Put(r.Context(), "form", form)
+ http.Redirect(w, r, r.Referer(), http.StatusFound)
+ return
+ }
+
idStr := chi.URLParam(r, "postID")
id, err := uuid.Parse(idStr)
@@ -28,7 +36,7 @@ func (h *CommentHandler) Store() http.HandlerFunc {
if err := h.store.CreateComment(&goddit.Comment{
ID: uuid.New(),
PostID: id,
- Content: content,
+ Content: form.Content,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
diff --git a/web/forms.go b/web/forms.go
new file mode 100644
index 0000000..e524629
--- /dev/null
+++ b/web/forms.go
@@ -0,0 +1,68 @@
+package web
+
+import "encoding/gob"
+
+func init() {
+ gob.Register(CreatePostForm{})
+ gob.Register(CreateThreadForm{})
+ gob.Register(CreateCommentForm{})
+ gob.Register(FormErrors{})
+}
+
+type FormErrors map[string]string
+
+type CreatePostForm struct {
+ Title string
+ Content string
+
+ Errors FormErrors
+}
+
+func (f *CreatePostForm) Validate() bool {
+ f.Errors = FormErrors{}
+
+ if f.Title == "" {
+ f.Errors["Title"] = "Please enter a title!"
+ }
+ if f.Content == "" {
+ f.Errors["Content"] = "Please enter a text!"
+ }
+
+ return len(f.Errors) == 0
+}
+
+type CreateThreadForm struct {
+ Title string
+ Description string
+
+ Errors FormErrors
+}
+
+func (f *CreateThreadForm) Validate() bool {
+ f.Errors = FormErrors{}
+
+ if f.Title == "" {
+ f.Errors["Title"] = "Please enter a title!"
+ }
+ if f.Description == "" {
+ f.Errors["Description"] = "Please enter a description!"
+ }
+
+ return len(f.Errors) == 0
+}
+
+type CreateCommentForm struct {
+ Content string
+
+ Errors FormErrors
+}
+
+func (f *CreateCommentForm) Validate() bool {
+ f.Errors = FormErrors{}
+
+ if f.Content == "" {
+ f.Errors["Content"] = "Please enter a comment!"
+ }
+
+ return len(f.Errors) == 0
+}
diff --git a/web/post_handler.go b/web/post_handler.go
index 5f7ac5b..97eafe6 100644
--- a/web/post_handler.go
+++ b/web/post_handler.go
@@ -102,8 +102,15 @@ func (h *PostHandler) Show() http.HandlerFunc {
func (h *PostHandler) Store() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- title := r.FormValue("title")
- content := r.FormValue("content")
+ form := CreatePostForm{
+ Title: r.FormValue("title"),
+ Content: r.FormValue("content"),
+ }
+ if !form.Validate() {
+ h.sessions.Put(r.Context(), "form", form)
+ http.Redirect(w, r, r.Referer(), http.StatusFound)
+ return
+ }
idStr := chi.URLParam(r, "id")
@@ -122,8 +129,8 @@ func (h *PostHandler) Store() http.HandlerFunc {
p := &goddit.Post{
ID: uuid.New(),
ThreadID: t.ID,
- Title: title,
- Content: content,
+ Title: form.Title,
+ Content: form.Content,
}
if err := h.store.CreatePost(p); err != nil {
diff --git a/web/sessions.go b/web/sessions.go
index d39c216..5a7a53e 100644
--- a/web/sessions.go
+++ b/web/sessions.go
@@ -22,6 +22,7 @@ func NewSessionsManager(dataSourceName string) (*scs.SessionManager, error) {
type SessionData struct {
FlashMessage string
+ Form interface{}
// UserID uuid.UUID
}
@@ -31,5 +32,10 @@ func GetSessionData(session *scs.SessionManager, ctx context.Context) SessionDat
data.FlashMessage = session.PopString(ctx, "flash")
// data.UserID, _ = session.Get(ctx "user_id").(uuid.UUID)
+ data.Form = session.Pop(ctx, "form")
+ if data.Form == nil {
+ data.Form = map[string]string{}
+ }
+
return data
}
diff --git a/web/thread_handler.go b/web/thread_handler.go
index 5c3efa8..9c71433 100644
--- a/web/thread_handler.go
+++ b/web/thread_handler.go
@@ -91,13 +91,20 @@ func (h *ThreadHandler) Show() http.HandlerFunc {
func (h *ThreadHandler) Store() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- title := r.FormValue("title")
- description := r.FormValue("description")
+ form := CreateThreadForm{
+ Title: r.FormValue("title"),
+ Description: r.FormValue("description"),
+ }
+ if !form.Validate() {
+ h.sessions.Put(r.Context(), "form", form)
+ http.Redirect(w, r, r.Referer(), http.StatusFound)
+ return
+ }
if err := h.store.CreateThread(&goddit.Thread{
ID: uuid.New(),
- Title: title,
- Description: description,
+ Title: form.Title,
+ Description: form.Description,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return