This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
/
user.go
executable file
·130 lines (109 loc) · 4.11 KB
/
user.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
package http
import (
"net/http"
"github.com/Tecsisa/foulkon/api"
"github.com/julienschmidt/httprouter"
)
// REQUESTS
type CreateUserRequest struct {
ExternalID string `json:"externalId,omitempty"`
Path string `json:"path,omitempty"`
}
type UpdateUserRequest struct {
Path string `json:"path,omitempty"`
}
// RESPONSES
type GetUserExternalIDsResponse struct {
ExternalIDs []string `json:"users,omitempty"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Total int `json:"total"`
}
type GetGroupsByUserIdResponse struct {
Groups []api.UserGroups `json:"groups,omitempty"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Total int `json:"total"`
}
// HANDLERS
func (wh *WorkerHandler) HandleAddUser(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Process request
request := &CreateUserRequest{}
requestInfo, _, apiErr := wh.processHttpRequest(r, w, nil, request)
if apiErr != nil {
wh.processHttpResponse(r, w, requestInfo, nil, apiErr, http.StatusBadRequest)
return
}
// Call user API to create user
response, err := wh.worker.UserApi.AddUser(requestInfo, request.ExternalID, request.Path)
wh.processHttpResponse(r, w, requestInfo, response, err, http.StatusCreated)
}
func (wh *WorkerHandler) HandleGetUserByExternalID(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Process request
requestInfo, filterData, apiErr := wh.processHttpRequest(r, w, ps, nil)
if apiErr != nil {
wh.processHttpResponse(r, w, requestInfo, nil, apiErr, http.StatusBadRequest)
return
}
// Call user API to get user
response, err := wh.worker.UserApi.GetUserByExternalID(requestInfo, filterData.ExternalID)
wh.processHttpResponse(r, w, requestInfo, response, err, http.StatusOK)
}
func (wh *WorkerHandler) HandleListUsers(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Process request
requestInfo, filterData, apiErr := wh.processHttpRequest(r, w, ps, nil)
if apiErr != nil {
wh.processHttpResponse(r, w, requestInfo, nil, apiErr, http.StatusBadRequest)
return
}
// Call user API to list users
result, total, err := wh.worker.UserApi.ListUsers(requestInfo, filterData)
// Create response
response := &GetUserExternalIDsResponse{
ExternalIDs: result,
Offset: filterData.Offset,
Limit: filterData.Limit,
Total: total,
}
wh.processHttpResponse(r, w, requestInfo, response, err, http.StatusOK)
}
func (wh *WorkerHandler) HandleUpdateUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Process request
request := &UpdateUserRequest{}
requestInfo, filterData, apiErr := wh.processHttpRequest(r, w, ps, request)
if apiErr != nil {
wh.processHttpResponse(r, w, requestInfo, nil, apiErr, http.StatusBadRequest)
return
}
// Call user API to update user
response, err := wh.worker.UserApi.UpdateUser(requestInfo, filterData.ExternalID, request.Path)
wh.processHttpResponse(r, w, requestInfo, response, err, http.StatusOK)
}
func (wh *WorkerHandler) HandleRemoveUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Process request
requestInfo, filterData, apiErr := wh.processHttpRequest(r, w, ps, nil)
if apiErr != nil {
wh.processHttpResponse(r, w, requestInfo, nil, apiErr, http.StatusBadRequest)
return
}
// Call user API to delete user
err := wh.worker.UserApi.RemoveUser(requestInfo, filterData.ExternalID)
wh.processHttpResponse(r, w, requestInfo, nil, err, http.StatusNoContent)
}
func (wh *WorkerHandler) HandleListGroupsByUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Process request
requestInfo, filterData, apiErr := wh.processHttpRequest(r, w, ps, nil)
if apiErr != nil {
wh.processHttpResponse(r, w, requestInfo, nil, apiErr, http.StatusBadRequest)
return
}
// Call user API to retrieve user's groups
result, total, err := wh.worker.UserApi.ListGroupsByUser(requestInfo, filterData)
response := GetGroupsByUserIdResponse{
Groups: result,
Offset: filterData.Offset,
Limit: filterData.Limit,
Total: total,
}
wh.processHttpResponse(r, w, requestInfo, response, err, http.StatusOK)
}