diff --git a/view/who.go b/view/who.go index 409773e..73bbd4e 100644 --- a/view/who.go +++ b/view/who.go @@ -30,6 +30,8 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error { return whoPut(namespace, g, w, r) case http.MethodPost: return whoPost(namespace, g, w, r) + case http.MethodDelete: + return whoDelete(namespace, g, w, r) default: http.NotFound(w, r) 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) } + +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"}`) +} diff --git a/view/who_test.go b/view/who_test.go index 1127535..6b7b6bf 100644 --- a/view/who_test.go +++ b/view/who_test.go @@ -156,6 +156,30 @@ func TestWho(t *testing.T) { b, _ = json.MarshalIndent(o, "", " ") 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 {