/
patch.go
50 lines (37 loc) · 898 Bytes
/
patch.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
package apicollectionv1
import (
"context"
"encoding/json"
"io"
"net/http"
"github.com/fulldump/box"
"github.com/fulldump/inceptiondb/collection"
)
func patch(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
s := GetServicer(ctx)
collectionName := box.GetUrlParameter(ctx, "collectionName")
col, err := s.GetCollection(collectionName)
if err != nil {
return err // todo: handle/wrap this properly
}
requestBody, err := io.ReadAll(r.Body)
if err != nil {
return err
}
patch := struct {
Patch interface{}
}{}
json.Unmarshal(requestBody, &patch) // TODO: handle err
e := json.NewEncoder(w)
traverse(requestBody, col, func(row *collection.Row) bool {
err := col.Patch(row, patch.Patch)
if err != nil {
// TODO: handle err??
// return err
return true
}
e.Encode(row.Payload) // todo: handle err?
return true
})
return nil
}