summaryrefslogtreecommitdiff
path: root/src/render/methods.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2024-05-18 15:55:59 +0200
committerBrian Picciano <mediocregopher@gmail.com>2024-05-18 15:55:59 +0200
commit93a8843e2e3391459fd333aef9e5c7617608c2b3 (patch)
tree2e5615cd22d1b39c7d5f870b88e3f29b5c24a223 /src/render/methods.go
parent8d7e708d98a3a46ba3ba08f9c8deeb4838bb8ca5 (diff)
Remove most rendering related querying from go http handlers
Diffstat (limited to 'src/render/methods.go')
-rw-r--r--src/render/methods.go51
1 files changed, 45 insertions, 6 deletions
diff --git a/src/render/methods.go b/src/render/methods.go
index 6dd9332..5b2c0b1 100644
--- a/src/render/methods.go
+++ b/src/render/methods.go
@@ -13,6 +13,7 @@ import (
"dev.mediocregopher.com/mediocre-blog.git/src/gmi/gemtext"
"dev.mediocregopher.com/mediocre-blog.git/src/post"
+ "dev.mediocregopher.com/mediocre-blog.git/src/post/asset"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
@@ -37,6 +38,12 @@ type GetPostsRes struct {
HasMore bool
}
+// GetDraftPostsRes are the fields returned from the GetDraftPosts method.
+type GetDraftPostsRes struct {
+ Posts []post.Post
+ HasMore bool
+}
+
// GetPostSeriesNextPreviousRes are the fields returned from the
// GetPostSeriesNextPreviousRes method.
type GetPostSeriesNextPreviousRes struct {
@@ -51,9 +58,12 @@ type Methods struct {
publicURL *url.URL
geminiGatewayURL *url.URL
postStore post.Store
+ postAssetStore asset.Store
+ postDraftStore post.DraftStore
preprocessFuncs post.PreprocessFunctions
- thisPost *post.StoredPost // cache
+ thisPost *post.StoredPost // cache
+ thisDraftPost *post.Post // cache
}
// NewMethods initializes a Methods using its required dependencies.
@@ -63,6 +73,8 @@ func NewMethods(
publicURL *url.URL,
geminiGatewayURL *url.URL,
postStore post.Store,
+ postAssetStore asset.Store,
+ postDraftStore post.DraftStore,
preprocessFuncs post.PreprocessFunctions,
) *Methods {
return &Methods{
@@ -71,16 +83,32 @@ func NewMethods(
publicURL,
geminiGatewayURL,
postStore,
+ postAssetStore,
+ postDraftStore,
preprocessFuncs,
nil, // thisPost
+ nil, // thisDraftPost
}
}
+func (m *Methods) GetTags() ([]string, error) {
+ return m.postStore.GetTags()
+}
+
+func (m *Methods) GetPostAssetIDs() ([]string, error) {
+ return m.postAssetStore.List()
+}
+
func (m *Methods) GetPosts(page, count int) (GetPostsRes, error) {
posts, hasMore, err := m.postStore.Get(page, count)
return GetPostsRes{posts, hasMore}, err
}
+func (m *Methods) GetDraftPosts(page, count int) (GetDraftPostsRes, error) {
+ posts, hasMore, err := m.postDraftStore.Get(page, count)
+ return GetDraftPostsRes{posts, hasMore}, err
+}
+
func (m *Methods) GetThisPost() (p post.StoredPost, err error) {
if m.thisPost != nil {
return *m.thisPost, nil
@@ -100,12 +128,23 @@ func (m *Methods) GetThisPost() (p post.StoredPost, err error) {
return m.postStore.GetByID(id)
}
-func (m *Methods) GetPostByID(id string) (post.StoredPost, error) {
- p, err := m.postStore.GetByID(id)
- if err != nil {
- return post.StoredPost{}, fmt.Errorf("fetching post %q: %w", id, err)
+func (m *Methods) GetThisDraftPost() (p post.Post, err error) {
+ if m.thisPost != nil {
+ return *m.thisDraftPost, nil
}
- return p, nil
+
+ defer func() {
+ m.thisDraftPost = &p
+ }()
+
+ id := path.Base(m.url.Path)
+ if id == "/" {
+ // An empty draft is fine, in the context of editing
+ return
+ }
+
+ id = strings.TrimSuffix(id, path.Ext(id))
+ return m.postDraftStore.GetByID(id)
}
func (m *Methods) GetPostSeriesNextPrevious(