aboutsummaryrefslogtreecommitdiffstats
path: root/internal/store/posts.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/store/posts.go')
-rw-r--r--internal/store/posts.go19
1 files changed, 15 insertions, 4 deletions
diff --git a/internal/store/posts.go b/internal/store/posts.go
index 569640b..706d132 100644
--- a/internal/store/posts.go
+++ b/internal/store/posts.go
@@ -18,16 +18,25 @@ type Post struct {
// joined
PageSlug string
PageTitle string
+ Tags []Tag // by name; IDs are not loaded
}
-const postCols = `p.id, p.page_id, p.slug, p.title, p.body_md, p.body_html, p.published, p.created_at, p.updated_at, g.slug, g.title`
+// The tags come along as two parallel arrays (names and slugs, both by name)
+// so every post query stays a single round trip; the alias p is the posts row.
+const postCols = `p.id, p.page_id, p.slug, p.title, p.body_md, p.body_html, p.published, p.created_at, p.updated_at, g.slug, g.title,
+ coalesce((SELECT array_agg(t.name ORDER BY t.name) FROM post_tags pt JOIN tags t ON t.id=pt.tag_id WHERE pt.post_id=p.id), '{}'),
+ coalesce((SELECT array_agg(t.slug ORDER BY t.name) FROM post_tags pt JOIN tags t ON t.id=pt.tag_id WHERE pt.post_id=p.id), '{}')`
func scanPost(row interface{ Scan(...any) error }) (*Post, error) {
var p Post
- err := row.Scan(&p.ID, &p.PageID, &p.Slug, &p.Title, &p.BodyMD, &p.BodyHTML, &p.Published, &p.CreatedAt, &p.UpdatedAt, &p.PageSlug, &p.PageTitle)
+ var names, slugs []string
+ err := row.Scan(&p.ID, &p.PageID, &p.Slug, &p.Title, &p.BodyMD, &p.BodyHTML, &p.Published, &p.CreatedAt, &p.UpdatedAt, &p.PageSlug, &p.PageTitle, &names, &slugs)
if err != nil {
return nil, wrap(err)
}
+ for i := range names {
+ p.Tags = append(p.Tags, Tag{Name: names[i], Slug: slugs[i]})
+ }
return &p, nil
}
@@ -130,6 +139,8 @@ func (bs *BlogStore) UpdatePost(ctx context.Context, p *Post) error {
}
func (bs *BlogStore) DeletePost(ctx context.Context, id int64) error {
- _, err := bs.db.Exec(ctx, `DELETE FROM posts WHERE id=$1`, id)
- return err
+ if _, err := bs.db.Exec(ctx, `DELETE FROM posts WHERE id=$1`, id); err != nil {
+ return err
+ }
+ return deleteOrphanTags(ctx, bs.db)
}