From 1f3ae665ed2e58ca572678ce7caf8b711f226392 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Tue, 29 Nov 2022 20:59:31 +0100 Subject: 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. --- src/http/drafts.go | 63 +++--------------------------------------------------- 1 file changed, 3 insertions(+), 60 deletions(-) (limited to 'src/http/drafts.go') 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)) }) } -- cgit v1.2.3