-
Notifications
You must be signed in to change notification settings - Fork 26
/
post.go
91 lines (85 loc) · 1.57 KB
/
post.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
package main
type Post struct {
ID int
UserID int
Title string
Body string
}
func InsertPost(post *Post) error {
var id int
err := db.QueryRow(`
INSERT INTO posts(user_id, title, body)
VALUES ($1, $2, $3)
RETURNING id
`, post.UserID, post.Title, post.Body).Scan(&id)
if err != nil {
return err
}
post.ID = id
return nil
}
func RemovePostByID(id int) error {
_, err := db.Exec("DELETE FROM posts WHERE id=$1", id)
return err
}
func GetPostByID(id int) (*Post, error) {
var (
userID int
title, body string
)
err := db.QueryRow(`
SELECT user_id, title, body
FROM posts
WHERE id=$1
`, id).Scan(&userID, &title, &body)
if err != nil {
return nil, err
}
return &Post{
ID: id,
UserID: userID,
Title: title,
Body: body,
}, nil
}
func GetPostByIDAndUser(id, userID int) (*Post, error) {
var title, body string
err := db.QueryRow(`
SELECT title, body
FROM posts
WHERE id=$1
AND user_id=$2
`, id, userID).Scan(&title, &body)
if err != nil {
return nil, err
}
return &Post{
ID: id,
UserID: userID,
Title: title,
Body: body,
}, nil
}
func GetPostsForUser(id int) ([]*Post, error) {
rows, err := db.Query(`
SELECT p.id, p.title, p.body
FROM posts AS p
WHERE p.user_id=$1
`, id)
if err != nil {
return nil, err
}
defer rows.Close()
var (
posts = []*Post{}
pid int
title, body string
)
for rows.Next() {
if err = rows.Scan(&pid, &title, &body); err != nil {
return nil, err
}
posts = append(posts, &Post{ID: id, UserID: id, Title: title, Body: body})
}
return posts, nil
}