early admin edit blog page

This commit is contained in:
ari melody 2025-11-07 02:35:51 +00:00
parent 65366032fd
commit 82fd17c836
Signed by: ari
GPG key ID: CF99829C92678188
7 changed files with 297 additions and 15 deletions

View file

@ -15,12 +15,8 @@ func Handler(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
mux.Handle("/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
blogID := r.PathValue("id")
w.Write([]byte(blogID))
}))
mux.Handle("/", handleBlogIndex(app))
mux.Handle("/{id}", serveBlogPost(app))
mux.Handle("/", serveBlogIndex(app))
mux.ServeHTTP(w, r)
})
@ -38,14 +34,9 @@ type (
}
)
func handleBlogIndex(app *model.AppState) http.Handler {
func serveBlogIndex(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, err := controller.GetSessionFromRequest(app, r)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to retrieve session: %v\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
session := r.Context().Value("session").(*model.Session)
posts, err := controller.GetBlogPosts(app.DB, false, -1, 0)
if err != nil {
@ -108,3 +99,35 @@ func handleBlogIndex(app *model.AppState) http.Handler {
}
})
}
func serveBlogPost(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*model.Session)
blogID := r.PathValue("id")
post, err := controller.GetBlogPost(app.DB, blogID)
if err != nil {
fmt.Printf("can't find blog with ID %s\n", blogID)
http.NotFound(w, r)
return
}
type blogPostData struct {
core.AdminPageData
Post *model.BlogPost
}
err = templates.EditBlogTemplate.Execute(w, blogPostData{
AdminPageData: core.AdminPageData{
Session: session,
},
Post: post,
})
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Error rendering admin edit page for blog %s: %v\n", blogID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
})
}