/
doc_new.go
143 lines (124 loc) · 4.42 KB
/
doc_new.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
/*
Copyright © 2020 FairOS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"encoding/json"
"net/http"
"strings"
"github.com/fairdatasociety/fairOS-dfs/pkg/auth"
"github.com/fairdatasociety/fairOS-dfs/pkg/collection"
"resenje.org/jsonhttp"
)
// DocRequest is used for creating a doc
type DocRequest struct {
PodName string `json:"podName,omitempty"`
TableName string `json:"tableName,omitempty"`
SimpleIndex string `json:"si,omitempty"`
Mutable bool `json:"mutable,omitempty"`
}
// SimpleDocRequest is used in doc delete request
type SimpleDocRequest struct {
PodName string `json:"podName,omitempty"`
TableName string `json:"tableName,omitempty"`
}
// DocCreateHandler godoc
//
// @Summary Create in doc table
// @Description DocCreateHandler is the api handler to create a new document database
// @ID doc-create
// @Tags doc
// @Accept json
// @Produce json
// @Param doc_request body DocRequest true "doc table info. si or simple index is a comma separated list of keys and their types. eg: 'first_name=string,age=number'. valid index types can be 'string', 'number', 'map', 'list'. default index is 'id' and it should be of type string"
// @Param Cookie header string true "cookie parameter"
// @Success 201 {object} response
// @Failure 400 {object} response
// @Failure 500 {object} response
// @Router /v1/doc/new [post]
func (h *Handler) DocCreateHandler(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if contentType != jsonContentType {
h.logger.Errorf("doc create: invalid request body type")
jsonhttp.BadRequest(w, &response{Message: "doc create: invalid request body type"})
return
}
decoder := json.NewDecoder(r.Body)
var docReq DocRequest
err := decoder.Decode(&docReq)
if err != nil {
h.logger.Errorf("doc create: could not decode arguments")
jsonhttp.BadRequest(w, &response{Message: "doc create: could not decode arguments"})
return
}
podName := docReq.PodName
if podName == "" {
h.logger.Errorf("doc create: \"podName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "doc create: \"podName\" argument missing"})
return
}
name := docReq.TableName
if name == "" {
h.logger.Errorf("doc create: \"tableName\" argument missing")
jsonhttp.BadRequest(w, &response{Message: "doc create: \"tableName\" argument missing"})
return
}
// by default, add the index type "id" as stringIndex
indexes := make(map[string]collection.IndexType)
si := docReq.SimpleIndex
if si != "" {
idxs := strings.Split(si, ",")
for _, idx := range idxs {
nt := strings.Split(idx, "=")
if len(nt) != 2 {
h.logger.Errorf("doc create: \"si\" invalid argument ")
jsonhttp.BadRequest(w, &response{Message: "doc create: \"si\" invalid argument"})
return
}
switch nt[1] {
case "string":
indexes[nt[0]] = collection.StringIndex
case "number":
indexes[nt[0]] = collection.NumberIndex
case "map":
indexes[nt[0]] = collection.MapIndex
case "list":
indexes[nt[0]] = collection.ListIndex
case "bytes":
default:
h.logger.Errorf("doc create: invalid \"indexType\" ")
jsonhttp.BadRequest(w, &response{Message: "doc create: invalid \"indexType\""})
return
}
}
}
mutable := docReq.Mutable
// get sessionId from request
sessionId, err := auth.GetSessionIdFromRequest(r)
if err != nil {
h.logger.Errorf("sessionId parse failed: ", err)
jsonhttp.BadRequest(w, &response{Message: ErrUnauthorized.Error()})
return
}
if sessionId == "" {
h.logger.Error("sessionId not set: ", err)
jsonhttp.BadRequest(w, &response{Message: ErrUnauthorized.Error()})
return
}
err = h.dfsAPI.DocCreate(sessionId, podName, name, indexes, mutable)
if err != nil {
h.logger.Errorf("doc create: %v", err)
jsonhttp.InternalServerError(w, &response{Message: "doc create: " + err.Error()})
return
}
jsonhttp.Created(w, &response{Message: "document db created"})
}