createUser creates a new user
(ctx dotweb.Context)
| 143 | |
| 144 | // createUser creates a new user |
| 145 | func 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 |
| 171 | func updateUser(ctx dotweb.Context) error { |