-
Notifications
You must be signed in to change notification settings - Fork 1
/
blog.go
162 lines (135 loc) · 2.61 KB
/
blog.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
package storage
import (
"context"
"github.com/theandrew168/bloggulus"
"github.com/theandrew168/bloggulus/internal/database"
)
type Blog struct {
db database.Conn
}
func NewBlog(db database.Conn) *Blog {
s := Blog{
db: db,
}
return &s
}
func (s *Blog) Create(blog *bloggulus.Blog) error {
stmt := `
INSERT INTO blog
(feed_url, site_url, title, etag, last_modified)
VALUES
($1, $2, $3, $4, $5)
RETURNING id`
args := []interface{}{
blog.FeedURL,
blog.SiteURL,
blog.Title,
blog.ETag,
blog.LastModified,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
row := s.db.QueryRow(ctx, stmt, args...)
err := database.Scan(row, &blog.ID)
if err != nil {
return err
}
return nil
}
func (s *Blog) Read(id int) (bloggulus.Blog, error) {
stmt := `
SELECT
id,
feed_url,
site_url,
title,
etag,
last_modified
FROM blog
WHERE id = $1`
var blog bloggulus.Blog
dest := []interface{}{
&blog.ID,
&blog.FeedURL,
&blog.SiteURL,
&blog.Title,
&blog.ETag,
&blog.LastModified,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
row := s.db.QueryRow(ctx, stmt, id)
err := database.Scan(row, dest...)
if err != nil {
return bloggulus.Blog{}, err
}
return blog, nil
}
func (s *Blog) ReadAll(limit, offset int) ([]bloggulus.Blog, error) {
stmt := `
SELECT
id,
feed_url,
site_url,
title,
etag,
last_modified
FROM blog
ORDER BY title ASC
LIMIT $1 OFFSET $2`
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
rows, err := s.db.Query(ctx, stmt, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
// use "make" here to encode JSON as an empty array instead of null
blogs := make([]bloggulus.Blog, 0)
for rows.Next() {
var blog bloggulus.Blog
dest := []interface{}{
&blog.ID,
&blog.FeedURL,
&blog.SiteURL,
&blog.Title,
&blog.ETag,
&blog.LastModified,
}
err := database.Scan(rows, dest...)
if err != nil {
return nil, err
}
blogs = append(blogs, blog)
}
if rows.Err() != nil {
return nil, rows.Err()
}
return blogs, nil
}
func (s *Blog) Update(blog bloggulus.Blog) error {
stmt := `
UPDATE blog
SET
feed_url = $2,
site_url = $3,
title = $4,
etag = $5,
last_modified = $6
WHERE id = $1`
args := []interface{}{
blog.ID,
blog.FeedURL,
blog.SiteURL,
blog.Title,
blog.ETag,
blog.LastModified,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
_, err := s.db.Exec(ctx, stmt, args...)
if err != nil {
return err
}
return nil
}