summaryrefslogtreecommitdiff
path: root/src/http/drafts.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-11-29 20:59:31 +0100
committerBrian Picciano <mediocregopher@gmail.com>2022-11-29 20:59:31 +0100
commit1f3ae665ed2e58ca572678ce7caf8b711f226392 (patch)
treee023602e07e2a80d24ef9d9527ddca0e1640e929 /src/http/drafts.go
parent31f8f37c5ad3ad4ac7b3cc93d0257dd80c877c7c (diff)
Introduce EDIT and MANAGE methods
All admin "index" pages are moved under MANAGE, so that we can have (for example) and normal "GET /posts" page later which would replace the current index page, and potentially corresponding pages for the other categories. The EDIT method replaces the old `?edit` pattern, which normalizes how we differentiate page functionality generally.
Diffstat (limited to 'src/http/drafts.go')
-rw-r--r--src/http/drafts.go63
1 files changed, 3 insertions, 60 deletions
diff --git a/src/http/drafts.go b/src/http/drafts.go
index cb776b0..4b98bc8 100644
--- a/src/http/drafts.go
+++ b/src/http/drafts.go
@@ -1,77 +1,20 @@
package http
import (
- "errors"
"fmt"
"net/http"
- "path/filepath"
- "strings"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
"github.com/mediocregopher/blog.mediocregopher.com/srv/post"
)
-func (a *api) renderDraftPostHandler() http.Handler {
+func (a *api) manageDraftPostsHandler() http.Handler {
- tpl := a.mustParseBasedTpl("post.html")
- renderDraftPostsIndexHandler := a.renderDraftPostsIndexHandler()
- renderDraftEditPostHandler := a.renderEditPostHandler(true)
-
- return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
-
- id := strings.TrimSuffix(filepath.Base(r.URL.Path), ".html")
-
- if id == "/" {
- renderDraftPostsIndexHandler.ServeHTTP(rw, r)
- return
- }
-
- if _, ok := r.URL.Query()["edit"]; ok {
- renderDraftEditPostHandler.ServeHTTP(rw, r)
- return
- }
-
- p, err := a.params.PostDraftStore.GetByID(id)
-
- if errors.Is(err, post.ErrPostNotFound) {
- http.Error(rw, "Post not found", 404)
- return
- } else if err != nil {
- apiutil.InternalServerError(
- rw, r, fmt.Errorf("fetching post with id %q: %w", id, err),
- )
- return
- }
-
- tplPayload, err := a.postToPostTplPayload(post.StoredPost{Post: p})
-
- if err != nil {
- apiutil.InternalServerError(
- rw, r, fmt.Errorf(
- "generating template payload for post with id %q: %w",
- id, err,
- ),
- )
- return
- }
-
- executeTemplate(rw, r, tpl, tplPayload)
- })
-}
-
-func (a *api) renderDraftPostsIndexHandler() http.Handler {
-
- renderEditPostHandler := a.renderEditPostHandler(true)
- tpl := a.mustParseBasedTpl("draft-posts.html")
+ tpl := a.mustParseBasedTpl("draft-posts-manage.html")
const pageCount = 20
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
- if _, ok := r.URL.Query()["edit"]; ok {
- renderEditPostHandler.ServeHTTP(rw, r)
- return
- }
-
page, err := apiutil.StrToInt(r.FormValue("p"), 0)
if err != nil {
apiutil.BadRequest(
@@ -125,6 +68,6 @@ func (a *api) postDraftPostHandler() http.Handler {
return
}
- a.executeRedirectTpl(rw, r, a.draftURL(p.ID, false)+"?edit")
+ a.executeRedirectTpl(rw, r, a.editDraftPostURL(p.ID, false))
})
}