MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / apiV1CreateUser

Function apiV1CreateUser

internal/web/api_v1_users.go:126–175  ·  view source on GitHub ↗

POST /admin/api/v1/users

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

124
125// POST /admin/api/v1/users
126func apiV1CreateUser(w http.ResponseWriter, r *http.Request) {
127 var body struct {
128 Username string `json:"Username"`
129 Password string `json:"Password"`
130 Role string `json:"Role"`
131 Email string `json:"EmailAddress"`
132 }
133 if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
134 writeAPIError(w, http.StatusBadRequest, "malformed request body: "+err.Error())
135 return
136 }
137
138 if body.Username == "" {
139 writeAPIError(w, http.StatusBadRequest, "Username is required")
140 return
141 }
142
143 if users.Exists(body.Username) {
144 writeAPIError(w, http.StatusConflict, "username already exists")
145 return
146 }
147
148 u := users.NewUserRecord(0, 0)
149 if err := u.SetUsername(body.Username); err != nil {
150 writeAPIError(w, http.StatusBadRequest, err.Error())
151 return
152 }
153 if body.Password != "" {
154 if err := u.SetPassword(body.Password); err != nil {
155 writeAPIError(w, http.StatusBadRequest, err.Error())
156 return
157 }
158 }
159 if body.Role != "" {
160 u.Role = body.Role
161 }
162 if body.Email != "" {
163 u.EmailAddress = body.Email
164 }
165
166 if err := users.CreateUser(u); err != nil {
167 writeAPIError(w, http.StatusBadRequest, err.Error())
168 return
169 }
170
171 writeJSON(w, http.StatusCreated, APIResponse[*users.UserRecord]{
172 Success: true,
173 Data: u,
174 })
175}
176
177// GET /admin/api/v1/users/search
178//

Callers

nothing calls this directly

Calls 7

SetUsernameMethod · 0.95
SetPasswordMethod · 0.95
ExistsFunction · 0.92
NewUserRecordFunction · 0.92
CreateUserFunction · 0.92
writeAPIErrorFunction · 0.85
writeJSONFunction · 0.85

Tested by

no test coverage detected