diff --git a/bruno/CodeSnippets/create_snippet.bru b/bruno/CodeSnippets/create_snippet.bru index 26ad351..638f2b0 100644 --- a/bruno/CodeSnippets/create_snippet.bru +++ b/bruno/CodeSnippets/create_snippet.bru @@ -4,8 +4,17 @@ meta { seq: 3 } -get { - url: - body: none +post { + url: {{host}}/api/snippet + body: json auth: none } + +body:json { + { + "title": "Test Snippet", + "slug": "test-snippet", + "markdown": "this is a test", + "tags": ["test"] + } +} diff --git a/bruno/CodeSnippets/get_snippets.bru b/bruno/CodeSnippets/get_snippets.bru new file mode 100644 index 0000000..60b3da2 --- /dev/null +++ b/bruno/CodeSnippets/get_snippets.bru @@ -0,0 +1,24 @@ +meta { + name: get_snippets + type: http + seq: 4 +} + +get { + url: {{host}}/api/snippet?limit=10&skip=10 + body: none + auth: none +} + +params:query { + limit: 10 + skip: 10 +} + +body:json { + { + "title": "Test Snippet", + "markdown": "## Cool Snippet\ndoes a cool thing", + "tags": ["git", "jj"] + } +} diff --git a/src/snippets/api.clj b/src/snippets/api.clj index d5e195e..c128804 100644 --- a/src/snippets/api.clj +++ b/src/snippets/api.clj @@ -2,7 +2,9 @@ (:require [ring.adapter.jetty :as jetty] [clojure.pprint :as pprint] + [snippets.db :as db] [muuntaja.middleware :as mm] + [ring.middleware.params] [reitit.ring :as rr])) (defn handle-ping [args] @@ -13,6 +15,12 @@ (pprint/pprint body) {:status 200, :body "snippet created"}) +(defn handle-view-snippet [{params :query-params}] + (let [limit (Integer/parseInt (get params "limit" "10")) + skip (Integer/parseInt (get params "skip" "0"))] + {:status 200 + :body (db/list-snippets {:limit limit :skip skip})})) + (defn wrap [handler id] (fn [request] (update (handler request) :wrap (fnil conj '()) id))) @@ -20,10 +28,12 @@ (def app (rr/ring-handler (rr/router - ["/api" {:middleware [mm/wrap-format + ["/api" {:middleware [ring.middleware.params/wrap-params + mm/wrap-format [wrap :api]]} ["/ping" {:get handle-ping}] - ["/snippet" {:post handle-create-snippet}]]) + ["/snippet" {:post handle-create-snippet + :get handle-view-snippet}]]) (rr/create-default-handler))) ;; (defroutes app-routes diff --git a/src/snippets/backfill.clj b/src/snippets/backfill.clj index 4a1a488..672d458 100644 --- a/src/snippets/backfill.clj +++ b/src/snippets/backfill.clj @@ -21,5 +21,5 @@ :tags (:snippet_types frontmatter)))) (map #(dissoc % :full-path))))) -;; create snippets from files -;; save snippets in db +;; used repl to do backfill +;; (doseq [s old-snippets] (xt/execute-tx db/client [[:put-docs :snippets (merge {:xt/id (:slug s)} s)]])) diff --git a/src/snippets/db.clj b/src/snippets/db.clj index c376378..a2df8b5 100644 --- a/src/snippets/db.clj +++ b/src/snippets/db.clj @@ -15,7 +15,11 @@ (defn uuid [] (str (java.util.UUID/randomUUID))) -(defn save-snippet [snippet] - (xt/execute-tx client [[:put-docs :snippets (merge snippet {:xt/id (uuid)})]])) +;; xtdb query docs: https://docs.xtdb.com/reference/main/xtql/queries.html#_limit +(defn list-snippets [{:keys [skip limit]}] + (xt/q client + (eval + (read-string + (format "(quote (-> (from :snippets [* {:xt/id id}]) (offset %s) (limit %s)))" skip limit))))) -;; (xt/q client ['(from :users [{:name "travis"}])]) + ;; (xt/execute-tx client [[:put-docs :snippets (merge snippet {:xt/id (uuid)})]]))