MCPcopy Create free account
hub / github.com/devfeel/dotweb / updateUser

Function updateUser

example/json-api/main.go:171–208  ·  view source on GitHub ↗

updateUser updates a user

(ctx dotweb.Context)

Source from the content-addressed store, hash-verified

169
170// updateUser updates a user
171func updateUser(ctx dotweb.Context) error {
172 idStr := ctx.GetRouterName("id")
173 id, err := strconv.Atoi(idStr)
174 if err != nil {
175 ctx.Response().Header().Set("Content-Type", "application/json")
176 return ctx.WriteJsonC(400, ErrorResponse{Error: "Invalid user ID"})
177 }
178
179 mu.RLock()
180 user, ok := users[id]
181 mu.RUnlock()
182
183 if !ok {
184 ctx.Response().Header().Set("Content-Type", "application/json")
185 return ctx.WriteJsonC(404, ErrorResponse{Error: "User not found"})
186 }
187
188 var update User
189 if err := json.Unmarshal(ctx.Request().PostBody(), &update); err != nil {
190 ctx.Response().Header().Set("Content-Type", "application/json")
191 return ctx.WriteJsonC(400, ErrorResponse{Error: "Invalid JSON"})
192 }
193
194 mu.Lock()
195 if update.Name != "" {
196 user.Name = update.Name
197 }
198 if update.Email != "" {
199 user.Email = update.Email
200 }
201 mu.Unlock()
202
203 ctx.Response().Header().Set("Content-Type", "application/json")
204 return ctx.WriteJsonC(200, SuccessResponse{
205 Message: "User updated",
206 Data: user,
207 })
208}
209
210// deleteUser deletes a user
211func deleteUser(ctx dotweb.Context) error {

Callers

nothing calls this directly

Calls 7

PostBodyMethod · 0.80
GetRouterNameMethod · 0.65
SetMethod · 0.65
ResponseMethod · 0.65
WriteJsonCMethod · 0.65
RequestMethod · 0.65
HeaderMethod · 0.45

Tested by

no test coverage detected