-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
43 lines (35 loc) · 793 Bytes
/
store.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
package main
import (
"database/sql"
)
type Store interface {
CreateBird(bird *Bird) error
GetBirds() ([]*Bird, error)
}
type dbStore struct {
db *sql.DB
}
func (store *dbStore) CreateBird(bird *Bird) error {
_, err := store.db.Query("INSERT INTO birds (species, description) VALUES ($1, $2)", bird.Species, bird.Description)
return err
}
func (store *dbStore) GetBirds() ([]*Bird, error) {
rows, err := store.db.Query("SELECT species, description from birds")
if err != nil {
return nil, err
}
defer rows.Close()
birds := []*Bird{}
for rows.Next() {
bird := &Bird{}
if err := rows.Scan(&bird.Species, &bird.Description); err != nil {
return nil, err
}
birds = append(birds, bird)
}
return birds, nil
}
var store Store
func InitStore(s Store) {
store = s
}