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/posts.go | 50 +++++++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) (limited to 'src/http/posts.go') diff --git a/src/http/posts.go b/src/http/posts.go index 09daac4..1950113 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -123,26 +123,14 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, return tplPayload, nil } -func (a *api) renderPostHandler() http.Handler { +func (a *api) getPostHandler() http.Handler { tpl := a.mustParseBasedTpl("post.html") - renderPostsIndexHandler := a.renderPostsIndexHandler() - renderEditPostHandler := a.renderEditPostHandler(false) return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { id := strings.TrimSuffix(filepath.Base(r.URL.Path), ".html") - if id == "/" { - renderPostsIndexHandler.ServeHTTP(rw, r) - return - } - - if _, ok := r.URL.Query()["edit"]; ok { - renderEditPostHandler.ServeHTTP(rw, r) - return - } - storedPost, err := a.params.PostStore.GetByID(id) if errors.Is(err, post.ErrPostNotFound) { @@ -171,19 +159,13 @@ func (a *api) renderPostHandler() http.Handler { }) } -func (a *api) renderPostsIndexHandler() http.Handler { +func (a *api) managePostsHandler() http.Handler { - renderEditPostHandler := a.renderEditPostHandler(false) - tpl := a.mustParseBasedTpl("posts.html") + tpl := a.mustParseBasedTpl("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( @@ -221,20 +203,26 @@ func (a *api) renderPostsIndexHandler() http.Handler { }) } -func (a *api) renderEditPostHandler(isDraft bool) http.Handler { +func (a *api) editPostHandler(isDraft bool) http.Handler { - tpl := a.mustParseBasedTpl("edit-post.html") + tpl := a.mustParseBasedTpl("post-edit.html") return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { id := filepath.Base(r.URL.Path) - var storedPost post.StoredPost + if id == "/" && !isDraft { + http.Error(rw, "Post id required", 400) + return + } + + var ( + storedPost post.StoredPost + err error + ) if id != "/" { - var err error - if isDraft { storedPost.Post, err = a.params.PostDraftStore.GetByID(id) } else { @@ -250,10 +238,6 @@ 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() @@ -348,7 +332,7 @@ func (a *api) postPostHandler() http.Handler { return } - a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)) + a.executeRedirectTpl(rw, r, a.editPostURL(p.ID, false)) }) } @@ -382,9 +366,9 @@ func (a *api) deletePostHandler(isDraft bool) http.Handler { } if isDraft { - a.executeRedirectTpl(rw, r, a.draftsURL(false)) + a.executeRedirectTpl(rw, r, a.manageDraftPostsURL(false)) } else { - a.executeRedirectTpl(rw, r, a.postsURL(false)) + a.executeRedirectTpl(rw, r, a.managePostsURL(false)) } }) } -- cgit v1.2.3