/
state.go
59 lines (52 loc) · 1.15 KB
/
state.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
package boltdb
import (
"context"
"encoding/json"
"github.com/xescugc/rebost/state"
bolt "go.etcd.io/bbolt"
)
type stateRepository struct {
client *bolt.DB
bucketName []byte
bucket *bolt.Bucket
key []byte
}
// NewStateRepository returns an implementation of the interface state.Repository
func NewStateRepository(c *bolt.DB) (state.Repository, error) {
bn := []byte("state")
if err := createBucket(c, bn); err != nil {
return nil, err
}
return &stateRepository{
client: c,
bucketName: bn,
key: []byte("state"),
}, nil
}
func (r *stateRepository) Find(ctx context.Context) (*state.State, error) {
var s state.State
b := r.bucket.Get(r.key)
if b == nil {
return &state.State{}, nil
}
err := json.Unmarshal(b, &s)
if err != nil {
return nil, err
}
return &s, nil
}
func (r *stateRepository) Update(ctx context.Context, s *state.State) error {
b, err := json.Marshal(s)
if err != nil {
return err
}
return r.bucket.Put(r.key, b)
}
func (r *stateRepository) DeleteAll(ctx context.Context) error {
bk, err := recreateBucket(r.bucket, r.bucketName)
if err != nil {
return err
}
r.bucket = bk
return nil
}