Add delete endpoint

master
Bel LaPointe 2020-07-23 21:20:08 -06:00
parent bdfc1c346b
commit 5af3f6f7b6
2 changed files with 40 additions and 0 deletions

View File

@ -30,6 +30,8 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return whoPut(namespace, g, w, r) return whoPut(namespace, g, w, r)
case http.MethodPost: case http.MethodPost:
return whoPost(namespace, g, w, r) return whoPost(namespace, g, w, r)
case http.MethodDelete:
return whoDelete(namespace, g, w, r)
default: default:
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
@ -134,3 +136,17 @@ func whoPost(namespace string, g storage.Graph, w http.ResponseWriter, r *http.R
return whoGet(namespace, g, w, r) return whoGet(namespace, g, w, r)
} }
func whoDelete(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
}
if err := g.Delete(r.Context(), namespace, entity.One{Name: id}); err != nil {
return err
}
return json.NewEncoder(w).Encode(`{"status":"ok"}`)
}

View File

@ -156,6 +156,30 @@ func TestWho(t *testing.T) {
b, _ = json.MarshalIndent(o, "", " ") b, _ = json.MarshalIndent(o, "", " ")
t.Logf("POST POST:\n%s", b) t.Logf("POST POST:\n%s", b)
}) })
t.Run("delete real", func(t *testing.T) {
r := httptest.NewRequest(http.MethodDelete, "/who/col?id=NEWBIE"+want.Name, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
r = httptest.NewRequest(http.MethodGet, "/who/col?id=NEWBIE"+want.Name, nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
})
t.Run("delete fake", func(t *testing.T) {
r := httptest.NewRequest(http.MethodDelete, "/who/col?id=FAKER"+want.Name, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
})
} }
func fillDB(t *testing.T, g storage.Graph) []entity.One { func fillDB(t *testing.T, g storage.Graph) []entity.One {