-
Notifications
You must be signed in to change notification settings - Fork 5
/
posts.sql.go
266 lines (242 loc) · 4.88 KB
/
posts.sql.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.16.0
// source: posts.sql
package postgres
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
)
const createPost = `-- name: CreatePost :one
insert into posts (
id,
"namespace",
slug,
current_content_id,
published_at
) values (
$1,
$2,
$3,
$4,
$5
) returning id
`
type CreatePostParams struct {
ID uuid.UUID
Namespace string
Slug string
CurrentContentID uuid.UUID
PublishedAt sql.NullTime
}
func (q *Queries) CreatePost(ctx context.Context, arg CreatePostParams) (uuid.UUID, error) {
row := q.db.QueryRowContext(ctx, createPost,
arg.ID,
arg.Namespace,
arg.Slug,
arg.CurrentContentID,
arg.PublishedAt,
)
var id uuid.UUID
err := row.Scan(&id)
return id, err
}
const createPostContent = `-- name: CreatePostContent :one
insert into posts_content (
id,
post_id,
title,
content
) values (
$1,
$2,
$3,
$4
) returning id
`
type CreatePostContentParams struct {
ID uuid.UUID
PostID uuid.UUID
Title string
Content string
}
func (q *Queries) CreatePostContent(ctx context.Context, arg CreatePostContentParams) (uuid.UUID, error) {
row := q.db.QueryRowContext(ctx, createPostContent,
arg.ID,
arg.PostID,
arg.Title,
arg.Content,
)
var id uuid.UUID
err := row.Scan(&id)
return id, err
}
const findPostBySlug = `-- name: FindPostBySlug :one
select
posts.id,
"namespace",
slug,
posts_content.title,
posts_content.content,
published_at
from posts
inner join posts_content
on posts_content.id = posts.current_content_id
where
deleted_at is null
and "namespace" = $1
and slug = $2
`
type FindPostBySlugParams struct {
Namespace string
Slug string
}
type FindPostBySlugRow struct {
ID uuid.UUID
Namespace string
Slug string
Title string
Content string
PublishedAt sql.NullTime
}
func (q *Queries) FindPostBySlug(ctx context.Context, arg FindPostBySlugParams) (FindPostBySlugRow, error) {
row := q.db.QueryRowContext(ctx, findPostBySlug, arg.Namespace, arg.Slug)
var i FindPostBySlugRow
err := row.Scan(
&i.ID,
&i.Namespace,
&i.Slug,
&i.Title,
&i.Content,
&i.PublishedAt,
)
return i, err
}
const listPosts = `-- name: ListPosts :many
select
posts.id,
"namespace",
slug,
posts_content.title,
posts_content.content,
published_at,
posts.created_at,
posts.updated_at
from posts
inner join posts_content
on posts_content.id = posts.current_content_id
where
deleted_at is null
and ($1::boolean or published_at is not null)
and "namespace" = $2
order by posts.created_at desc
limit $4
offset $3
`
type ListPostsParams struct {
IncludeDrafts bool
Namespace string
StartFrom int32
PageSize int32
}
type ListPostsRow struct {
ID uuid.UUID
Namespace string
Slug string
Title string
Content string
PublishedAt sql.NullTime
CreatedAt time.Time
UpdatedAt time.Time
}
func (q *Queries) ListPosts(ctx context.Context, arg ListPostsParams) ([]ListPostsRow, error) {
rows, err := q.db.QueryContext(ctx, listPosts,
arg.IncludeDrafts,
arg.Namespace,
arg.StartFrom,
arg.PageSize,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListPostsRow
for rows.Next() {
var i ListPostsRow
if err := rows.Scan(
&i.ID,
&i.Namespace,
&i.Slug,
&i.Title,
&i.Content,
&i.PublishedAt,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const postsMetadata = `-- name: PostsMetadata :one
select
count(posts.id) as total_size,
$1::boolean as drafts_included
from posts
where
deleted_at is null
and ($1::boolean or published_at is not null)
and "namespace" = $2
`
type PostsMetadataParams struct {
IncludeDrafts bool
Namespace string
}
type PostsMetadataRow struct {
TotalSize int64
DraftsIncluded bool
}
func (q *Queries) PostsMetadata(ctx context.Context, arg PostsMetadataParams) (PostsMetadataRow, error) {
row := q.db.QueryRowContext(ctx, postsMetadata, arg.IncludeDrafts, arg.Namespace)
var i PostsMetadataRow
err := row.Scan(&i.TotalSize, &i.DraftsIncluded)
return i, err
}
const updatePost = `-- name: UpdatePost :one
update posts
set
slug = $1,
current_content_id = $2,
published_at = $3,
updated_at = now()
where
id = $4 and
deleted_at is null
returning id
`
type UpdatePostParams struct {
Slug string
CurrentContentID uuid.UUID
PublishedAt sql.NullTime
ID uuid.UUID
}
func (q *Queries) UpdatePost(ctx context.Context, arg UpdatePostParams) (uuid.UUID, error) {
row := q.db.QueryRowContext(ctx, updatePost,
arg.Slug,
arg.CurrentContentID,
arg.PublishedAt,
arg.ID,
)
var id uuid.UUID
err := row.Scan(&id)
return id, err
}