-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateRoll.go
75 lines (67 loc) · 2.12 KB
/
createRoll.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
package main
import (
"encoding/json"
"errors"
"net/http"
"reflect"
"strconv"
"strings"
)
// Create
func addClient(newClient Client) error {
for _, client := range clients {
if client.ID == newClient.ID {
return errors.New("client already exists")
}
}
clients = append(clients, newClient)
return nil
}
func createClient(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var newClient Client
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields() // disallow unknown fields in the request body
if err := decoder.Decode(&newClient); err != nil {
// Handle errors in decoding request body
w.WriteHeader(http.StatusBadRequest)
message := "Invalid request body"
json.NewEncoder(w).Encode(map[string]string{"error": message})
return
}
// Check if client with given ID already exists
for _, client := range clients {
if client.ID == newClient.ID {
w.WriteHeader(http.StatusBadRequest) // Set the status code to 400
message := "Client with given ID already exists"
json.NewEncoder(w).Encode(map[string]string{"error": message})
return
}
}
// Check if newClient has any unknown fields
v := reflect.ValueOf(newClient)
for i := 0; i < v.NumField(); i++ {
fieldName := v.Type().Field(i).Name
if !strings.HasPrefix(fieldName, "ID") &&
!strings.HasPrefix(fieldName, "Name") &&
!strings.HasPrefix(fieldName, "JobTitle") &&
!strings.HasPrefix(fieldName, "BusinessUnit") &&
!strings.HasPrefix(fieldName, "Gender") &&
!strings.HasPrefix(fieldName, "Salary") &&
!strings.HasPrefix(fieldName, "City") &&
!strings.HasPrefix(fieldName, "Country") {
message := "unknown field in new client"
json.NewEncoder(w).Encode(map[string]string{"error": message})
return
}
}
// Handle errors in adding new client
newClient.ID = strconv.Itoa(len(clients) + 2 + 2000)
if err := addClient(newClient); err != nil {
w.WriteHeader(http.StatusInternalServerError) // Set the status code to 500
message := "Unable to create client"
json.NewEncoder(w).Encode(map[string]string{"error": message})
return
}
json.NewEncoder(w).Encode(newClient)
}