diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-08-18 23:07:09 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-08-18 23:07:09 -0600 |
commit | c3135306b32e3ee18c3c412fbb2ea81b455201d5 (patch) | |
tree | f7dd21fc9e0bab9b06a713a8218e52be777f1b15 /srv/src/http/posts.go | |
parent | dfa9bcb9e23b10bb28973d1d27f05e04e8638320 (diff) |
drafts functionality added, needs a publish button still
Diffstat (limited to 'srv/src/http/posts.go')
-rw-r--r-- | srv/src/http/posts.go | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/srv/src/http/posts.go b/srv/src/http/posts.go index 8484817..278ae43 100644 --- a/srv/src/http/posts.go +++ b/srv/src/http/posts.go @@ -18,7 +18,7 @@ import ( "github.com/mediocregopher/blog.mediocregopher.com/srv/post" ) -func (a *api) parsePostBody(storedPost post.StoredPost) (*txttpl.Template, error) { +func (a *api) parsePostBody(post post.Post) (*txttpl.Template, error) { tpl := txttpl.New("root") tpl = tpl.Funcs(txttpl.FuncMap(a.tplFuncs())) @@ -43,7 +43,7 @@ func (a *api) parsePostBody(storedPost post.StoredPost) (*txttpl.Template, error }, }) - tpl, err := tpl.New(storedPost.ID + "-body.html").Parse(storedPost.Body) + tpl, err := tpl.New(post.ID + "-body.html").Parse(post.Body) if err != nil { return nil, err @@ -60,7 +60,7 @@ type postTplPayload struct { func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, error) { - bodyTpl, err := a.parsePostBody(storedPost) + bodyTpl, err := a.parsePostBody(storedPost.Post) if err != nil { return postTplPayload{}, fmt.Errorf("parsing post body as template: %w", err) } @@ -232,7 +232,12 @@ func (a *api) renderEditPostHandler(isDraft bool) http.Handler { if id != "/" { var err error - storedPost, err = a.params.PostStore.GetByID(id) + + if isDraft { + storedPost.Post, err = a.params.PostDraftStore.GetByID(id) + } else { + storedPost, err = a.params.PostStore.GetByID(id) + } if errors.Is(err, post.ErrPostNotFound) { http.Error(rw, "Post not found", 404) @@ -291,7 +296,7 @@ func postFromPostReq(r *http.Request) (post.Post, error) { return p, nil } -func (a *api) postPostHandler() http.Handler { +func (a *api) postPostHandler(isDraft bool) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { @@ -301,7 +306,13 @@ func (a *api) postPostHandler() http.Handler { return } - first, err := a.params.PostStore.Set(p, time.Now()) + var first bool + + if isDraft { + err = a.params.PostDraftStore.Set(p) + } else { + first, err = a.params.PostStore.Set(p, time.Now()) + } if err != nil { apiutil.InternalServerError( @@ -310,7 +321,7 @@ func (a *api) postPostHandler() http.Handler { return } - if first { + if !isDraft && first { a.params.Logger.Info(r.Context(), "publishing blog post to mailing list") urlStr := a.postURL(p.ID, true) @@ -323,11 +334,15 @@ func (a *api) postPostHandler() http.Handler { } } - a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)+"?edit") + if isDraft { + a.executeRedirectTpl(rw, r, a.draftURL(p.ID, false)+"?edit") + } else { + a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)+"?edit") + } }) } -func (a *api) deletePostHandler() http.Handler { +func (a *api) deletePostHandler(isDraft bool) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { @@ -338,7 +353,13 @@ func (a *api) deletePostHandler() http.Handler { return } - err := a.params.PostStore.Delete(id) + var err error + + if isDraft { + err = a.params.PostDraftStore.Delete(id) + } else { + err = a.params.PostStore.Delete(id) + } if errors.Is(err, post.ErrPostNotFound) { http.Error(rw, "Post not found", 404) @@ -350,8 +371,11 @@ func (a *api) deletePostHandler() http.Handler { return } - a.executeRedirectTpl(rw, r, a.postsURL(false)) - + if isDraft { + a.executeRedirectTpl(rw, r, a.draftsURL(false)) + } else { + a.executeRedirectTpl(rw, r, a.postsURL(false)) + } }) } |