summaryrefslogtreecommitdiff
path: root/srv/src
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-20 09:33:03 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-20 09:33:03 -0600
commit0bc0204f0b718464d7ea9d97d6d03ee81f1953c6 (patch)
tree73cdde7976391f153d457ac3682ba29a3873fc1d /srv/src
parent1242be7cfec1faa6a880d625307e32a1a91937ac (diff)
Implement post deleting
Diffstat (limited to 'srv/src')
-rw-r--r--srv/src/api/api.go7
-rw-r--r--srv/src/api/posts.go28
2 files changed, 34 insertions, 1 deletions
diff --git a/srv/src/api/api.go b/srv/src/api/api.go
index a6fc779..fc6f6a2 100644
--- a/srv/src/api/api.go
+++ b/srv/src/api/api.go
@@ -213,7 +213,12 @@ func (a *api) handler() http.Handler {
v2Mux := http.NewServeMux()
v2Mux.Handle("/follow.html", a.renderDumbTplHandler("follow.html"))
v2Mux.Handle("/posts/", http.StripPrefix("/posts",
- a.renderPostHandler(),
+ apiutil.MethodMux(map[string]http.Handler{
+ "GET": a.renderPostHandler(),
+ "DELETE": authMiddleware(auther,
+ formMiddleware(a.deletePostHandler()),
+ ),
+ }),
))
v2Mux.Handle("/assets/", http.StripPrefix("/assets",
apiutil.MethodMux(map[string]http.Handler{
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/")
+
+ })
+}