1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package db
import (
"database/sql"
"encoding/json"
"time"
)
type PostRecord struct {
Slug string
Title string
Date string
Tags []string
Draft bool
Blocks string // raw EditorJS JSON
UpdatedAt int64 // Unix microseconds
}
// UpsertPost inserts or updates a post record.
func (m *MetaDB) UpsertPost(p PostRecord) error {
tags, _ := json.Marshal(p.Tags)
draft := 0
if p.Draft {
draft = 1
}
_, err := m.db.Exec(`
INSERT INTO posts (slug, title, date, tags, draft, blocks, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(slug) DO UPDATE SET
title = excluded.title,
date = excluded.date,
tags = excluded.tags,
draft = excluded.draft,
blocks = excluded.blocks,
updated_at = excluded.updated_at
`, p.Slug, p.Title, p.Date, string(tags), draft, p.Blocks, p.UpdatedAt)
return err
}
// GetPost retrieves a single post by slug.
func (m *MetaDB) GetPost(slug string) (*PostRecord, error) {
row := m.db.QueryRow(
`SELECT slug, title, date, tags, draft, blocks, updated_at FROM posts WHERE slug = ?`, slug)
var p PostRecord
var tagsJSON string
var draft int
if err := row.Scan(&p.Slug, &p.Title, &p.Date, &tagsJSON, &draft, &p.Blocks, &p.UpdatedAt); err != nil {
return nil, err
}
p.Draft = draft != 0
_ = json.Unmarshal([]byte(tagsJSON), &p.Tags)
if p.Tags == nil {
p.Tags = []string{}
}
return &p, nil
}
// ListPosts returns all posts, optionally including drafts.
func (m *MetaDB) ListPosts(includeDrafts bool) ([]PostRecord, error) {
query := `SELECT slug, title, date, tags, draft, blocks, updated_at FROM posts`
if !includeDrafts {
query += ` WHERE draft = 0`
}
query += ` ORDER BY date DESC, slug`
rows, err := m.db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
return scanPosts(rows)
}
// DeletePost removes a post by slug.
func (m *MetaDB) DeletePost(slug string) error {
_, err := m.db.Exec(`DELETE FROM posts WHERE slug = ?`, slug)
return err
}
func scanPosts(rows *sql.Rows) ([]PostRecord, error) {
var posts []PostRecord
for rows.Next() {
var p PostRecord
var tagsJSON string
var draft int
if err := rows.Scan(&p.Slug, &p.Title, &p.Date, &tagsJSON, &draft, &p.Blocks, &p.UpdatedAt); err != nil {
return nil, err
}
p.Draft = draft != 0
_ = json.Unmarshal([]byte(tagsJSON), &p.Tags)
if p.Tags == nil {
p.Tags = []string{}
}
posts = append(posts, p)
}
return posts, rows.Err()
}
// DB returns the underlying *sql.DB for advanced queries (e.g., transaction handling).
func (m *MetaDB) DB() *sql.DB {
return m.db
}
// GetPostRawPath returns the path for indexing in the search database.
func (p *PostRecord) GetPostRawPath() string {
return "/" + p.Slug
}
// GetUpdatedTime returns the updated_at as a time.Time for comparison.
func (p *PostRecord) GetUpdatedTime() time.Time {
return time.UnixMicro(p.UpdatedAt).UTC()
}
// AddRedirect inserts or replaces a redirect from fromSlug to toSlug.
func (m *MetaDB) AddRedirect(fromSlug, toSlug string) error {
_, err := m.db.Exec(
`INSERT INTO redirects (from_slug, to_slug) VALUES (?, ?)
ON CONFLICT(from_slug) DO UPDATE SET to_slug = excluded.to_slug`,
fromSlug, toSlug,
)
return err
}
// GetRedirect returns the slug that fromSlug redirects to, or sql.ErrNoRows if none.
func (m *MetaDB) GetRedirect(fromSlug string) (string, error) {
var toSlug string
err := m.db.QueryRow(
`SELECT to_slug FROM redirects WHERE from_slug = ?`, fromSlug,
).Scan(&toSlug)
return toSlug, err
}
// CollapseRedirects updates all redirects pointing to oldSlug so they point to newSlug instead,
// preventing redirect chains when a slug is renamed again.
func (m *MetaDB) CollapseRedirects(oldSlug, newSlug string) error {
_, err := m.db.Exec(
`UPDATE redirects SET to_slug = ? WHERE to_slug = ?`, newSlug, oldSlug,
)
return err
}
|