-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
comment.go
217 lines (184 loc) · 3.91 KB
/
comment.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
package graphql
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
)
// Comment is a comment on a post.
type Comment struct {
ID string `json:"id"`
Post *Post `json:"post"`
User *User `json:"author"`
Content string `json:"content"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
// IsLinkable tells gqlgen this model has a URI function.
func (Comment) IsLinkable() {}
// URI returns the URI for this comment.
func (c *Comment) URI() *URI {
return NewURI("https://writing.natwelch.com/comment/" + c.ID)
}
func (c *Comment) GetURI() URI {
return *c.URI()
}
// AllComments returns all comments orderd by time.
func AllComments(ctx context.Context, limit int, offset int) ([]*Comment, error) {
rows, err := db.QueryContext(
ctx, `
SELECT id, user_id, post_id, content, created_at, modified_at
FROM comments
ORDER BY created_at DESC
LIMIT $1 OFFSET $2
`, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
comments := make([]*Comment, 0)
for rows.Next() {
c := &Comment{}
var userID string
var p int64
err := rows.Scan(
&c.ID,
&userID,
&p,
&c.Content,
&c.Created,
&c.Modified,
)
if err != nil {
return nil, err
}
c.User, err = GetUser(ctx, userID)
if err != nil {
return nil, err
}
c.Post, err = GetPost(ctx, p)
if err != nil {
return nil, err
}
comments = append(comments, c)
}
if err = rows.Err(); err != nil {
return nil, err
}
return comments, nil
}
// GetComment returns a single comment by ID.
func GetComment(ctx context.Context, id string) (*Comment, error) {
c := &Comment{}
row := db.QueryRowContext(
ctx, `
SELECT id, post_id, user_id, content, created_at, modified_at
FROM comments
WHERE id = $1
`, id)
var postID, userID string
err := row.Scan(
&c.ID,
&postID,
&userID,
&c.Content,
&c.Created,
&c.Modified,
)
if err != nil {
return nil, err
}
c.User, err = GetUser(ctx, userID)
if err != nil {
return nil, err
}
c.Post, err = GetPostString(ctx, postID)
if err != nil {
return nil, err
}
return c, nil
}
// PostComments returns comments for a post ID.
func PostComments(ctx context.Context, p string, limit int, offset int) ([]*Comment, error) {
if p == "" {
return nil, fmt.Errorf("no post specified")
}
rows, err := db.QueryContext(
ctx, `
SELECT id, user_id, content, created_at, modified_at
FROM comments
WHERE post_id = $1
ORDER BY created_at ASC
LIMIT $2 OFFSET $3
`, p, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
comments := make([]*Comment, 0)
for rows.Next() {
c := &Comment{}
var userID string
err := rows.Scan(
&c.ID,
&userID,
&c.Content,
&c.Created,
&c.Modified,
)
if err != nil {
return nil, err
}
c.User, err = GetUser(ctx, userID)
if err != nil {
return nil, err
}
c.Post, err = GetPostString(ctx, p)
if err != nil {
return nil, err
}
comments = append(comments, c)
}
if err = rows.Err(); err != nil {
return nil, err
}
return comments, nil
}
// Save adds the comment to the database and checks that no data is missing.
func (c *Comment) Save(ctx context.Context) error {
if c.ID == "" {
uuid, err := uuid.NewRandom()
if err != nil {
return err
}
c.ID = uuid.String()
}
if c.Created.IsZero() {
c.Created = time.Now()
}
c.Modified = time.Now()
if c.Post == nil {
return fmt.Errorf("post cannot be nil")
}
if c.User == nil {
return fmt.Errorf("user cannot be nil")
}
if _, err := db.ExecContext(
ctx,
`
INSERT INTO comments(id, post_id, user_id, content, created_at, modified_at)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id) DO UPDATE
SET (post_id, user_id, content, created_at, modified_at) = ($2, $3, $4, $5, $6)
WHERE comments.id = $1;
`,
c.ID,
c.Post.ID,
c.User.ID,
c.Content,
c.Created,
c.Modified); err != nil {
return err
}
return nil
}