Implement post deleting

This commit is contained in:
Brian Picciano 2022-05-20 09:33:03 -06:00
parent 1242be7cfe
commit 0bc0204f0b
2 changed files with 34 additions and 1 deletions

View File

@ -213,7 +213,12 @@ func (a *api) handler() http.Handler {
v2Mux := http.NewServeMux() v2Mux := http.NewServeMux()
v2Mux.Handle("/follow.html", a.renderDumbTplHandler("follow.html")) v2Mux.Handle("/follow.html", a.renderDumbTplHandler("follow.html"))
v2Mux.Handle("/posts/", http.StripPrefix("/posts", 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", v2Mux.Handle("/assets/", http.StripPrefix("/assets",
apiutil.MethodMux(map[string]http.Handler{ apiutil.MethodMux(map[string]http.Handler{

View File

@ -137,3 +137,31 @@ func (a *api) renderPostsIndexHandler() http.Handler {
executeTemplate(rw, r, tpl, tplPayload) 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/")
})
}