summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-08-19 21:47:38 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-08-19 21:47:38 -0600
commitf365b0975778984281b69d66150ee2d026e2613b (patch)
treee16022ab67413f96538cb720065264f73f562cb0
parent33a81f73e1ba59e76cf94c4439b80176cbb8261f (diff)
implemented draft publishing and removed New Posts link/capability
-rw-r--r--srv/src/http/api.go4
-rw-r--r--srv/src/http/drafts.go20
-rw-r--r--srv/src/http/posts.go67
-rw-r--r--srv/src/http/tpl/edit-post.html20
-rw-r--r--srv/src/http/tpl/posts.html6
5 files changed, 79 insertions, 38 deletions
diff --git a/srv/src/http/api.go b/srv/src/http/api.go
index eb7990f..44b9170 100644
--- a/srv/src/http/api.go
+++ b/srv/src/http/api.go
@@ -202,7 +202,7 @@ func (a *api) blogHandler() http.Handler {
mux.Handle("/posts/", http.StripPrefix("/posts",
apiutil.MethodMux(map[string]http.Handler{
"GET": a.renderPostHandler(),
- "POST": a.postPostHandler(false),
+ "POST": a.postPostHandler(),
"DELETE": a.deletePostHandler(false),
"PREVIEW": a.previewPostHandler(),
}),
@@ -223,7 +223,7 @@ func (a *api) blogHandler() http.Handler {
apiutil.MethodMux(map[string]http.Handler{
"GET": a.renderDraftPostHandler(),
- "POST": a.postPostHandler(true),
+ "POST": a.postDraftPostHandler(),
"DELETE": a.deletePostHandler(true),
"PREVIEW": a.previewPostHandler(),
}),
diff --git a/srv/src/http/drafts.go b/srv/src/http/drafts.go
index 8bb08e5..cb776b0 100644
--- a/srv/src/http/drafts.go
+++ b/srv/src/http/drafts.go
@@ -108,3 +108,23 @@ func (a *api) renderDraftPostsIndexHandler() http.Handler {
executeTemplate(rw, r, tpl, tplPayload)
})
}
+
+func (a *api) postDraftPostHandler() http.Handler {
+ return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+
+ p, err := postFromPostReq(r)
+ if err != nil {
+ apiutil.BadRequest(rw, r, err)
+ return
+ }
+
+ if err := a.params.PostDraftStore.Set(p); err != nil {
+ apiutil.InternalServerError(
+ rw, r, fmt.Errorf("storing post with id %q: %w", p.ID, err),
+ )
+ return
+ }
+
+ a.executeRedirectTpl(rw, r, a.draftURL(p.ID, false)+"?edit")
+ })
+}
diff --git a/srv/src/http/posts.go b/srv/src/http/posts.go
index 278ae43..c3f6363 100644
--- a/srv/src/http/posts.go
+++ b/srv/src/http/posts.go
@@ -2,6 +2,7 @@ package http
import (
"bytes"
+ "context"
"errors"
"fmt"
"html/template"
@@ -16,6 +17,7 @@ import (
"github.com/gomarkdown/markdown/parser"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
+ "github.com/mediocregopher/mediocre-go-lib/v2/mctx"
)
func (a *api) parsePostBody(post post.Post) (*txttpl.Template, error) {
@@ -248,6 +250,10 @@ func (a *api) renderEditPostHandler(isDraft bool) http.Handler {
)
return
}
+
+ } else if !isDraft {
+ http.Error(rw, "Post ID required in URL", 400)
+ return
}
tags, err := a.params.PostStore.GetTags()
@@ -296,49 +302,54 @@ func postFromPostReq(r *http.Request) (post.Post, error) {
return p, nil
}
-func (a *api) postPostHandler(isDraft bool) http.Handler {
+func (a *api) storeAndPublishPost(ctx context.Context, p post.Post) error {
+
+ first, err := a.params.PostStore.Set(p, time.Now())
+
+ if err != nil {
+ return fmt.Errorf("storing post with id %q: %w", p.ID, err)
+ }
+
+ if !first {
+ return nil
+ }
+
+ a.params.Logger.Info(ctx, "publishing blog post to mailing list")
+ urlStr := a.postURL(p.ID, true)
+
+ if err := a.params.MailingList.Publish(p.Title, urlStr); err != nil {
+ return fmt.Errorf("publishing post to mailing list: %w", err)
+ }
+
+ if err := a.params.PostDraftStore.Delete(p.ID); err != nil {
+ return fmt.Errorf("deleting draft: %w", err)
+ }
+
+ return nil
+}
+
+func (a *api) postPostHandler() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ ctx := r.Context()
+
p, err := postFromPostReq(r)
if err != nil {
apiutil.BadRequest(rw, r, err)
return
}
- var first bool
+ ctx = mctx.Annotate(ctx, "postID", p.ID)
- if isDraft {
- err = a.params.PostDraftStore.Set(p)
- } else {
- first, err = a.params.PostStore.Set(p, time.Now())
- }
-
- if err != nil {
+ if err := a.storeAndPublishPost(ctx, p); err != nil {
apiutil.InternalServerError(
- rw, r, fmt.Errorf("storing post with id %q: %w", p.ID, err),
+ rw, r, fmt.Errorf("storing/publishing post with id %q: %w", p.ID, err),
)
return
}
- if !isDraft && first {
-
- a.params.Logger.Info(r.Context(), "publishing blog post to mailing list")
- urlStr := a.postURL(p.ID, true)
-
- if err := a.params.MailingList.Publish(p.Title, urlStr); err != nil {
- apiutil.InternalServerError(
- rw, r, fmt.Errorf("publishing post with id %q: %w", p.ID, err),
- )
- return
- }
- }
-
- if isDraft {
- a.executeRedirectTpl(rw, r, a.draftURL(p.ID, false)+"?edit")
- } else {
- a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)+"?edit")
- }
+ a.executeRedirectTpl(rw, r, a.postURL(p.ID, false))
})
}
diff --git a/srv/src/http/tpl/edit-post.html b/srv/src/http/tpl/edit-post.html
index ea1f2c1..ea07680 100644
--- a/srv/src/http/tpl/edit-post.html
+++ b/srv/src/http/tpl/edit-post.html
@@ -90,6 +90,7 @@
</p>
<p>
+
<input
type="submit"
value="Preview"
@@ -99,8 +100,23 @@
{{ if .Payload.IsDraft }}
<input type="submit" value="Save" formaction="{{ BlogURL "drafts/" }}" />
- {{ else if eq .Payload.Post.ID "" }}
- <input type="submit" value="Publish" formaction="{{ BlogURL "posts/" }}" />
+
+
+ <script>
+ function confirmPublish(event) {
+ if (!confirm("Are you sure you're ready to publish?"))
+ event.preventDefault();
+ }
+ </script>
+
+
+ <input
+ type="submit"
+ value="Publish"
+ formaction="{{ BlogURL "posts/" }}"
+ onclick="confirmPublish(event)"
+ />
+
{{ else }}
<input type="submit" value="Update" formaction="{{ BlogURL "posts/" }}" />
{{ end }}
diff --git a/srv/src/http/tpl/posts.html b/srv/src/http/tpl/posts.html
index 946c713..5f75168 100644
--- a/srv/src/http/tpl/posts.html
+++ b/srv/src/http/tpl/posts.html
@@ -2,12 +2,6 @@
<h1>Posts</h1>
- <p>
- <a href="{{ BlogURL "posts/" }}?edit">
- New Post
- </a>
- </p>
-
{{ if ge .Payload.PrevPage 0 }}
<p>
<a href="?p={{ .Payload.PrevPage}}">&lt; &lt; Previous Page</a>