-
Notifications
You must be signed in to change notification settings - Fork 26
/
comment.go
81 lines (76 loc) · 1.47 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
package main
type Comment struct {
ID int
UserID int
PostID int
Title string
Body string
}
func InsertComment(comment *Comment) error {
var id int
err := db.QueryRow(`
INSERT INTO comments(user_id, post_id, title, body)
VALUES ($1, $2, $3, $4)
RETURNING id
`, comment.UserID, comment.PostID, comment.Title, comment.Body).Scan(&id)
if err != nil {
return err
}
comment.ID = id
return nil
}
func RemoveCommentByID(id int) error {
_, err := db.Exec("DELETE FROM comments WHERE id=$1", id)
return err
}
func GetCommentByIDAndPost(id int, postID int) (*Comment, error) {
var (
userID int
title, body string
)
err := db.QueryRow(`
SELECT user_id, title, body
FROM posts
WHERE id=$1
AND post_id=$2
`, id, postID).Scan(&userID, &title, &body)
if err != nil {
return nil, err
}
return &Comment{
ID: id,
UserID: userID,
PostID: postID,
Title: title,
Body: body,
}, nil
}
func GetCommentsForPost(id int) ([]*Comment, error) {
rows, err := db.Query(`
SELECT c.id, c.user_id, c.title, c.body
FROM comments AS c
WHERE c.post_id=$1
`, id)
if err != nil {
return nil, err
}
defer rows.Close()
var (
comments = []*Comment{}
cid, userID int
title, body string
)
for rows.Next() {
if err = rows.Scan(&cid, &userID, &title, &body); err != nil {
return nil, err
}
comments = append(comments, &Comment{
ID: cid,
UserID: userID,
PostID: id,
Title: title,
Body: body,
})
}
return comments, nil
}