-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepo.go
43 lines (37 loc) · 914 Bytes
/
repo.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 priv
import (
"database/sql"
)
type Repository interface {
Account() Account
Catalog(name string) Catalog
ListCatalogs() ([]Catalog, error)
}
type RepositoryImpl struct {
db *sql.DB
account *AccountImpl
}
func (repo *RepositoryImpl) Account() Account {
return repo.account
}
func (repo *RepositoryImpl) Catalog(name string) Catalog {
return &CatalogImpl{db: repo.db, repo: repo, name: name}
}
func (repo *RepositoryImpl) ListCatalogs() ([]Catalog, error) {
rows, err := repo.db.Query(`SELECT id, name FROM catalog WHERE account_id = $1 ORDER BY name`, repo.account.id)
if err != nil {
return nil, err
}
defer rows.Close()
var id int
var name string
var catalogs []Catalog
for rows.Next() {
err = rows.Scan(&id, &name)
if err != nil {
return nil, err
}
catalogs = append(catalogs, &CatalogImpl{db: repo.db, repo: repo, id: id, name: name})
}
return catalogs, nil
}