HEAD and PATCH to create peers and list all items

This commit is contained in:
Bel LaPointe
2020-07-23 21:45:52 -06:00
parent 5af3f6f7b6
commit 1411171107
3 changed files with 134 additions and 6 deletions

View File

@@ -32,6 +32,10 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return whoPost(namespace, g, w, r)
case http.MethodDelete:
return whoDelete(namespace, g, w, r)
case http.MethodPatch:
return whoPatch(namespace, g, w, r)
case http.MethodHead:
return whoHead(namespace, g, w, r)
default:
http.NotFound(w, r)
return nil
@@ -150,3 +154,45 @@ func whoDelete(namespace string, g storage.Graph, w http.ResponseWriter, r *http
return json.NewEncoder(w).Encode(`{"status":"ok"}`)
}
func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, `{"error":"no ?id provided"}`, http.StatusBadRequest)
return nil
}
one := entity.One{}
if err := json.NewDecoder(r.Body).Decode(&one); err != nil {
return err
}
if one.Name == "" {
http.Error(w, `{"error":"no name provided"}`, http.StatusBadRequest)
return nil
}
relationship := one.Relationship
one.Relationship = ""
if err := g.Insert(r.Context(), namespace, one); err != nil {
return err
}
one.Relationship = relationship
if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.Set{Key: "connections." + one.Name, Value: one.Peer()}); err != nil {
return err
}
return whoGet(namespace, g, w, r)
}
func whoHead(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
ones, err := g.List(r.Context(), namespace)
if err != nil {
return err
}
names := make([]string, len(ones))
for i := range ones {
names[i] = ones[i].Name
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(names)
}