-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.go
More file actions
227 lines (206 loc) · 7.1 KB
/
insert.go
File metadata and controls
227 lines (206 loc) · 7.1 KB
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
package jsonapi
import (
"context"
"net/http"
"github.com/neuronlabs/neuron-extensions/codec/jsonapi"
"github.com/neuronlabs/neuron-extensions/server/http/httputil"
"github.com/neuronlabs/neuron-extensions/server/http/log"
"github.com/neuronlabs/neuron/codec"
"github.com/neuronlabs/neuron/database"
"github.com/neuronlabs/neuron/mapping"
"github.com/neuronlabs/neuron/query"
"github.com/neuronlabs/neuron/server"
)
// HandleInsert handles json:api post endpoint for the 'model'. Panics if the model is not mapped for given API controller.
func (a *API) HandleInsert(model mapping.Model) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
a.handleInsert(a.Controller.MustModelStruct(model))(rw, req)
}
}
func (a *API) handleInsert(mStruct *mapping.ModelStruct) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
// unmarshal the input from the request body.
pu := jsonapi.GetCodec(a.Controller).(codec.PayloadUnmarshaler)
payload, err := pu.UnmarshalPayload(req.Body, codec.UnmarshalOptions{StrictUnmarshal: a.Options.StrictUnmarshal, ModelStruct: mStruct})
if err != nil {
log.Debugf("Unmarshal scope for: '%s' failed: %v", mStruct.Collection(), err)
a.marshalErrors(rw, 0, err)
return
}
switch len(payload.Data) {
case 0:
err := httputil.ErrInvalidInput()
err.Detail = "nothing to insert"
a.marshalErrors(rw, 0, err)
return
case 1:
default:
err := httputil.ErrInvalidInput()
err.Detail = "bulk insert not implemented yet."
a.marshalErrors(rw, 0, err)
return
}
model := payload.Data[0]
// Divide fieldset into fields and relations.
if len(payload.FieldSets) != 1 {
err := httputil.ErrInvalidInput()
err.Detail = "bulk inserted not implemented yet"
a.marshalErrors(rw, 0, err)
return
}
var selectedPrimary bool
fields := mapping.FieldSet{}
for _, field := range payload.FieldSets[0] {
switch field.Kind() {
case mapping.KindRelationshipSingle, mapping.KindRelationshipMultiple:
if field.Relationship().Kind() == mapping.RelBelongsTo {
relationer, ok := model.(mapping.SingleRelationer)
if !ok {
log.Errorf("Model: '%s' doesn't implement mapping.SingleRelationer interface", mStruct.Collection())
a.marshalErrors(rw, 500, httputil.ErrInternalError())
return
}
relation, err := relationer.GetRelationModel(field)
if err != nil {
log.Errorf("Getting relation model failed: %v", err)
a.marshalErrors(rw, 500, httputil.ErrInternalError())
return
}
if relation.IsPrimaryKeyZero() {
a.marshalErrors(rw, http.StatusBadRequest, httputil.ErrInvalidQueryParameter())
return
}
fielder, ok := model.(mapping.Fielder)
if !ok {
log.Errorf("Model: '%s' doesn't implement mapping.Fielder interface", mStruct.Collection())
a.marshalErrors(rw, 500, httputil.ErrInternalError())
}
foreignKey := field.Relationship().ForeignKey()
if err = fielder.SetFieldValue(foreignKey, relation.GetPrimaryKeyValue()); err != nil {
log.Errorf("Setting relation foreign key value failed: %v", err)
a.marshalErrors(rw, 500, httputil.ErrInternalError())
return
}
if !fields.Contains(foreignKey) {
fields = append(fields, foreignKey)
}
}
payload.IncludedRelations = append(payload.IncludedRelations, &query.IncludedRelation{
StructField: field,
})
case mapping.KindPrimary:
fields = append(fields, field)
selectedPrimary = true
case mapping.KindAttribute:
fields = append(fields, field)
}
}
payload.FieldSets = []mapping.FieldSet{fields}
// Check if a model is allowed to set it's primary key.
if selectedPrimary && !mStruct.AllowClientID() {
log.Debug2f("Creating: '%s' with client-generated ID is forbidden", mStruct.Collection())
err := httputil.ErrInvalidJSONFieldValue()
err.Detail = "Client-Generated ID is not allowed for this model."
err.Status = "403"
a.marshalErrors(rw, http.StatusForbidden, err)
return
}
// Prepare parameters.
ctx := req.Context()
db := a.DB
var (
result *codec.Payload
isTransactioner bool
)
// Try to get model's InsertHandler.
modelHandler, hasModelHandler := a.handlers[mStruct]
if hasModelHandler {
if w, ok := modelHandler.(server.WithContextInserter); ok {
if ctx, err = w.InsertWithContext(ctx); err != nil {
a.marshalErrors(rw, 0, err)
return
}
}
var it server.InsertTransactioner
if it, isTransactioner = modelHandler.(server.InsertTransactioner); isTransactioner {
err = database.RunInTransaction(ctx, db, it.InsertWithTransaction(), func(db database.DB) error {
result, err = a.insertHandleChain(ctx, db, payload)
return err
})
}
}
if !isTransactioner {
result, err = a.insertHandleChain(ctx, db, payload)
}
if err != nil {
a.marshalErrors(rw, 0, err)
return
}
// if the primary was provided in the input and if the config doesn't allow to return
// created value with given client-id - return simple status NoContent
if selectedPrimary && a.Options.NoContentOnInsert {
// if the primary was provided
rw.WriteHeader(http.StatusNoContent)
return
}
if len(result.Data) == 0 {
log.Error("No data in the result payload")
a.marshalErrors(rw, 500, httputil.ErrInternalError())
return
}
// get the primary field value so that it could be used for the jsonapi marshal process.
stringID, err := model.GetPrimaryKeyStringValue()
if err != nil {
log.Errorf("Getting primary key string value failed for the model: %v", model)
a.marshalErrors(rw, 500, httputil.ErrInternalError())
return
}
linkType := codec.ResourceLink
// but if the config doesn't allow that - set 'jsonapi.NoLink'
if !a.Options.PayloadLinks {
linkType = codec.NoLink
}
result.ModelStruct = mStruct
result.FieldSets = []mapping.FieldSet{append(mStruct.Fields(), mStruct.RelationFields()...)}
if result.MarshalLinks.Type == codec.NoLink {
result.MarshalLinks = codec.LinkOptions{
Type: linkType,
BaseURL: a.Options.PathPrefix,
RootID: stringID,
Collection: mStruct.Collection(),
}
}
result.MarshalSingularFormat = true
a.marshalPayload(rw, result, http.StatusCreated)
}
}
func (a *API) insertHandleChain(ctx context.Context, db database.DB, payload *codec.Payload) (*codec.Payload, error) {
modelHandler, hasModelHandler := a.handlers[payload.ModelStruct]
if hasModelHandler {
beforeInserter, ok := modelHandler.(server.BeforeInsertHandler)
if ok {
if err := beforeInserter.HandleBeforeInsert(ctx, db, payload); err != nil {
return nil, err
}
}
}
insertHandler, ok := modelHandler.(server.InsertHandler)
if !ok {
// If nothing is being found take the default handler.
insertHandler = a.defaultHandler
}
result, err := insertHandler.HandleInsert(ctx, db, payload)
if err != nil {
log.Debugf("Handle insert failed: %v", err)
return nil, err
}
if hasModelHandler {
afterHandler, ok := modelHandler.(server.AfterInsertHandler)
if ok {
if err = afterHandler.HandleAfterInsert(ctx, db, result); err != nil {
return nil, err
}
}
}
return result, nil
}