Files
mtusi2026lab4/internal/httpapi/users.go
T

30 lines
635 B
Go

package httpapi
import "net/http"
type createUserRequest struct {
Username string `json:"username"`
DisplayName string `json:"display_name"`
}
func (api *API) createUser(w http.ResponseWriter, r *http.Request) {
var body createUserRequest
if !readJSON(w, r, &body, "username", "display_name") {
return
}
user, err := api.messenger.RegisterUser(body.Username, body.DisplayName)
if err != nil {
writeAppError(w, err)
return
}
writeJSON(w, http.StatusCreated, user)
}
func (api *API) currentUser(w http.ResponseWriter, r *http.Request) {
user := userFromContext(r.Context())
writeJSON(w, http.StatusOK, user)
}