-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.go
45 lines (38 loc) · 895 Bytes
/
toc.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
package service
import (
bolt "github.com/etcd-io/bbolt"
"github.com/peter-mount/go-kernel/v2/rest"
"github.com/peter-mount/nre-feeds/darwinref"
)
func (dr *DarwinRefService) TocHandler(r *rest.Rest) error {
return dr.reference.View(func(tx *bolt.Tx) error {
id := r.Var("id")
if toc, exists := dr.reference.GetToc(tx, id); exists {
r.Status(200).
JSON().
Value(toc)
} else {
r.Status(404)
}
return nil
})
}
func (dr *DarwinRefService) AllTocsHandler(r *rest.Rest) error {
return dr.reference.View(func(tx *bolt.Tx) error {
resp := &darwinref.TocsResponse{}
if err := tx.Bucket([]byte("DarwinToc")).ForEach(func(k, v []byte) error {
toc := &darwinref.Toc{}
if toc.FromBytes(v) {
resp.Toc = append(resp.Toc, toc)
}
return nil
}); err != nil {
return err
} else {
r.Status(200).
JSON().
Value(resp)
}
return nil
})
}