From 93a8843e2e3391459fd333aef9e5c7617608c2b3 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 18 May 2024 15:55:59 +0200 Subject: Remove most rendering related querying from go http handlers --- src/render/methods.go | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'src/render') 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( -- cgit v1.2.3