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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package post
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
)
type DraftStore interface {
// Set sets the draft Post's data into the storage, keyed by the draft
// Post's ID.
Set(post Post) error
// Get returns count draft Posts, sorted id descending, offset by the
// given page number. The returned boolean indicates if there are more pages
// or not.
Get(page, count int) ([]Post, bool, error)
// GetByID will return the draft Post with the given ID, or ErrPostNotFound.
GetByID(id string) (Post, error)
// Delete will delete the draft Post with the given ID.
Delete(id string) error
}
type draftStore struct {
db *SQLDB
}
// NewDraftStore initializes a new DraftStore using an existing SQLDB.
func NewDraftStore(db *SQLDB) DraftStore {
return &draftStore{
db: db,
}
}
func (s *draftStore) Set(post Post) error {
if post.ID == "" {
return errors.New("post ID can't be empty")
}
tagsJSON, err := json.Marshal(post.Tags)
if err != nil {
return fmt.Errorf("json marshaling tags %#v: %w", post.Tags, err)
}
_, err = s.db.db.Exec(
`INSERT INTO post_drafts (
id, title, description, tags, series, body
)
VALUES
(?, ?, ?, ?, ?, ?)
ON CONFLICT (id) DO UPDATE SET
title=excluded.title,
description=excluded.description,
tags=excluded.tags,
series=excluded.series,
body=excluded.body`,
post.ID,
post.Title,
post.Description,
&sql.NullString{String: string(tagsJSON), Valid: len(post.Tags) > 0},
&sql.NullString{String: post.Series, Valid: post.Series != ""},
post.Body,
)
if err != nil {
return fmt.Errorf("inserting into post_drafts: %w", err)
}
return nil
}
func (s *draftStore) get(
querier interface {
Query(string, ...interface{}) (*sql.Rows, error)
},
limit, offset int,
where string, whereArgs ...interface{},
) (
[]Post, error,
) {
query := `
SELECT
p.id, p.title, p.description, p.tags, p.series, p.body
FROM post_drafts p
` + where + `
ORDER BY p.id ASC`
if limit > 0 {
query += fmt.Sprintf(" LIMIT %d", limit)
}
if offset > 0 {
query += fmt.Sprintf(" OFFSET %d", offset)
}
rows, err := querier.Query(query, whereArgs...)
if err != nil {
return nil, fmt.Errorf("selecting: %w", err)
}
defer rows.Close()
var posts []Post
for rows.Next() {
var (
post Post
tags, series sql.NullString
)
err := rows.Scan(
&post.ID, &post.Title, &post.Description, &tags, &series,
&post.Body,
)
if err != nil {
return nil, fmt.Errorf("scanning row: %w", err)
}
post.Series = series.String
if tags.String != "" {
if err := json.Unmarshal([]byte(tags.String), &post.Tags); err != nil {
return nil, fmt.Errorf("json parsing %q: %w", tags.String, err)
}
}
posts = append(posts, post)
}
return posts, nil
}
func (s *draftStore) Get(page, count int) ([]Post, bool, error) {
posts, err := s.get(s.db.db, count+1, page*count, ``)
if err != nil {
return nil, false, fmt.Errorf("querying post_drafts: %w", err)
}
var hasMore bool
if len(posts) > count {
hasMore = true
posts = posts[:count]
}
return posts, hasMore, nil
}
func (s *draftStore) GetByID(id string) (Post, error) {
posts, err := s.get(s.db.db, 0, 0, `WHERE p.id=?`, id)
if err != nil {
return Post{}, fmt.Errorf("querying post_drafts: %w", err)
}
if len(posts) == 0 {
return Post{}, ErrPostNotFound
}
if len(posts) > 1 {
panic(fmt.Sprintf("got back multiple draft posts querying id %q: %+v", id, posts))
}
return posts[0], nil
}
func (s *draftStore) Delete(id string) error {
if _, err := s.db.db.Exec(`DELETE FROM post_drafts WHERE id = ?`, id); err != nil {
return fmt.Errorf("deleting from post_drafts: %w", err)
}
return nil
}
|