-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
book.go
105 lines (88 loc) · 2.03 KB
/
book.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
package graphql
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
)
// Book is a book on Goodreads.
type Book struct {
ID string `json:"id"`
GoodreadsID string
Title string `json:"title"`
Link string
Authors []string
ISBN string
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
}
// IsLinkable exists to show that this method implements the Linkable type in
// graphql.
func (Book) IsLinkable() {}
// Save inserts or updates a book into the database.
func (b *Book) Save(ctx context.Context) error {
if b.ID == "" {
uuid, err := uuid.NewRandom()
if err != nil {
return err
}
b.ID = uuid.String()
}
if b.Created.IsZero() {
b.Created = time.Now()
}
b.Modified = time.Now()
if _, err := db.ExecContext(
ctx,
`
INSERT INTO books(id, title, goodreads_id, created_at, modified_at)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (id) DO UPDATE
SET (title, goodreads_id, created_at, modified_at) = ($2, $3, $4, $5)
WHERE books.id = $1;
`,
b.ID,
b.Title,
b.GoodreadsID,
b.Created,
b.Modified); err != nil {
return err
}
return nil
}
// URI returns an absolute link to this book.
func (b *Book) URI() *URI {
return NewURI(fmt.Sprintf("https://www.goodreads.com/book/show/%s", b.GoodreadsID))
}
func (b *Book) GetURI() URI {
return *b.URI()
}
// GetBooks returns all books from the database.
func GetBooks(ctx context.Context, limit int, offset int) ([]*Book, error) {
rows, err := db.QueryContext(ctx, `
SELECT id, title, goodreads_id, created_at, modified_at
FROM books
ORDER BY modified_at
LIMIT $1 OFFSET $2`,
limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
books := make([]*Book, 0)
for rows.Next() {
book := new(Book)
err := rows.Scan(&book.ID, &book.Title, &book.GoodreadsID, &book.Created, &book.Modified)
if err != nil {
return nil, err
}
if book.Created.IsZero() {
book.Created = time.Now()
}
books = append(books, book)
}
if err = rows.Err(); err != nil {
return nil, err
}
return books, nil
}