impl entities except for PATCH
This commit is contained in:
46
.view/register.go
Normal file
46
.view/register.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"local/dndex/storage"
|
||||
"local/dndex/storage/entity"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func register(g storage.RateLimitedGraph, 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.RateLimitedGraph, 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{
|
||||
ID: uuid.New().String(),
|
||||
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"})
|
||||
}
|
||||
Reference in New Issue
Block a user