-
Notifications
You must be signed in to change notification settings - Fork 69
/
storage.go
159 lines (148 loc) · 4.22 KB
/
storage.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package cellar
import (
"context"
"log"
"strings"
"github.com/boltdb/bolt"
storage "goa.design/examples/cellar/gen/storage"
)
// storage service example implementation.
// The example methods log the requests and return zero values.
type storageSvc struct {
db *Bolt
logger *log.Logger
}
// NewStorage returns the storage service implementation.
func NewStorage(db *bolt.DB, logger *log.Logger) (storage.Service, error) {
// Setup database
bolt, err := NewBoltDB(db)
if err != nil {
return nil, err
}
// Build and return service implementation.
return &storageSvc{bolt, logger}, nil
}
// List all stored bottles
func (s *storageSvc) List(ctx context.Context) (res storage.StoredBottleCollection, err error) {
if err = s.db.LoadAll("CELLAR", &res); err != nil {
return nil, err // internal error
}
return res, nil
}
// Show bottle by ID
func (s *storageSvc) Show(ctx context.Context, p *storage.ShowPayload) (res *storage.StoredBottle, view string, err error) {
if p.View != nil {
view = *p.View
} else {
view = "default"
}
if err = s.db.Load("CELLAR", p.ID, &res); err != nil {
if err == ErrNotFound {
return nil, view, &storage.NotFound{
Message: err.Error(),
ID: p.ID,
}
}
return nil, view, err // internal error
}
return res, view, nil
}
// Add new bottle and return its ID.
func (s *storageSvc) Add(ctx context.Context, p *storage.Bottle) (res string, err error) {
res, err = s.db.NewID("CELLAR")
if err != nil {
return "", err // internal error
}
sb := storage.StoredBottle{
ID: res,
Name: p.Name,
Winery: p.Winery,
Vintage: p.Vintage,
Composition: p.Composition,
Description: p.Description,
Rating: p.Rating,
}
if err = s.db.Save("CELLAR", res, &sb); err != nil {
return "", err // internal error
}
return res, nil
}
// Remove bottle from storage
func (s *storageSvc) Remove(ctx context.Context, p *storage.RemovePayload) (err error) {
return s.db.Delete("CELLAR", p.ID) // internal error if not nil
}
// Rate bottles by IDs
func (s *storageSvc) Rate(ctx context.Context, p map[uint32][]string) (err error) {
for rating, ids := range p {
for _, id := range ids {
var b storage.StoredBottle
if err = s.db.Load("CELLAR", id, &b); err != nil {
if err == ErrNotFound {
continue
}
}
sb := storage.StoredBottle{
ID: id,
Name: b.Name,
Winery: b.Winery,
Vintage: b.Vintage,
Composition: b.Composition,
Description: b.Description,
Rating: &rating,
}
if err = s.db.Save("CELLAR", id, &sb); err != nil {
return err // internal error
}
}
}
return nil
}
// Add n number of bottles and return their IDs. This is a multipart request
// and each part has field name 'bottle' and contains the encoded bottle info
// to be added.
func (s *storageSvc) MultiAdd(ctx context.Context, p []*storage.Bottle) (res []string, err error) {
res = make([]string, 0, len(p))
for _, bottle := range p {
id, err := s.db.NewID("CELLAR")
if err != nil {
return nil, err // internal error
}
sb := storage.StoredBottle{
ID: id,
Name: bottle.Name,
Winery: bottle.Winery,
Vintage: bottle.Vintage,
Composition: bottle.Composition,
Description: bottle.Description,
Rating: bottle.Rating,
}
if err = s.db.Save("CELLAR", id, &sb); err != nil {
return nil, err // internal error
}
res = append(res, id)
}
return res, nil
}
// Update bottles with the given IDs. This is a multipart request and each part
// has field name 'bottle' and contains the encoded bottle info to be updated.
// The IDs in the query parameter is mapped to each part in the request.
func (s *storageSvc) MultiUpdate(ctx context.Context, p *storage.MultiUpdatePayload) error {
for _, id := range p.Ids {
for _, bottle := range p.Bottles {
sb := storage.StoredBottle{
ID: id,
Name: bottle.Name,
Winery: bottle.Winery,
Vintage: bottle.Vintage,
Composition: bottle.Composition,
Description: bottle.Description,
Rating: bottle.Rating,
}
if err := s.db.Save("CELLAR", id, &sb); err != nil {
return err // internal error
}
}
}
s.logger.Printf("Updated bottles: %s", strings.Join(p.Ids, ", "))
return nil
}