From 75044eef0331bb9448da813288aafc6735ce7c22 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 10:13:46 -0600 Subject: Implement edit post page --- srv/src/api/posts.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'srv/src/api/posts.go') diff --git a/srv/src/api/posts.go b/srv/src/api/posts.go index a906f14..e5916e1 100644 --- a/srv/src/api/posts.go +++ b/srv/src/api/posts.go @@ -138,13 +138,43 @@ func (a *api) renderPostsIndexHandler() http.Handler { }) } +func (a *api) editPostHandler() http.Handler { + + tpl := a.mustParseBasedTpl("edit-post.html") + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + id := filepath.Base(r.URL.Path) + + var storedPost post.StoredPost + + if id != "/" { + + var err error + storedPost, err = a.params.PostStore.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 + } + } + + executeTemplate(rw, r, tpl, storedPost) + }) +} + func (a *api) deletePostHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { id := filepath.Base(r.URL.Path) - if id == "" { + if id == "/" { apiutil.BadRequest(rw, r, errors.New("id is required")) return } -- cgit v1.2.3