44 lines
1023 B
Go
44 lines
1023 B
Go
package view
|
|
|
|
import (
|
|
"encoding/json"
|
|
"local/dndex/storage"
|
|
"local/dndex/storage/entity"
|
|
"net/http"
|
|
)
|
|
|
|
func register(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
|
switch r.Method {
|
|
case http.MethodPost:
|
|
return registerPost(g, w, r)
|
|
default:
|
|
http.NotFound(w, r)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func registerPost(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
|
namespace, err := getAuthNamespace(r)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return nil
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return nil
|
|
}
|
|
password := r.FormValue("password")
|
|
if len(password) == 0 && r.URL.Query().Get("public") == "" {
|
|
http.Error(w, `{"error": "password required"}`, http.StatusBadRequest)
|
|
return nil
|
|
}
|
|
one := entity.One{
|
|
Name: UserKey,
|
|
Title: password,
|
|
}
|
|
if err := g.Insert(r.Context(), namespace, one); err != nil {
|
|
return err
|
|
}
|
|
return json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
|
}
|