-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlink.go
126 lines (109 loc) · 3.11 KB
/
link.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
package graphql
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/lib/pq"
)
// Link is a link I have save on pinboard or a link in a post.
type Link struct {
ID string `json:"id"`
Title string `json:"title"`
URI URI `json:"uri"`
Created time.Time `json:"created"`
Description string `json:"description"`
Screenshot URI `json:"screenshot"`
Tags []string `json:"tags"`
Modified time.Time `json:"modified"`
}
func (l *Link) GetURI() URI {
return l.URI
}
// Save inserts or updates a link into the database.
func (l *Link) Save(ctx context.Context) error {
if l.Created.IsZero() {
l.Created = time.Now()
}
l.Modified = time.Now()
if _, err := db.ExecContext(
ctx,
`
INSERT INTO links(title, uri, description, created, created_at, modified_at, tags)
VALUES ($1, $2, $3, $4, $6, $6, $5)
ON CONFLICT (uri) DO UPDATE
SET (title, description, created, modified_at, tags) = ($1, $3, $4, $6, $5)
WHERE links.uri = $2;
`,
l.Title,
l.URI,
l.Description,
l.Created,
pq.Array(l.Tags),
time.Now(),
); err != nil {
return err
}
return nil
}
// IsLinkable exists to show that this method implements the Linkable type in
// graphql.
func (l *Link) IsLinkable() {}
// GetLinkByURI gets a link by uri from the database.
func GetLinkByURI(ctx context.Context, uri string) (*Link, error) {
var link Link
row := db.QueryRowContext(ctx, "SELECT id, title, uri, description, created, modified_at, tags FROM links WHERE uri = $1", uri)
err := row.Scan(&link.ID, &link.Title, &link.URI, &link.Description, &link.Created, &link.Modified, pq.Array(&link.Tags))
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("no link %q", uri)
case err != nil:
return nil, fmt.Errorf("error with get: %w", err)
default:
return &link, nil
}
}
// GetLinkByID gets a link by id from the database.
func GetLinkByID(ctx context.Context, id string) (*Link, error) {
var link Link
row := db.QueryRowContext(ctx, "SELECT id, title, uri, description, created, modified_at, tags FROM links WHERE id = $1", id)
err := row.Scan(&link.ID, &link.Title, &link.URI, &link.Description, &link.Created, &link.Modified, pq.Array(&link.Tags))
switch {
case err == sql.ErrNoRows:
return nil, fmt.Errorf("no link %q", id)
case err != nil:
return nil, fmt.Errorf("error with get: %w", err)
default:
return &link, nil
}
}
// GetLinks returns all links from the database.
func GetLinks(ctx context.Context, limit int, offset int) ([]*Link, error) {
rows, err := db.QueryContext(ctx, `
SELECT id, title, uri, description, created, modified_at, tags
FROM links
ORDER BY created DESC
LIMIT $1 OFFSET $2`,
limit,
offset)
if err != nil {
return nil, err
}
defer rows.Close()
links := make([]*Link, 0)
for rows.Next() {
link := new(Link)
err := rows.Scan(&link.ID, &link.Title, &link.URI, &link.Description, &link.Created, &link.Modified, pq.Array(&link.Tags))
if err != nil {
return nil, err
}
if link.Created.IsZero() {
link.Created = time.Now()
}
links = append(links, link)
}
if err = rows.Err(); err != nil {
return nil, err
}
return links, nil
}