forked from kabukky/journey
/
retrieval.go
398 lines (372 loc) · 11.7 KB
/
retrieval.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package database
import (
"database/sql"
"encoding/json"
"github.com/kabukky/journey/structure"
"time"
)
const stmtRetrievePostsCount = "SELECT count(*) FROM posts WHERE page = 0 AND status = 'published'"
const stmtRetrievePostsCountByUser = "SELECT count(*) FROM posts WHERE page = 0 AND status = 'published' AND author_id = ?"
const stmtRetrievePostsCountByTag = "SELECT count(*) FROM posts, posts_tags WHERE posts_tags.post_id = posts.id AND posts_tags.tag_id = ? AND page = 0 AND status = 'published'"
const stmtRetrievePostsForIndex = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, image, author_id, published_at FROM posts WHERE page = 0 AND status = 'published' ORDER BY published_at DESC LIMIT ? OFFSET ?"
const stmtRetrievePostsForApi = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, image, author_id, published_at FROM posts ORDER BY id DESC LIMIT ? OFFSET ?"
const stmtRetrievePostsByUser = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, image, author_id, published_at FROM posts WHERE page = 0 AND status = 'published' AND author_id = ? ORDER BY published_at DESC LIMIT ? OFFSET ?"
const stmtRetrievePostsByTag = "SELECT posts.id, posts.uuid, posts.title, posts.slug, posts.markdown, posts.html, posts.featured, posts.page, posts.status, posts.image, posts.author_id, posts.published_at FROM posts, posts_tags WHERE posts_tags.post_id = posts.id AND posts_tags.tag_id = ? AND page = 0 AND status = 'published' ORDER BY posts.published_at DESC LIMIT ? OFFSET ?"
const stmtRetrievePostById = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, image, author_id, published_at FROM posts WHERE id = ?"
const stmtRetrievePostBySlug = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, image, author_id, published_at FROM posts WHERE slug = ?"
const stmtRetrieveUserById = "SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE id = ?"
const stmtRetrieveUserBySlug = "SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE slug = ?"
const stmtRetrieveUserByName = "SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE name = ?"
const stmtRetrieveTags = "SELECT tag_id FROM posts_tags WHERE post_id = ?"
const stmtRetrieveTagById = "SELECT id, name, slug FROM tags WHERE id = ?"
const stmtRetrieveTagBySlug = "SELECT id, name, slug FROM tags WHERE slug = ?"
const stmtRetrieveTagIdBySlug = "SELECT id FROM tags WHERE slug = ?"
const stmtRetrieveHashedPasswordByName = "SELECT password FROM users WHERE name = ?"
const stmtRetrieveUsersCount = "SELECT count(*) FROM users"
const stmtRetrieveBlog = "SELECT value FROM settings WHERE key = ?"
const stmtRetrievePostCreationDateById = "SELECT created_at FROM posts WHERE id = ?"
func RetrievePostById(id int64) (*structure.Post, error) {
// Retrieve post
row := readDB.QueryRow(stmtRetrievePostById, id)
return extractPost(row)
}
func RetrievePostBySlug(slug string) (*structure.Post, error) {
// Retrieve post
row := readDB.QueryRow(stmtRetrievePostBySlug, slug)
return extractPost(row)
}
func RetrievePostsByUser(user_id int64, limit int64, offset int64) ([]structure.Post, error) {
// Retrieve posts
rows, err := readDB.Query(stmtRetrievePostsByUser, user_id, limit, offset)
defer rows.Close()
if err != nil {
return nil, err
}
posts, err := extractPosts(rows)
if err != nil {
return nil, err
}
return *posts, nil
}
func RetrievePostsByTag(tag_id int64, limit int64, offset int64) ([]structure.Post, error) {
// Retrieve posts
rows, err := readDB.Query(stmtRetrievePostsByTag, tag_id, limit, offset)
defer rows.Close()
if err != nil {
return nil, err
}
posts, err := extractPosts(rows)
if err != nil {
return nil, err
}
return *posts, nil
}
func RetrievePostsForIndex(limit int64, offset int64) ([]structure.Post, error) {
// Retrieve posts
rows, err := readDB.Query(stmtRetrievePostsForIndex, limit, offset)
defer rows.Close()
if err != nil {
return nil, err
}
posts, err := extractPosts(rows)
if err != nil {
return nil, err
}
return *posts, nil
}
func RetrievePostsForApi(limit int64, offset int64) ([]structure.Post, error) {
// Retrieve posts
rows, err := readDB.Query(stmtRetrievePostsForApi, limit, offset)
defer rows.Close()
if err != nil {
return nil, err
}
posts, err := extractPosts(rows)
if err != nil {
return nil, err
}
return *posts, nil
}
func extractPosts(rows *sql.Rows) (*[]structure.Post, error) {
posts := make([]structure.Post, 0)
for rows.Next() {
post := structure.Post{}
var userId int64
var status string
err := rows.Scan(&post.Id, &post.Uuid, &post.Title, &post.Slug, &post.Markdown, &post.Html, &post.IsFeatured, &post.IsPage, &status, &post.Image, &userId, &post.Date)
if err != nil {
return nil, err
}
// If there was no publication date attached to the post, make its creation date the date of the post
if post.Date == nil {
post.Date, err = retrievePostCreationDateById(post.Id)
if err != nil {
return nil, err
}
}
// Evaluate status
if status == "published" {
post.IsPublished = true
} else {
post.IsPublished = false
}
// Retrieve user
post.Author, err = RetrieveUser(userId)
if err != nil {
return nil, err
}
// Retrieve tags
post.Tags, err = RetrieveTags(post.Id)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return &posts, nil
}
func extractPost(row *sql.Row) (*structure.Post, error) {
post := structure.Post{}
var userId int64
var status string
err := row.Scan(&post.Id, &post.Uuid, &post.Title, &post.Slug, &post.Markdown, &post.Html, &post.IsFeatured, &post.IsPage, &status, &post.Image, &userId, &post.Date)
if err != nil {
return nil, err
}
// If there was no publication date attached to the post, make its creation date the date of the post
if post.Date == nil {
post.Date, err = retrievePostCreationDateById(post.Id)
if err != nil {
return nil, err
}
}
// Evaluate status
if status == "published" {
post.IsPublished = true
} else {
post.IsPublished = false
}
// Retrieve user
post.Author, err = RetrieveUser(userId)
if err != nil {
return nil, err
}
// Retrieve tags
post.Tags, err = RetrieveTags(post.Id)
if err != nil {
return nil, err
}
return &post, nil
}
func RetrieveNumberOfPosts() (int64, error) {
var count int64
// Retrieve number of posts
row := readDB.QueryRow(stmtRetrievePostsCount)
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func RetrieveNumberOfPostsByUser(user_id int64) (int64, error) {
var count int64
// Retrieve number of posts
row := readDB.QueryRow(stmtRetrievePostsCountByUser, user_id)
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func RetrieveNumberOfPostsByTag(tag_id int64) (int64, error) {
var count int64
// Retrieve number of posts
row := readDB.QueryRow(stmtRetrievePostsCountByTag, tag_id)
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func retrievePostCreationDateById(post_id int64) (*time.Time, error) {
var date time.Time
// Retrieve number of posts
row := readDB.QueryRow(stmtRetrievePostCreationDateById, post_id)
err := row.Scan(&date)
if err != nil {
return &date, err
}
return &date, nil
}
func RetrieveUser(id int64) (*structure.User, error) {
user := structure.User{}
// Retrieve user
row := readDB.QueryRow(stmtRetrieveUserById, id)
err := row.Scan(&user.Id, &user.Name, &user.Slug, &user.Email, &user.Image, &user.Cover, &user.Bio, &user.Website, &user.Location)
if err != nil {
return nil, err
}
return &user, nil
}
func RetrieveUserBySlug(slug string) (*structure.User, error) {
user := structure.User{}
// Retrieve user
row := readDB.QueryRow(stmtRetrieveUserBySlug, slug)
err := row.Scan(&user.Id, &user.Name, &user.Slug, &user.Email, &user.Image, &user.Cover, &user.Bio, &user.Website, &user.Location)
if err != nil {
return nil, err
}
return &user, nil
}
func RetrieveUserByName(name []byte) (*structure.User, error) {
user := structure.User{}
// Retrieve user
row := readDB.QueryRow(stmtRetrieveUserByName, name)
err := row.Scan(&user.Id, &user.Name, &user.Slug, &user.Email, &user.Image, &user.Cover, &user.Bio, &user.Website, &user.Location)
if err != nil {
return nil, err
}
return &user, nil
}
func RetrieveTags(postId int64) ([]structure.Tag, error) {
tags := make([]structure.Tag, 0)
// Retrieve tags
rows, err := readDB.Query(stmtRetrieveTags, postId)
defer rows.Close()
if err != nil {
return nil, err
}
for rows.Next() {
var tagId int64
err := rows.Scan(&tagId)
if err != nil {
return nil, err
}
tag, err := RetrieveTag(tagId)
// TODO: Error while receiving individual tag is ignored right now. Keep it this way?
if err == nil {
tags = append(tags, *tag)
}
}
return tags, nil
}
func RetrieveTag(tagId int64) (*structure.Tag, error) {
tag := structure.Tag{}
// Retrieve tag
row := readDB.QueryRow(stmtRetrieveTagById, tagId)
err := row.Scan(&tag.Id, &tag.Name, &tag.Slug)
if err != nil {
return nil, err
}
return &tag, nil
}
func RetrieveTagBySlug(slug string) (*structure.Tag, error) {
tag := structure.Tag{}
// Retrieve tag
row := readDB.QueryRow(stmtRetrieveTagBySlug, slug)
err := row.Scan(&tag.Id, &tag.Name, &tag.Slug)
if err != nil {
return nil, err
}
return &tag, nil
}
func RetrieveTagIdBySlug(slug string) (int64, error) {
var id int64
row := readDB.QueryRow(stmtRetrieveTagIdBySlug, slug)
err := row.Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
func RetrieveHashedPasswordForUser(name []byte) ([]byte, error) {
var hashedPassword []byte
row := readDB.QueryRow(stmtRetrieveHashedPasswordByName, name)
err := row.Scan(&hashedPassword)
if err != nil {
return []byte{}, err
}
return hashedPassword, nil
}
func RetrieveBlog() (*structure.Blog, error) {
tempBlog := structure.Blog{}
// Title
row := readDB.QueryRow(stmtRetrieveBlog, "title")
err := row.Scan(&tempBlog.Title)
if err != nil {
return &tempBlog, err
}
// Description
row = readDB.QueryRow(stmtRetrieveBlog, "description")
err = row.Scan(&tempBlog.Description)
if err != nil {
return &tempBlog, err
}
// Logo
row = readDB.QueryRow(stmtRetrieveBlog, "logo")
err = row.Scan(&tempBlog.Logo)
if err != nil {
return &tempBlog, err
}
// Cover
row = readDB.QueryRow(stmtRetrieveBlog, "cover")
err = row.Scan(&tempBlog.Cover)
if err != nil {
return &tempBlog, err
}
// PostsPerPage
row = readDB.QueryRow(stmtRetrieveBlog, "postsPerPage")
err = row.Scan(&tempBlog.PostsPerPage)
if err != nil {
return &tempBlog, err
}
// ActiveTheme
row = readDB.QueryRow(stmtRetrieveBlog, "activeTheme")
err = row.Scan(&tempBlog.ActiveTheme)
if err != nil {
return &tempBlog, err
}
// Post count
postCount, err := RetrieveNumberOfPosts()
if err != nil {
return &tempBlog, err
}
tempBlog.PostCount = postCount
// Navigation
var navigation []byte
row = readDB.QueryRow(stmtRetrieveBlog, "navigation")
err = row.Scan(&navigation)
if err != nil {
return &tempBlog, err
}
tempBlog.NavigationItems, err = makeNavigation(navigation)
if err != nil {
return &tempBlog, err
}
return &tempBlog, err
}
func RetrieveActiveTheme() (*string, error) {
var activeTheme string
row := readDB.QueryRow(stmtRetrieveBlog, "activeTheme")
err := row.Scan(&activeTheme)
if err != nil {
return &activeTheme, err
}
return &activeTheme, nil
}
func RetrieveUsersCount() int {
userCount := -1
row := readDB.QueryRow(stmtRetrieveUsersCount)
err := row.Scan(&userCount)
if err != nil {
return -1
}
return userCount
}
func makeNavigation(navigation []byte) ([]structure.Navigation, error) {
navigationItems := make([]structure.Navigation, 0)
err := json.Unmarshal(navigation, &navigationItems)
if err != nil {
return navigationItems, err
}
return navigationItems, nil
}