|
|
|
@ -123,88 +123,26 @@ func (a *api) postToPostTplPayload(storedPost post.StoredPost) (postTplPayload, |
|
|
|
|
return tplPayload, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (a *api) getPostsHandler() http.Handler { |
|
|
|
|
func (a *api) renderPostHandler() http.Handler { |
|
|
|
|
|
|
|
|
|
tpl := a.mustParseBasedTpl("posts.html") |
|
|
|
|
getPostHandler := a.getPostHandler() |
|
|
|
|
const pageCount = 20 |
|
|
|
|
tpl := a.mustParseBasedTpl("post.html") |
|
|
|
|
renderPostsIndexHandler := a.renderPostsIndexHandler() |
|
|
|
|
renderEditPostHandler := a.renderEditPostHandler(false) |
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
id := filepath.Base(r.URL.Path) |
|
|
|
|
|
|
|
|
|
if id != "/" { |
|
|
|
|
getPostHandler.ServeHTTP(rw, r) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
page, err := apiutil.StrToInt(r.FormValue("p"), 0) |
|
|
|
|
if err != nil { |
|
|
|
|
apiutil.BadRequest( |
|
|
|
|
rw, r, fmt.Errorf("invalid page number: %w", err), |
|
|
|
|
) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tag := r.FormValue("tag") |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
posts []post.StoredPost |
|
|
|
|
hasMore bool |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if tag == "" { |
|
|
|
|
posts, hasMore, err = a.params.PostStore.Get(page, pageCount) |
|
|
|
|
} else { |
|
|
|
|
posts, err = a.params.PostStore.GetByTag(tag) |
|
|
|
|
} |
|
|
|
|
id := strings.TrimSuffix(filepath.Base(r.URL.Path), ".html") |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
apiutil.InternalServerError( |
|
|
|
|
rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err), |
|
|
|
|
) |
|
|
|
|
if id == "/" { |
|
|
|
|
renderPostsIndexHandler.ServeHTTP(rw, r) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tags, err := a.params.PostStore.GetTags() |
|
|
|
|
if err != nil { |
|
|
|
|
apiutil.InternalServerError( |
|
|
|
|
rw, r, fmt.Errorf("fething tags: %w", err), |
|
|
|
|
) |
|
|
|
|
if _, ok := r.URL.Query()["edit"]; ok { |
|
|
|
|
renderEditPostHandler.ServeHTTP(rw, r) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tplPayload := struct { |
|
|
|
|
Posts []post.StoredPost |
|
|
|
|
PrevPage, NextPage int |
|
|
|
|
Tags []string |
|
|
|
|
}{ |
|
|
|
|
Posts: posts, |
|
|
|
|
PrevPage: -1, |
|
|
|
|
NextPage: -1, |
|
|
|
|
Tags: tags, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if page > 0 { |
|
|
|
|
tplPayload.PrevPage = page - 1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if hasMore { |
|
|
|
|
tplPayload.NextPage = page + 1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
executeTemplate(rw, r, tpl, tplPayload) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (a *api) getPostHandler() http.Handler { |
|
|
|
|
|
|
|
|
|
tpl := a.mustParseBasedTpl("post.html") |
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
id := strings.TrimSuffix(filepath.Base(r.URL.Path), ".html") |
|
|
|
|
|
|
|
|
|
storedPost, err := a.params.PostStore.GetByID(id) |
|
|
|
|
|
|
|
|
|
if errors.Is(err, post.ErrPostNotFound) { |
|
|
|
@ -233,13 +171,19 @@ func (a *api) getPostHandler() http.Handler { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (a *api) managePostsHandler() http.Handler { |
|
|
|
|
func (a *api) renderPostsIndexHandler() http.Handler { |
|
|
|
|
|
|
|
|
|
tpl := a.mustParseBasedTpl("posts-manage.html") |
|
|
|
|
renderEditPostHandler := a.renderEditPostHandler(false) |
|
|
|
|
tpl := a.mustParseBasedTpl("posts.html") |
|
|
|
|
const pageCount = 20 |
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
if _, ok := r.URL.Query()["edit"]; ok { |
|
|
|
|
renderEditPostHandler.ServeHTTP(rw, r) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
page, err := apiutil.StrToInt(r.FormValue("p"), 0) |
|
|
|
|
if err != nil { |
|
|
|
|
apiutil.BadRequest( |
|
|
|
@ -277,26 +221,20 @@ func (a *api) managePostsHandler() http.Handler { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (a *api) editPostHandler(isDraft bool) http.Handler { |
|
|
|
|
func (a *api) renderEditPostHandler(isDraft bool) http.Handler { |
|
|
|
|
|
|
|
|
|
tpl := a.mustParseBasedTpl("post-edit.html") |
|
|
|
|
tpl := a.mustParseBasedTpl("edit-post.html") |
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
id := filepath.Base(r.URL.Path) |
|
|
|
|
|
|
|
|
|
if id == "/" && !isDraft { |
|
|
|
|
http.Error(rw, "Post id required", 400) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
storedPost post.StoredPost |
|
|
|
|
err error |
|
|
|
|
) |
|
|
|
|
var storedPost post.StoredPost |
|
|
|
|
|
|
|
|
|
if id != "/" { |
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
if isDraft { |
|
|
|
|
storedPost.Post, err = a.params.PostDraftStore.GetByID(id) |
|
|
|
|
} else { |
|
|
|
@ -312,6 +250,10 @@ func (a *api) editPostHandler(isDraft bool) http.Handler { |
|
|
|
|
) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else if !isDraft { |
|
|
|
|
http.Error(rw, "Post ID required in URL", 400) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tags, err := a.params.PostStore.GetTags() |
|
|
|
@ -406,7 +348,7 @@ func (a *api) postPostHandler() http.Handler { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
a.executeRedirectTpl(rw, r, a.editPostURL(p.ID, false)) |
|
|
|
|
a.executeRedirectTpl(rw, r, a.postURL(p.ID, false)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -440,9 +382,9 @@ func (a *api) deletePostHandler(isDraft bool) http.Handler { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if isDraft { |
|
|
|
|
a.executeRedirectTpl(rw, r, a.manageDraftPostsURL(false)) |
|
|
|
|
a.executeRedirectTpl(rw, r, a.draftsURL(false)) |
|
|
|
|
} else { |
|
|
|
|
a.executeRedirectTpl(rw, r, a.managePostsURL(false)) |
|
|
|
|
a.executeRedirectTpl(rw, r, a.postsURL(false)) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|