-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
187 lines (159 loc) · 4.58 KB
/
data.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
package main
import (
"database/sql"
"fmt"
"log"
"time"
)
type Parent struct {
Id int64
Content Content
Author string
}
type Post struct {
Id int64
Content Content
Author string
Parent *Parent
ReplyCount int
CreatedAt time.Time
}
func AuthorDisplay(post any) string {
switch post := post.(type) {
case *Post:
return authorDisplay(post.Author)
case Post:
return authorDisplay(post.Author)
case *Parent:
return authorDisplay(post.Author)
case Parent:
return authorDisplay(post.Author)
default:
return "<invalid>"
}
}
func authorDisplay(s string) string {
if s == "" {
return "anonymous"
} else {
return "@" + s
}
}
func QuerySinglePost(db *sql.DB, id int) (post Post, err error) {
row := db.QueryRow(`SELECT c.id,
c.content_markdown,
c.content_html,
coalesce(c.author, ''),
c.children_count,
c.created_at,
(c.parent_id IS NOT NULL AND p.deleted_at IS NULL) as has_parent,
coalesce(p.id, 0),
coalesce(p.content_markdown, ''),
coalesce(p.content_html, ''),
coalesce(p.author, '')
FROM posts c
LEFT JOIN posts p ON p.id = c.parent_id AND p.deleted_at IS NULL
WHERE c.deleted_at IS NULL AND c.id = $1`, id)
var hasParent bool
var parent Parent
err = row.Scan(&post.Id, &post.Content.Markdown, &post.Content.Html, &post.Author, &post.ReplyCount, &post.CreatedAt, &hasParent,
&parent.Id, &parent.Content.Markdown, &parent.Content.Html, &parent.Author)
if err == nil && hasParent {
post.Parent = &parent
}
return
}
func QueryPosts(db *sql.DB) ([]Post, error) {
rows, err := db.Query(`SELECT c.id,
c.content_markdown,
c.content_html,
coalesce(c.author, ''),
c.children_count,
c.created_at,
(c.parent_id IS NOT NULL AND p.deleted_at IS NULL) as has_parent,
coalesce(p.id, 0),
coalesce(p.content_markdown, ''),
coalesce(p.content_html, ''),
coalesce(p.author, '')
FROM posts c
LEFT JOIN posts p ON p.id = c.parent_id AND p.deleted_at IS NULL
WHERE c.deleted_at IS NULL
ORDER BY c.id DESC`)
if err != nil {
return nil, err
}
defer rows.Close()
var result []Post
for rows.Next() {
var post Post
var hasParent bool
var parent Parent
err := rows.Scan(&post.Id, &post.Content.Markdown, &post.Content.Html, &post.Author, &post.ReplyCount, &post.CreatedAt, &hasParent,
&parent.Id, &parent.Content.Markdown, &parent.Content.Html, &parent.Author)
if err != nil {
return nil, fmt.Errorf("failed to read a row: %w", err)
}
if hasParent {
post.Parent = &parent
}
result = append(result, post)
}
return result, nil
}
func QueryChildrenPosts(db *sql.DB, parentPostId int64) ([]Post, error) {
rows, err := db.Query(`SELECT c.id,
c.content_markdown,
c.content_html,
coalesce(c.author, ''),
c.children_count,
c.created_at
FROM posts c
LEFT JOIN posts p ON p.id = c.parent_id AND p.deleted_at IS NULL
WHERE c.deleted_at IS NULL AND c.parent_id = $1
ORDER BY c.id`, parentPostId)
if err != nil {
return nil, err
}
defer rows.Close()
var result []Post
for rows.Next() {
var post Post
err := rows.Scan(&post.Id, &post.Content.Markdown, &post.Content.Html, &post.Author, &post.ReplyCount, &post.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to read a row: %w", err)
}
result = append(result, post)
}
return result, nil
}
func InsertPost(db *sql.DB, content Content, username string, parentPostId *int64, anonymous bool) (int64, error) {
var usernamePtr *string
if !anonymous {
usernamePtr = &username
}
res, err := db.Exec(`INSERT INTO posts (content_markdown, content_html, author, parent_id) VALUES ($1, $2, $3, $4)`, content.Markdown, content.Html, usernamePtr, parentPostId)
if err != nil {
return 0, fmt.Errorf("failed to insert a row: %w", err)
}
if parentPostId != nil {
updateChildCount(db, *parentPostId)
}
return res.LastInsertId()
}
func DeletePost(db *sql.DB, postId int64, username string, isAdmin bool) error {
var parentId *int64
err := db.QueryRow(`UPDATE posts SET deleted_at = CURRENT_TIMESTAMP WHERE deleted_at IS NULL AND id = $1 AND (author = $2 OR $3) RETURNING parent_id`, postId, username, isAdmin).Scan(&parentId)
if err != nil {
return err
}
if parentId != nil {
updateChildCount(db, *parentId)
}
return nil
}
func updateChildCount(db *sql.DB, postId int64) {
_, err := db.Exec(`UPDATE posts SET children_count = (SELECT count(*) FROM posts c WHERE c.deleted_at IS NULL AND c.parent_id = posts.id) WHERE id = $1`, postId)
if err != nil {
log.Printf("Failed to update child count: %s", err)
}
}