-
Notifications
You must be signed in to change notification settings - Fork 0
/
org.go
267 lines (227 loc) · 5.61 KB
/
org.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/johnamadeo/server"
)
// Organization :
type Organization struct {
Name string
Admin string
CrossMatchTrait string
}
// CreateOrganizationRequestBody :
type CreateOrganizationRequestBody struct {
Organization string `json:"org"`
}
// SetCrossMatchTraitRequestBody :
type SetCrossMatchTraitRequestBody struct {
Trait string `json:"trait"`
}
// GetOrganizationsHandler : HTTP Handler for fetching all the organizations an admin manages
func GetOrganizationsHandler(w http.ResponseWriter, r *http.Request) {
function := "GetOrganizationsHandler"
if r.Method != "GET" && r.Method != "" {
LogAndWriteErr(
w,
errors.New("Only GET requests are allowed at this route"),
http.StatusMethodNotAllowed,
function,
)
return
}
queries, ok := r.URL.Query()["admin"]
if !ok || len(queries) > 1 {
LogAndWriteErr(
w,
errors.New("request query parameters must contain 'admin'"),
http.StatusBadRequest,
function,
)
return
}
organizations, err := getOrganizations(queries[0])
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
resp := map[string][]string{"orgs": organizations}
bytes, err := json.Marshal(resp)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
LogAndWrite(w, bytes, http.StatusOK, function)
}
// CreateOrganizationHandler : HTTP handler for creating a new organization
func CreateOrganizationHandler(w http.ResponseWriter, r *http.Request) {
function := "CreateOrganizationHandler"
if r.Method != "POST" {
LogAndWriteErr(
w,
errors.New("Only POST requests are allowed at this route"),
http.StatusMethodNotAllowed,
function,
)
return
}
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
defer r.Body.Close()
var body CreateOrganizationRequestBody
err = json.Unmarshal(bytes, &body)
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
queries, ok := r.URL.Query()["admin"]
if !ok || len(queries) > 1 {
LogAndWriteErr(
w,
errors.New("request query parameters must contain 'admin'"),
http.StatusBadRequest,
function,
)
return
}
admin := queries[0]
fmt.Println(body.Organization, admin)
err = createOrganization(body.Organization, admin)
if err != nil {
LogAndWriteStatusInternalServerError(w, err, function)
return
}
LogAndWrite(
w,
server.StrToBytes("Successfully created new organization"),
http.StatusCreated,
function,
)
}
// CrossMatchTraitHandler : HTTP handler for changing or setting a cross match trait for an organization
func CrossMatchTraitHandler(w http.ResponseWriter, r *http.Request) {
function := "CrossMatchTraitHandler"
if r.Method != "POST" {
LogAndWriteErr(w, errors.New("Only POST requests are allowed at this route"), http.StatusMethodNotAllowed, function)
return
}
orgname, err := getQueryParam(r, "org")
if err != nil {
LogAndWriteStatusBadRequest(w, err, function)
return
}
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
LogAndWriteErr(w, errors.New("Malformed body."), http.StatusBadRequest, function)
return
}
defer r.Body.Close()
var body SetCrossMatchTraitRequestBody
err = json.Unmarshal(bytes, &body)
if err != nil {
LogAndWriteErr(w, errors.New("Request body is malformed"), http.StatusBadRequest, function)
return
}
err = setCrossMatchTrait(orgname, body.Trait)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(server.ErrToBytes(err))
return
}
LogAndWrite(w, server.StrToBytes("Successfully set the cross match trait"), http.StatusCreated, function)
}
// GetOrganizations :
func getOrganizations(admin string) ([]string, error) {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return []string{}, err
}
rows, err := db.Query(
"SELECT name FROM organizations WHERE admin = $1",
admin,
)
if err != nil {
return []string{}, err
}
organizations := []string{}
for rows.Next() {
var organization string
err := rows.Scan(&organization)
if err != nil {
return []string{}, err
}
organizations = append(organizations, organization)
}
return organizations, nil
}
func createOrganization(name string, admin string) error {
if name == "" {
return errors.New("Organization name cannot be an empty string")
}
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return err
}
_, err = db.Exec(
"INSERT INTO organizations (name, admin) VALUES ($1, $2)",
name,
admin,
)
if err != nil {
return err
}
return nil
}
// GetCrossMatchTrait : Placeholder
func GetCrossMatchTrait(orgname string) (string, error) {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return "", err
}
rows, err := db.Query(
"SELECT cross_match_trait FROM organizations WHERE name = $1",
orgname,
)
if err != nil {
return "", err
}
var crossMatchTraitSQL sql.NullString
for rows.Next() {
err := rows.Scan(&crossMatchTraitSQL)
if err != nil {
return "", err
}
break
}
crossMatchTrait := ""
if crossMatchTraitSQL.Valid {
crossMatchTrait = crossMatchTraitSQL.String
}
return crossMatchTrait, nil
}
func setCrossMatchTrait(orgname string, crossMatchTrait string) error {
db, err := server.CreateDBConnection(LocalDBConnection)
defer db.Close()
if err != nil {
return err
}
_, err = db.Exec(
"UPDATE organizations SET cross_match_trait = $1 WHERE name = $2",
crossMatchTrait,
orgname,
)
if err != nil {
return err
}
return nil
}