From 0bc0204f0b718464d7ea9d97d6d03ee81f1953c6 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 09:33:03 -0600 Subject: Implement post deleting --- srv/src/api/posts.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'srv/src/api/posts.go') diff --git a/srv/src/api/posts.go b/srv/src/api/posts.go index 68754a3..a906f14 100644 --- a/srv/src/api/posts.go +++ b/srv/src/api/posts.go @@ -137,3 +137,31 @@ func (a *api) renderPostsIndexHandler() http.Handler { executeTemplate(rw, r, tpl, tplPayload) }) } + +func (a *api) deletePostHandler() http.Handler { + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + id := filepath.Base(r.URL.Path) + + if id == "" { + apiutil.BadRequest(rw, r, errors.New("id is required")) + return + } + + err := a.params.PostStore.Delete(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("deleting post with id %q: %w", id, err), + ) + return + } + + a.executeRedirectTpl(rw, r, "posts/") + + }) +} -- cgit v1.2.3