From 2c4b617ddeb71a5856075c94dcc7dfff3cfcbcaa Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 10:47:22 -0600 Subject: Implement saving of new and edited posts --- srv/src/api/api.go | 3 +++ srv/src/api/posts.go | 30 ++++++++++++++++++++++++++++++ srv/src/api/tpl/edit-post.html | 30 ++++++++++++++++++++++-------- srv/src/post/post.go | 5 +++++ 4 files changed, 60 insertions(+), 8 deletions(-) (limited to 'srv') diff --git a/srv/src/api/api.go b/srv/src/api/api.go index bc56164..adecff5 100644 --- a/srv/src/api/api.go +++ b/srv/src/api/api.go @@ -216,6 +216,9 @@ func (a *api) handler() http.Handler { apiutil.MethodMux(map[string]http.Handler{ "GET": a.renderPostHandler(), "EDIT": a.editPostHandler(), + "POST": authMiddleware(auther, + formMiddleware(a.postPostHandler()), + ), "DELETE": authMiddleware(auther, formMiddleware(a.deletePostHandler()), ), diff --git a/srv/src/api/posts.go b/srv/src/api/posts.go index e5916e1..845d9ff 100644 --- a/srv/src/api/posts.go +++ b/srv/src/api/posts.go @@ -7,6 +7,7 @@ import ( "net/http" "path/filepath" "strings" + "time" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" @@ -168,6 +169,35 @@ func (a *api) editPostHandler() http.Handler { }) } +func (a *api) postPostHandler() http.Handler { + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + p := post.Post{ + ID: r.PostFormValue("id"), + Title: r.PostFormValue("title"), + Description: r.PostFormValue("description"), + Tags: strings.Fields(r.PostFormValue("tags")), + Series: r.PostFormValue("series"), + } + + p.Body = strings.TrimSpace(r.PostFormValue("body")) + // textareas encode newlines as CRLF for historical reasons + p.Body = strings.ReplaceAll(p.Body, "\r\n", "\n") + + if err := a.params.PostStore.Set(p, time.Now()); err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("storing post with id %q: %w", p.ID, err), + ) + return + } + + redirectPath := fmt.Sprintf("posts/%s?method=edit", p.ID) + + a.executeRedirectTpl(rw, r, redirectPath) + }) +} + func (a *api) deletePostHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { diff --git a/srv/src/api/tpl/edit-post.html b/srv/src/api/tpl/edit-post.html index 9e30d4d..708858d 100644 --- a/srv/src/api/tpl/edit-post.html +++ b/srv/src/api/tpl/edit-post.html @@ -1,17 +1,31 @@ {{ define "body" }} +

+ + + +

+
+ {{ .CSRFFormInput }} +
- - + + {{ if eq .Payload.ID "" }} + + {{ else }} + {{ .Payload.ID }} + + {{ end }}
@@ -68,7 +82,7 @@ placeholder="Blog body" style="height: 50vh;" > - {{ .Payload.Body }} + {{- .Payload.Body -}}
diff --git a/srv/src/post/post.go b/srv/src/post/post.go index 766a543..803356e 100644 --- a/srv/src/post/post.go +++ b/srv/src/post/post.go @@ -124,6 +124,11 @@ func (s *store) withTx(cb func(*sql.Tx) error) error { } func (s *store) Set(post Post, now time.Time) error { + + if post.ID == "" { + return errors.New("post ID can't be empty") + } + return s.withTx(func(tx *sql.Tx) error { nowTS := now.Unix() -- cgit v1.2.3