forked from rs/rest-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod_item_put.go
107 lines (104 loc) · 3.32 KB
/
method_item_put.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
package rest
import (
"context"
"net/http"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/schema"
)
// itemPut handles PUT resquests on an item URL
//
// Reference: http://tools.ietf.org/html/rfc2616#section-9.6
func itemPut(ctx context.Context, r *http.Request, route *RouteMatch) (status int, headers http.Header, body interface{}) {
var payload map[string]interface{}
if e := decodePayload(r, &payload); e != nil {
return e.Code, nil, e
}
lookup, e := route.Lookup()
if e != nil {
return e.Code, nil, e
}
rsrc := route.Resource()
// Fetch original item if exist (PUT can be used to create a document with a manual id)
var original *resource.Item
if l, err := rsrc.Find(ctx, lookup, 0, 1); err != nil && err != ErrNotFound {
e = NewError(err)
return e.Code, nil, e
} else if len(l.Items) == 1 {
original = l.Items[0]
}
// Check if method is allowed based on the type of PUT:
// - PUT on non existing item = create
// - PUT on existing item = replace
mode := resource.Create
if original != nil {
// If original is found, the mode is replace rather than create
mode = resource.Replace
}
if !rsrc.Conf().IsModeAllowed(mode) {
return 405, nil, &Error{405, "Invalid method", nil}
}
// If-Match / If-Unmodified-Since handling
if err := checkIntegrityRequest(r, original); err != nil {
return err.Code, nil, err
}
status = 200
var changes map[string]interface{}
var base map[string]interface{}
if original == nil {
// PUT used to create a new document
changes, base = rsrc.Validator().Prepare(ctx, payload, nil, false)
status = 201
} else {
// PUT used to replace an existing document
changes, base = rsrc.Validator().Prepare(ctx, payload, &original.Payload, true)
}
// Append lookup fields to base payload so it isn't caught by ReadOnly
// (i.e.: contains id and parent resource refs if any)
for k, v := range route.ResourcePath.Values() {
base[k] = v
// Also, ensure there's no tombstone set on the field
if changes[k] == schema.Tombstone {
delete(changes, k)
}
}
doc, errs := rsrc.Validator().Validate(changes, base)
if len(errs) > 0 {
return 422, nil, &Error{422, "Document contains error(s)", errs}
}
// Check that fields with the Reference validator reference an existing object
if err := checkReferences(ctx, doc, rsrc.Validator()); err != nil {
return err.Code, nil, err
}
if original != nil {
if id, found := doc["id"]; found && id != original.ID {
return 422, nil, &Error{422, "Cannot change document ID", nil}
}
}
item, err := resource.NewItem(doc)
if err != nil {
e = NewError(err)
return e.Code, nil, e
}
// If we have an original item, pass it to the handler so we make sure
// we are still replacing the same version of the object as handler is
// supposed check the original etag before storing when an original object
// is provided.
if original != nil {
if err := rsrc.Update(ctx, item, original); err != nil {
e = NewError(err)
return e.Code, nil, e
}
} else {
if err := rsrc.Insert(ctx, []*resource.Item{item}); err != nil {
e = NewError(err)
return e.Code, nil, e
}
}
// Apply selector so response gets the same format as read requests
item.Payload, err = lookup.ApplySelector(ctx, rsrc.Validator(), item.Payload, getReferenceResolver(ctx, rsrc))
if err != nil {
e = NewError(err)
return e.Code, nil, e
}
return status, nil, item
}