updateUser updates a user
(ctx dotweb.Context)
| 169 | |
| 170 | // updateUser updates a user |
| 171 | func 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 |
| 211 | func deleteUser(ctx dotweb.Context) error { |
nothing calls this directly
no test coverage detected