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

Function createUser

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

createUser creates a new user

(ctx dotweb.Context)

Source from the content-addressed store, hash-verified

143
144// createUser creates a new user
145func createUser(ctx dotweb.Context) error {
146 var user User
147 if err := json.Unmarshal(ctx.Request().PostBody(), &user); err != nil {
148 ctx.Response().Header().Set("Content-Type", "application/json")
149 return ctx.WriteJsonC(400, ErrorResponse{Error: "Invalid JSON"})
150 }
151
152 if user.Name == "" || user.Email == "" {
153 ctx.Response().Header().Set("Content-Type", "application/json")
154 return ctx.WriteJsonC(400, ErrorResponse{Error: "Name and email required"})
155 }
156
157 mu.Lock()
158 user.ID = nextID
159 nextID++
160 users[user.ID] = &user
161 mu.Unlock()
162
163 ctx.Response().Header().Set("Content-Type", "application/json")
164 return ctx.WriteJsonC(201, SuccessResponse{
165 Message: "User created",
166 Data: &user,
167 })
168}
169
170// updateUser updates a user
171func updateUser(ctx dotweb.Context) error {

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected