-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
231 lines (193 loc) · 5.13 KB
/
client.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package fire
import (
"github.com/256dpi/jsonapi/v2"
"github.com/256dpi/xo"
"github.com/256dpi/fire/coal"
)
// Client wraps a jsonapi.Client to directly interact with models.
type Client struct {
client *jsonapi.Client
}
// NewClient will create and return a new client.
func NewClient(client *jsonapi.Client) *Client {
return &Client{
client: client,
}
}
// List will list the provided models.
func (c *Client) List(model coal.Model, reqs ...jsonapi.Request) ([]coal.Model, *jsonapi.Document, error) {
// list resources
doc, err := c.client.List(c.getType(model), reqs...)
if err != nil {
return nil, doc, c.rewriteError(err)
}
// get listed models
var listedModels []coal.Model
for _, resource := range doc.Data.Many {
model := coal.GetMeta(model).Make()
err = AssignResource(model, resource)
if err != nil {
return nil, doc, err
}
listedModels = append(listedModels, model)
}
return listedModels, doc, nil
}
// Find will find and return the provided model.
func (c *Client) Find(model coal.Model, reqs ...jsonapi.Request) (coal.Model, *jsonapi.Document, error) {
// find resource
doc, err := c.client.Find(c.getType(model), model.ID().Hex(), reqs...)
if err != nil {
return nil, doc, c.rewriteError(err)
}
// get found model
foundModel := coal.GetMeta(model).Make()
err = AssignResource(foundModel, doc.Data.One)
if err != nil {
return nil, doc, err
}
return foundModel, doc, nil
}
// Create will create the provided model and return the created model.
func (c *Client) Create(model coal.Model) (coal.Model, *jsonapi.Document, error) {
// convert model
resource, err := ConvertModel(model)
if err != nil {
return nil, nil, err
}
// unset ID
resource.ID = ""
// create resource
doc, err := c.client.Create(resource)
if err != nil {
return nil, doc, c.rewriteError(err)
}
// get created model
createdModel := coal.GetMeta(model).Make()
err = AssignResource(createdModel, doc.Data.One)
if err != nil {
return nil, doc, err
}
return createdModel, doc, nil
}
// Update will update the provided model and return the updated model.
func (c *Client) Update(model coal.Model) (coal.Model, *jsonapi.Document, error) {
// convert model
resource, err := ConvertModel(model)
if err != nil {
return nil, nil, err
}
// update resource
doc, err := c.client.Update(resource)
if err != nil {
return nil, doc, c.rewriteError(err)
}
// get updated model
updatedModel := coal.GetMeta(model).Make()
err = AssignResource(updatedModel, doc.Data.One)
if err != nil {
return nil, doc, err
}
return updatedModel, doc, nil
}
// Delete will delete the provided model.
func (c *Client) Delete(model coal.Model) error {
// delete resource
err := c.client.Delete(c.getType(model), model.ID().Hex())
if err != nil {
return c.rewriteError(err)
}
return nil
}
func (c *Client) getType(model coal.Model) string {
return coal.GetMeta(model).PluralName
}
func (c *Client) rewriteError(err error) error {
// get error
je, ok := err.(*jsonapi.Error)
if !ok {
return err
}
// check errors
for _, e := range []xo.BaseErr{
ErrAccessDenied,
ErrResourceNotFound,
ErrDocumentNotUnique,
} {
ee := e.Self().(*xo.Err).Err.(*jsonapi.Error)
if ee.Status == je.Status && ee.Detail == je.Detail {
return e.Self()
}
}
return err
}
// ModelClient is model specific client.
type ModelClient[M coal.Model] struct {
*Client
}
// ClientFor creates a model specific client for the specified model using the
// provided generic client.
func ClientFor[M coal.Model](c *Client) *ModelClient[M] {
return &ModelClient[M]{
Client: c,
}
}
// List will return a list of models.
func (c *ModelClient[M]) List(reqs ...jsonapi.Request) ([]M, *jsonapi.Document, error) {
// perform list
var zero M
models, doc, err := c.Client.List(zero, reqs...)
if err != nil {
return nil, doc, err
}
// convert models
list := make([]M, 0, len(models))
for _, m := range models {
list = append(list, m.(M))
}
return list, doc, nil
}
// Find will find and return the model with the provided ID.
func (c *ModelClient[M]) Find(id coal.ID, reqs ...jsonapi.Request) (M, *jsonapi.Document, error) {
// perform find
var zero M
model := coal.GetMeta(zero).Make().(M)
model.GetBase().DocID = id
m, doc, err := c.Client.Find(model, reqs...)
if err != nil {
return zero, doc, err
}
return m.(M), doc, nil
}
// Create will create the provided model and return the created model.
func (c *ModelClient[M]) Create(model M) (M, *jsonapi.Document, error) {
// perform create
m, doc, err := c.Client.Create(model)
if err != nil {
var zero M
return zero, doc, err
}
return m.(M), doc, nil
}
// Update will update the provided model and return the updated model.
func (c *ModelClient[M]) Update(model M) (M, *jsonapi.Document, error) {
// perform update
m, doc, err := c.Client.Update(model)
if err != nil {
var zero M
return zero, doc, err
}
return m.(M), doc, nil
}
// Delete will delete the model with the provided ID.
func (c *ModelClient[M]) Delete(id coal.ID) error {
// perform delete
var zero M
model := coal.GetMeta(zero).Make().(M)
model.GetBase().DocID = id
err := c.Client.Delete(model)
if err != nil {
return err
}
return nil
}