get endpoint for listing snippets working

This commit is contained in:
Travis Shears 2025-06-07 11:16:28 +02:00
parent 700710df56
commit 4acc4e2201
5 changed files with 57 additions and 10 deletions

View file

@ -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"]
}
}

View file

@ -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"]
}
}

View file

@ -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

View file

@ -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)]]))

View file

@ -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)})]]))