-
Notifications
You must be signed in to change notification settings - Fork 10
/
app_users.go
197 lines (166 loc) · 4.29 KB
/
app_users.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
package crocgodyl
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
type User struct {
ID int `json:"id"`
ExternalID string `json:"external_id"`
UUID string `json:"uuid"`
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Language string `json:"language"`
RootAdmin bool `json:"root_admin"`
TwoFactor bool `json:"2fa"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
func (u *User) FullName() string {
return u.FirstName + " " + u.LastName
}
func (u *User) UpdateDescriptor() *UpdateUserDescriptor {
return &UpdateUserDescriptor{
ExternalID: u.ExternalID,
Email: u.Email,
Username: u.Username,
FirstName: u.FirstName,
LastName: u.LastName,
Language: u.Language,
RootAdmin: u.RootAdmin,
}
}
func (a *Application) GetUsers() ([]*User, error) {
req := a.newRequest("GET", "/users", nil)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Data []struct {
Attributes *User `json:"attributes"`
} `json:"data"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
users := make([]*User, 0, len(model.Data))
for _, u := range model.Data {
users = append(users, u.Attributes)
}
return users, nil
}
func (a *Application) GetUser(id int) (*User, error) {
req := a.newRequest("GET", fmt.Sprintf("/users/%d", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes User `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
func (a *Application) GetUserExternal(id string) (*User, error) {
req := a.newRequest("GET", fmt.Sprintf("/users/external/%s", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes User `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
type CreateUserDescriptor struct {
ExternalID string `json:"external_id,omitempty"`
Email string `json:"email"`
Username string `json:"username"`
Password string `json:"password,omitempty"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Language string `json:"language,omitempty"`
RootAdmin bool `json:"root_admin,omitempty"`
}
func (a *Application) CreateUser(fields CreateUserDescriptor) (*User, error) {
data, _ := json.Marshal(fields)
body := bytes.Buffer{}
body.Write(data)
req := a.newRequest("POST", "/users", &body)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes User `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
type UpdateUserDescriptor struct {
ExternalID string `json:"external_id,omitempty"`
Email string `json:"email,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Language string `json:"language,omitempty"`
RootAdmin bool `json:"root_admin,omitempty"`
}
func (a *Application) UpdateUser(id int, fields UpdateUserDescriptor) (*User, error) {
data, _ := json.Marshal(fields)
body := bytes.Buffer{}
body.Write(data)
req := a.newRequest("PATCH", fmt.Sprintf("/users/%d", id), &body)
res, err := a.Http.Do(req)
if err != nil {
return nil, err
}
buf, err := validate(res)
if err != nil {
return nil, err
}
var model struct {
Attributes User `json:"attributes"`
}
if err = json.Unmarshal(buf, &model); err != nil {
return nil, err
}
return &model.Attributes, nil
}
func (a *Application) DeleteUser(id int) error {
req := a.newRequest("DELETE", fmt.Sprintf("/users/%d", id), nil)
res, err := a.Http.Do(req)
if err != nil {
return err
}
_, err = validate(res)
return err
}